* Co-thread for parallel processing
@ 2011-12-02 9:23 Randi Botse
2011-12-02 9:28 ` Bogdan Cristea
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Randi Botse @ 2011-12-02 9:23 UTC (permalink / raw)
To: linux-c-programming
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?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Co-thread for parallel processing
2011-12-02 9:23 Co-thread for parallel processing Randi Botse
@ 2011-12-02 9:28 ` Bogdan Cristea
2011-12-02 10:10 ` Randi Botse
2011-12-02 11:26 ` Srinivasa T N
2011-12-03 21:13 ` Hendrik Visage
2 siblings, 1 reply; 5+ messages in thread
From: Bogdan Cristea @ 2011-12-02 9:28 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
On Friday 02 December 2011 10:23:49 you wrote:
> 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?
I would use a semaphore which should be locked by the treads once the
condition variable is set en unlocked when the result is available for the
main thread.
> 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?
I don't understand, please provide a working example
--
Bogdan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Co-thread for parallel processing
2011-12-02 9:28 ` Bogdan Cristea
@ 2011-12-02 10:10 ` Randi Botse
0 siblings, 0 replies; 5+ messages in thread
From: Randi Botse @ 2011-12-02 10:10 UTC (permalink / raw)
To: Bogdan Cristea; +Cc: linux-c-programming
> I would use a semaphore...
Ok, let see what I can do with that.
2. Just forget that, that might be caused by my frustation... ;)
> 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?
I don't understand, please provide a working example
> Bogdan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Co-thread for parallel processing
2011-12-02 9:23 Co-thread for parallel processing Randi Botse
2011-12-02 9:28 ` Bogdan Cristea
@ 2011-12-02 11:26 ` Srinivasa T N
2011-12-03 21:13 ` Hendrik Visage
2 siblings, 0 replies; 5+ messages in thread
From: Srinivasa T N @ 2011-12-02 11:26 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
> 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?
I think you need a pthread_cond_broadcast () after do_work () in each
thread.
Regards,
Seenu.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Co-thread for parallel processing
2011-12-02 9:23 Co-thread for parallel processing Randi Botse
2011-12-02 9:28 ` Bogdan Cristea
2011-12-02 11:26 ` Srinivasa T N
@ 2011-12-03 21:13 ` Hendrik Visage
2 siblings, 0 replies; 5+ messages in thread
From: Hendrik Visage @ 2011-12-03 21:13 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
On Fri, Dec 2, 2011 at 11:23 AM, Randi Botse <nightdecoder@gmail.com> wrote:
> 1. What the proper way to the main thread to wait for all co-threads
> until finished?
I'll do a semop() with sem_op=1 in each thread's start, and a
sem_op=-1 when each thread's finished.
Then in the main thread I'll wait with a sem_op = 0.
> 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?
Please show the code snippet in the co-thread that is not working as
well as the mainthread's method you try to wake up the co-threads
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-12-03 21:13 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-02 9:23 Co-thread for parallel processing Randi Botse
2011-12-02 9:28 ` Bogdan Cristea
2011-12-02 10:10 ` Randi Botse
2011-12-02 11:26 ` Srinivasa T N
2011-12-03 21:13 ` Hendrik Visage
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).