Want to avoid one call to sempost for eight threads
Scenario is:
nthreads = 8
for j=0 = nthreads
pthread_created (thread_func)
for 1=0 to 1000
{
// Some work.
for j=0 = nthreads
sempost(mutex1)
// thread_func()
for j=0 = nthreads
semwait(mutex2)
// Some work.
}
thread_func()
{
while(1)
{
semwait(mutex1)
// Thread funcionality.
sempost(mutex2)
}
}
Que: I want 开发者_JAVA百科a more efficient synchronization technique than current one. I want to remove one call to sempost
(total 8, one for one thread) at least. At the same time I want every thread to start their work at the same time.
How can it be achieved?
doing barrier or sempost/wait by each thread would involved the same expense. I want to remove one set of sempost/wait or one barrier in your case
What you want is a barrier, which blocks waiting threads until all threads have entered barrier_wait(). Depending on your threading implementation, you might already have a barrier primitive (pthread_barrier for instance).
The other option is a conditional wait (cond_wait), which would be ideal for your "launch" option.
To synchronize, you need to use one of these primitives. Synchronization has overhead as you have determined - either make larger workloads, synchronize less, or use another concurrency scheme (STM)
精彩评论