-6

i wan't to suspend and resume main function is there any way except sleep method. Please Help

user2058173
  • 109
  • 1
  • 10
  • 2
    too vague, What are you really trying to do? a sleep? or wait for a condition on another thread ? see pthread in this case https://computing.llnl.gov/tutorials/pthreads/ – hl037_ Mar 16 '14 at 09:04
  • Detention would be a more appropriate punishment. – Captain Obvlious Mar 16 '14 at 09:06
  • Please see http://stackoverflow.com/questions/22419068/stdmutexlock-blocking-cpu-usage/22419120#comment34090624_22419120 – Ed Heal Mar 16 '14 at 09:10
  • i wan't to avoid spin lock...just when one event has done then i wan't to continue my main() method execution – user2058173 Mar 16 '14 at 09:12

1 Answers1

0

Assuming you have other threads running apart from main, you can use use a sem_wait (semaphore initialized to 0) in main() and then from your thread you can call sem_post whenever you want main() to run.

Read about semaphore and usage:

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • plz tell me if i do sem_wait (2 times) then after doing sem_post one time does it invoke the one thread if semaphore is initialized to 0 – user2058173 Mar 16 '14 at 12:42
  • The value of `semaphore` has to be `>=1` for `sem_wait` to proceed. If you start with initial value `0`, then `sem_wait` will block untill `sem_post` increments it by `1` to `1`. For every `sem_wait`, there has to be a corresponding `sem_post` which unblocks the blocked thread. Hope this helps you! – brokenfoot Mar 16 '14 at 12:51
  • but after 2 sem_wait and 1 sem_post whether the first thread that is blocked will continue or need one more sem_post which will then continue running the two threads at same time (init with 0) – user2058173 Mar 16 '14 at 13:07
  • and if initialized with 0 then sem_wait will block and makes value equal -1 then after sem_post its value will bcm 0 and thread will be unblocked so it does not need value >=1 – user2058173 Mar 16 '14 at 13:12
  • Whether it remains `0` or goes `negative`, is implementation dependent, either way the concept is the same. I'll talk about the version where it remains at zero. For `sem_wait` to proceed, the value of `semaphore` has to be `non-zero`. If it's `zero`, then it waits, no decrement operation is performed. If the value is `0` and two threads perform `sem_wait` then value remains at zero, not decremented. WHenever the 1st `sem_post` occurs, value is incremented by 1, the thread that had been waiting for longer time proceeeds, makes sem value back to `0`. Check out the link I have added in the ans. – brokenfoot Mar 16 '14 at 13:31