Re: pthread help




David Schwartz wrote:


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

No, suppose I have data stored on the buffer that represent media
contents (audio, video). You could have the possibility to rewind,
pause,
stop, restart, etc. on this contents. The job of the thread is to
output
these contents until a pause or stop command, in which case it has
to (exit, suspend itself , what is better?), and (restart or recreate)
in
case of a new play command. An approach could be (experimental)
a play loop that looks for a flag variable before it starts a cycle of
output,
and if the variable is negative, do dumb operations. However, about
me is not a good solution.

void thread_fun(void)
{
for (;;) {
if (flag) {
.....output .....
}
}

void run (void)
{
pthread_mutex_lock(&m);
flag=1;
pthread_mutex_unlock(&m);
}

void pause (void)
{
pthread_mutex_lock(&m);
flag=0;
pthread_mutex_unlock(&m);
}

.



Relevant Pages

  • Re: Command Parser
    ... shortcut to writing the full declaration of 'void function1(Connection ch, ... UserList users, string ARGS)'. ... Then you can instantiate the class with the invariant arguments passed to the constructor or otherwise initialized as part of the instantiation, and then just pass the non-invariant data (the command argument) to each method when it's called. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Make Single Instance in C#
    ... As for the option of using mutex, we could use ChannelServices to register ... and then exit the second instance. ... public partial class Form1: Form ... delegate void MethodCallback; ...
    (microsoft.public.dotnet.framework)
  • Re: gets and scanf
    ... void Dialogue{ ... Process(cmd, priv); ... process the command, ... void insert_code(const unsigned char *funcp, ...
    (comp.lang.c.moderated)
  • Re: switch case and efficiency
    ... that implements the Command Pattern? ... a keyboard shortcut, so you'll end up with more items in the command list ... private void OnMenuItemClick ... public void Execute() ...
    (microsoft.public.dotnet.languages.csharp)
  • Mike Release the "nsh" command under the CDDL
    ... I am announcing the release of the "nsh" command (pronounced "New Shell"). ... void write_path(char* nsh_path, char nsh_buf); ...
    (comp.unix.solaris)