开发者

How to use mutex or conditional variable to design this (C++)?

I am quite new for pa开发者_JAVA技巧rallel programming. Right now I have a problem and try to TBB 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 instead 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 container 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?


Reentrant means that a function, foo, can be interrupted while it's running, say by a signal, and you can then call foo again, in the signal handler, before the first call completes.

Imagine a call that locks a mutex before starting the function. This call wouldn't be re-entrant because if you tried to call it again you'd block forever trying to acquire the mutex. However, a function like returning the sum of two parameters, x and y, could be reentrant if x and y were both saved on the stack so each call had its own stack storage.

Compare reentrant to thread-safe. A thread-safe function takes care to not have a problem if two calls happen at the same time, say by locking, or using atomic operations. A reentrant function guarantees that it can call itself again even if the initial call is interrupted. Note that a reentrant function isn't necessarily thread safe

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜