问题
I am laying in the framework for a tool that will generate a binary data table. I a plan on making this multithreaded to take full advantage of the 24 cores at my disposal. (I am estimating that the wall time for generation of the data will be about 50 days–in a single thread.). I have done this in the past using server/client design with socket communication as I needed to distributed this across multiple machines.
This time around, I am looking at a single machine/multi-threaded approach and am trying to figure out how to do this the right way.
The master thread will handle the assignment of tasks to each child thread and determining the offset into the allocated memory.
Each thread will write to a unique address range within the allocated memory. Because these blocks will never overlap between records, no two threads will ever attempt to write the same offset.
void computeRecord(void *taskInput)
{
struct TaskData *taskData = (TaskData *)(taskInput);
RecordData data;
// A huge long computation block to populate data
// (4-5 second run time)
long record_id = taskData->record_id;
char *buffer = taskData->start_buffer;
// mutex lock needed here ??
int n_bytes = sizeof(RecordData)
memcpy( (char *)(buffer+record_id*n_bytes), (char *)(&recordData) n_bytes);
// mutex unlock here ?
}
Long setup. Short question. Is the mutex necessary in this case?
回答1:
For best performance, you'll want your data to be aligned to cache-lines - this will avoid the different CPU cores from "bouncing" cache-lines between each other.
But regardless of that, as long as we are talking separate bytes that are interacted on independently, no protection is needed. Only if more than one thread is accessing the very same byte [also applies when accessing multiple bytes, of course].
Edit: This statement is of course only true if the processor has byte addressing. The processor that comes to mind that doesn't is Alpha, but there may be others.
(Edit2: No, doesn't matter in C++11 compliant compiler, it's up to the compiler to deal with byte addressing in a thread-safe manner)
来源:https://stackoverflow.com/questions/34700839/is-mutex-needed-for-different-offsets-into-allocated-heap-memory