Solved

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

Posted on 2014-02-25
3
341 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
understanding interfaces 3 248
SQL Select 9 270
child constructor and parent constructor, overriding and overloading 6 88
Permutation and Combination 9 98
Setting up SVN Server using Windows and Apache Purpose of the document:       This article will explain the process of how to configure SVN repository in a windows environment using APACHE web server. What is SVN? (http://subversion.tigris.org/) …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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