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.

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?
c ++ c multithreading mutex
MikeMayer67
source share