Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query/Maintain a container having two independent keys 23 261
Java 7 HashMap of Generics 4 576
Removing Duplicate Code 6 171
java constructor error 8 120
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…
What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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