Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is mutex needed for different offsets into allocated heap memory

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.

 {Meta Data}{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 the mutex necessary in this case?

like image 888
MikeMayer67 Avatar asked Jan 10 '16 00:01

MikeMayer67


People also ask

Do multiple threads share the same heap?

The answer is simple: all threads in C share the same address space. This means all memory, the heap included, is shared between all threads.

What data structure is implemented in heap memory?

Heaps are usually implemented with an array, as follows: Each element in the array represents a node of the heap, and. The parent / child relationship is defined implicitly by the elements' indices in the array.


1 Answers

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)

like image 169
Mats Petersson Avatar answered Nov 15 '22 12:11

Mats Petersson