Is a mutex required for different offsets in the allocated heap memory

I am laying the framework for a tool that will generate a binary data table. I plan to make this multithreaded in order to make full use of the 24 cores at my disposal. (I estimate that the wall time for data generation will be about 50 days - in one thread.). In the past, I did this using a server / client design with socket support, since I needed to distribute it across multiple machines.

This time I look at one machine / multi-threaded approach and try to figure out how to do it right.

The main thread will handle the task assignment for each child thread and determine the offset in the allocated memory.

Each stream will write to a unique range of addresses in the allocated memory. Since these blocks will never overlap between records, neither of the two threads will ever try to write the same offset.

{Metadata} {Rec1} {Rec2} {Rec3} {Rec4} {...} {...} {...}

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 a mutex required in this case?

+7
c ++ c multithreading mutex
source share
1 answer

To achieve maximum performance, you want your data to be aligned along the cache lines - this will avoid the fact that different CPU cores will “skip” cache lines among themselves.

But regardless of this, while we are talking about individual bytes that interact independently, protection is not required. Only if more than one stream accesses the same byte [also applies to multiple bytes, of course].

Edit: This operator, of course, is valid only if the processor has byte addressing. A processor that comes to mind that is not an Alpha, but may be different. (Edit2: No, it doesn't matter in a C ++ 11 compatible compiler, before the compiler to handle byte addressing in a thread-safe manner)

+5
source share

All Articles