Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-25
3
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1080 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The CRUD Functions CRUD, meaning "Create, Read, Update, Delete (http://en.wikipedia.org/wiki/Create,_read,_update_and_delete)" is a common term to data base developers.  It describes the essential functions of data base table maintenance.  This art…
Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 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