How can I make thread sleep or wake up?

General OpenMP discussion

How can I make thread sleep or wake up?

Postby bluekds » Tue Aug 05, 2008 12:31 am

Hi everyone.

I want to make a thread sleep until other thread complete its work.
But I could not find methods that make a thread sleep or wake up.

I think I can do it by using lock and unlock.

Bellow is my idea that makes thread1 sleep until thread 2 finish its work.
But VC2005 recognize it as a deadlock.

Code: Select all
Thread1 {
  foo() ;
  omp_set_lock(&lock) ;
  omp_unset_lock(&lock) ;

foo() {
  omp_set_lock(&lock) ;

Thread2 {
  // do one's work
  omp_unset_lock(&lock) ;

I think I can avoid VS2005's false deadlock detection by using additional lock variable.
But it seems a little complex.

Is there someone who haver better idea?
plz help me.

Thanks to read.

Re: How can I make thread sleep or wake up?

Postby ejd » Tue Aug 05, 2008 6:20 am

Your question might be better asked on a Microsoft forum since this forum is about general OpenMP questions and not implementation specific questions. In general, OpenMP doesn't provide a mechanism to put threads to sleep. When OpenMP was originally designed, it was with the idea of partitioning work to threads based on worksharing constructs. Many implementations found that because of skewed workloads, it was beneficial to allow the user some ability to say whether a thread should spin or sleep at barriers, so they added implementation specific options (or environment variables) to control this.

With the addition of tasks to the Version 3 spec, OpenMP has been extended to handle more than just worksharing constructs. You will also see that an attempt was made to standardize the environment variable (omp_wait_policy) used to control what threads do at barriers. A great deal of discussion went into this and you can see that this variable only provides a "hint" to an implementation. It was extremely hard to get the committee to agree on even this general form. In any case, this is still only a hint and doesn't even really define at which barriers it will be used for. Explicit thread control is a long ways off - it ever - for OpenMP. If you really need this level of control, then you might have to go to a lower level and use pthreads, posix threads, windows threads, etc.
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: How can I make thread sleep or wake up?

Postby bluekds » Tue Aug 05, 2008 8:29 pm

Thanks to your answer.
I will try to use pthread.

Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests