Re: pthread help
- From: "David Schwartz" <davids@xxxxxxxxxxxxx>
- Date: 10 Nov 2006 09:42:29 -0800
gaetanoortisi@xxxxxxxx wrote:
I have some trouble in thread programming. What I do as an exercise is
to create a thread on a loop function involved in data stream output
from a buffer. The main execution thread have the possibility to pause
the loop thread, change the buffer location the loop thread use as
source
(rewind it, for example), restart or signal the loop thread to restart
output.
I read libc info pages, I think mutexes and condition variables could
help
but is a little too abstract to me to begin an approach.
Can anyone point me to some elegant solutions?
I would recommend avoiding example code where you give a thread an
infinite amount of work to do and then try to change what it's doing.
It's a very unrealistic scenario -- threads much more typically finish
the jobs they're doing or continue working on a job until they can't
make further progress. Trying to artificially force a master/slave type
arrangement on threads just results in code that's nothing like real
world code.
How about having one thread that writes data to a file and one or more
other threads that give it data to write or command it to close the
file and open a new one or rewind to the beginning of the file.
That way, you can have a linked-list of jobs for the file thread to do.
A thread can acquire a mutex, add a job to the linked list, and then
unlock the mutex and signal the condition variable. The file thread can
lock the mutex, while the linked list is empty, block on the condtition
variable, then pop the head job off the queue and release the mutex. It
can then do whatever that job says, whether it's write the data or
whatever.
DS
.
- Follow-Ups:
- Re: pthread help
- From: gaetanoortisi
- Re: pthread help
- References:
- pthread help
- From: gaetanoortisi
- pthread help
- Prev by Date: pthread help
- Next by Date: Re: "unconnected" UDP connections
- Previous by thread: pthread help
- Next by thread: Re: pthread help
- Index(es):
Relevant Pages
|