-2

I am learning POSIX threads. My question - How to suspend and resume pthread ? Like threads in Java, is there any function like wait, notify etc? I want to run the single pthread multiple times according to some situations.

thanks in advance

charlotte
  • 1,031
  • 3
  • 12
  • 19
  • https://stackoverflow.com/questions/11468333/linux-threads-suspend-resume This might have what you need. – J0hn Jul 11 '17 at 12:41

1 Answers1

0

How to suspend and resume pthread ? Like threads in Java, is there any function like wait, notify etc?

Read about conditional variables in pthread. Basically, conditional variables allow you to check for a condition before a thread can proceed further. Say for example there are 3 threads which will only proceed if some condition is true. In case, this condition is false, the threads will release the mutex lock they are holding and "wait". As the lock on the shared object of type pthread_mutex_t is released, some other thread(s) will go ahead and do their stuff thus at some point in time the condition will become true (hopefully). Check the function called pthread_cond_wait.

Once the said condition becomes true (or is satisfied), then the functions pthread_cond_signal / pthread_cond_broadcast could be used to "notify" the threads which were waiting on the pthread_cond_t object. These threads (3 threads in this simple explanation) then acquire the mutex lock automatically (of course, one thread at a time) and go about their business.

Check this tutorial.

I want to run the single pthread multiple times according to some situations.

Care to explain further?

babon
  • 3,615
  • 2
  • 20
  • 20