[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
pthreads condition variables
Say I have a large multicore, SMP system that supports 256 parallel
I want to use all 256 threads to perform some computation in parallel.
1. create 256 threads
2. threads go to sleep waiting for work
3. partition data into 256 parallel regions
4. signal all threads to process data
When threads go to sleep on line 2, they execute:
When the main thread signals all threads on line 4, it executes:
The problem here is when all 256 threads wake up at the same time, they
will all try to lock the same mutex, creating a lot of contention.
Because the data is partitioned into 256 parallel regions, each thread
works on a different data set, hence no need for mutual exclusion. When
a thread wakes up, it immediately calls pthread_mutex_unlock, which
seems to be redundant, but necessary to allow other threads to continue.
The alternative is to have each thread sleep on its own private
condition variable and mutex. This would require calling
'pthread_cond_signal' for each thread, that is 256 time in total.
So is there a way to design a function similar to
pthread_cond_broadcast, that doesn't cause each thread to lock the same
mutex when it wakes up?
Main Index |
Thread Index |