Solved

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

Posted on 2014-02-25
3
313 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Pretext and Context: In this series, I wish to design the data model of systems in question. I will start by defining the scope and boundaries of the system, followed by subsystems involved, then finally the entities in those systems and subsystems…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

12 Experts available now in Live!

Get 1:1 Help Now