user494085
user494085

Reputation: 336

Are mutex changes broadcast to other cores on a multicore system?

I want to manipulate a tree-based data structure with several threads and protect each node with a mutex. The target architecture has several hundred threads and operates on (virtual) shared memory.

If CPU 1 acquires a lock on a node it is to manipulate, will this change of state mutex be communicated to all other cores actively or only if requested?

I would assume that like all other changes of shared variables, mutex changes are written to main memory. Caches of other CPUs are invalidated and replaced based on their policy. The value of the mutex is only accessed by another CPU if it wants to lock it or if their cache replacement policy loads it from memory.

Is this correct, or are mutexes treated differently and broadcast immediately or withheld until requested? I'd like to avoid invalidating hundreds of caches each time a node is locked. This question indicates that the bus is locked each time a mutex is used, which does not seem to be a problem.

Edit

I'm asking about the C++11 mutexes as implemented by GCC 4.8. (As far as I know, the Intel Compiler also uses the GCC libraries for anything to do with the C++11 specification.) I'm worried about overhead more than about correctness.

Upvotes: 2

Views: 842

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69892

All threads running on any core will co-operate with the mutex as you would expect. You don't need to take any special action.

You can reasonably expect there to be some memory fences involved under the covers, but this is an implementation detail and not something you would normally want to concern yourself with.

side note: if you can parallelise your process fully (i.e. one thread does not depend on the data produced by another) then you might find that giving each thread its own copy of the data (and therefore not requiring mutexes at all) will give you very much improved performance.

Upvotes: 2

Related Questions