Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2465 Discussions

Question about the TBB mutex and conditional variable.

I am quite new for parallel programming. Right now I have a problem and try to use TBB to solve it.

To simplified the problem, we can imagine that there are several people (tasks) picking balls and putting them into a container (concurrent_vector) according to the hash value of the number on the ball. Because we need to make sure it is lossless. The ball is represented as a link list (this is the reason to use concurrent_vector instread of concurrent_hashmap, I need random access). If the container is almost full (there is a threshold and condition to judge it). One people will put all the balls from the current containner to a large container. For correctness, when he moving balls to the other container, all the other people should stop adding more balls and wait until he finishes. Due to moving balls around requires a lot of time, it would be better that all the other people stop the current task and help moving balls around.
How should I design it for better efficiency, should I use mutex, or spin_mutex, or conditional variable?
Because right now, I am using concurrent_vector, modifying the container contain is done in parallel. Do I need to lock the whole vector for the moving procedure?
Also I have a question about the TBB mutex. What does it mean no reenterance?
Thanks a lot.
0 Kudos
1 Reply
Valued Contributor III
Reentrant and recursive seem to be synonyms for mutexes (hmm, which is "better"?), and you can generally find such basic definitions on TBB has mutex and recursve_mutex, both thin wrappers for mutexes provided by the operating system.

Is this a teaching exercise perhaps? It seems to assume general threads, not tasks, You would use mutex if you expect long wait times (seems appropriate during "moving balls around"), spin_mutex for short wait times (busy waiting!), and a condition variable assumes the use of a mutex (it does not replace one).

But maybe you should first spend some time actually doing the exercise? :-)
0 Kudos