asked on
#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;
}
ASKER
Programming theory deals with the design, implementation, analysis, characterization, and classification of programming languages and their individual features, along with introductory programming concepts like program structure, variable declaration and conditional and looping constructs. Sub-disciplines include the formal semantics of programming languages, type theory, program analysis and transformation, comparative programming language analysis, metaprogramming, domain-specific languages, compiler construction and run-time systems.
TRUSTED BY
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