views:

152

answers:

2

Is there a function call that can associate with a specific thread to make the thread run or wait? I have 4 threads in my program and I am trying to figure out a way to tell any one of the threads to wait or run when I want them to.

+2  A: 

Your question is quite general. It really comes down to: Review the pthreads documentation.

If what you want is to have thread A wait for thread B to finish, check out pthread_join().

If what you want is to have thread A wait until thread B says it's okay to continue, you will need a mutex and a conditional variable. Check out pthread_cond_wait() and associated functions.

RarrRarrRarr
Apparently I need to type faster. Agreed - especially on pthread_cond_wait(), which is what I think they're looking for.
Brian Roach
Yeah, I am trying to pause a thread before finishing, let another thread run (2nd thread) , pause the second thread, let the third thread run...ect ... If that makes any sence
MRP
@MRP, as you are describing it, it doesn't. If each thread must run after another has finished, then there is little reason to use threads.
jbcreix
A: 

A common mechanism for handling multiple threads is a master/slave approach where one thread hands off tasks for others to perform. The crux to this style, which I think is what you're getting at, is to realize that even in the slave threads, each thread inherently has total control over its own execution. The master doesn't really "force" the other threads to do anything, the master can make a request but the slave thread must voluntarily accept direction from the master... hrm... so maybe master/slave is a misnomer here... Anyway, The common solution for the slave threads is in pseudocode

while (!should_exit):
    while (!have_something_to_do):
        wait_for_something_to_do()
    do_the_thing_i_was_waiting_for()

You can implement this strategy quite easily in C using a structure like the following (I'm resorting to pseudo-c-ish code for the example's sake though)

struct Slave {
    Mutex              thread_lock;
    ConditionVariable  thread_cv;
    int                exit_flag;
    void             (*thread_operation)();
};

void slave_thread( Slave * slave ) {
    while( !exit_flag )
    {
        lock( thread_lock );
        while( slave->thread_operation == NULL )
            slave->thread_cv.wait( thread_lock );
        unlock( thread_lock );
        (*slave->thread_operation)(); // do the master's bidding...
    }
}

void master_thread()
{
   Slave slave1;
   Slave slave2;

   slave1.thread_operation = NULL;
   slave2.thread_operation = NULL;

   // create the threads. They'll immediately block on the condition variable

   slave1.thread_operation = some_function_pointer;
   slave2.thread_operation = some_other_function_pointer;

   notify_one( slave1.thread_cv ) // Now that the function pointers are set, wake
   notify_one( slave2.thread_cv ) // the slave threads

}

It's an overly simplistic example, of course, but hopefully it'll give you the general idea.

Rakis
So if i understand correctly, the master thread controls a pointer which tells the slave thread to run next.
MRP
@MRP: You need to change the way you think about it - instead of thinking that the master thread tells the slave thread to execute, the master thread puts some work in the slave's "in-box", and the slave runs while there's some work for it to do.
caf
Ok than kyou that makes more sense
MRP