Co-thread for parallel processing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



My program use co-threads, because creating and reinitialize thread is
expensive, it first create threads and reuse them. The main thread
will send each co-thread a event (through condition variable)
simultaneously to do task in parallel way. Each co-thread has variety
work-time, let say it can be (in miliseconds) 200ms, 130ms, 317ms,
90ms etc to get result to be consumed by main thread.

Here I did to wake-up all threads in the main thread:

.....
for (i = 0; i < nthread; i++)
    pthread_cond_broadcast(&cond);
wait_all_worker(); /* wait for all worker threads to finish */
get_all_worker_result(); /* get result from all worker thread */
...


And worker-thread routines:

...
for (;;) {
  pthread_mutex_lock();
  pthread_cond_wait();
  pthread_mutex_unlock();

  do_work();
  fill_result();
}
....

My dilema is:

1. What the proper way to the main thread to wait for all co-threads
until finished?
2. After calling pthread_cond_signal() or pthread_cond_broadcast()
followed by sleep()/nanosleep(),
    the condition variable looks like not signaled, (the co-thread
routine doesn't run). What make this?
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux