Solved

Waiting in main thread for a flag to be set by thread

Posted on 2014-02-25
3
323 Views
Last Modified: 2014-03-07
#include<iostream.h>
#include <cstdlib>
#include <pthread.h>
using namespace std;

bool flag = false;

void* doStuff(void t)
{
	//does lots of stuff
	flag = true;
	
	//does lots of stuff. takes long time to do that
}
pthread_t threadCall(int t)
{
   pthread_t threads;
   int rc;
   int i;
      
      rc = pthread_create(&threads, NULL, doStuff, (void *)t);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }

 return threads;

}

int main()
{
	int t =10;
	pthread_t thread= threadCall(t);
	
	//does some function calls
	while(!flag);
	
	//do some stuff when flag gets true.
	
	pthread_join(thread, NULL);
	return 0;
}

Open in new window


above is my code. I want to wait in main thread for flag to be set true by another thread as you can see here doStuff() function set the flag true.  In main thread, statement while(!flag); waits in busy waiting until flag get true.

Is there any other way to wait in main thread except this busy waiting.

Thanks in advance
0
Comment
Question by:bsdex
  • 2
3 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39888863
What you have is called a spin lock.
What you want would be mutex with condition variables.
Here is a good explanation of how they work in your context (from the pthread library)
https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 270 total points
ID: 39888892
Here is a simple example similar to your code

#include<iostream.h>
#include <cstdlib>
#include <pthread.h>
using namespace std;

pthread_mutex_t mutex;
pthread_cond_t cond_var;

void* doStuff(void t)
{
	//does lots of stuff
        pthread_mutex_lock(&mutex); //Make sure signal is called after the wait
	pthread_cond_signal(&cond_var); //Tell the main thread to continue
        pthread_mutex_unlock(&mutex); //Unlock the mutex
	//does lots of stuff. takes long time to do that
}
pthread_t threadCall(int t)
{
   pthread_t threads;
   int rc;
   int i;
      
      rc = pthread_create(&threads, NULL, doStuff, (void *)t);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }

 return threads;

}

int main()
{
	int t =10;
        pthread_mutex_lock(&mutex); // Make sure signal is called after the wait
	pthread_t thread= threadCall(t);
	
	//does some function calls
	pthread_cond_wait(&cont_var, &mutex); //Unlocks the mutex and waits for the signal
	
	//do some stuff when flag gets true.
	
	pthread_join(thread, NULL);
	return 0;
}

Open in new window


Read up on mutex and condition variables to understand.
Quick intro
If the mutex is locked, the thread calling lock will wait until it unlocks. You can use this to make sure things happen in the proper order.
pthread_cond_wait unlocks the mutex and waits for a signal on the condition variable. The mutex is used to make sure signal is called after wait so it is not missed.
0
 
LVL 1

Author Closing Comment

by:bsdex
ID: 39911936
Thank you very much :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now