#include <iostream>#include<unistd.h>#include<pthread.h>#include<string>using namespacestd;#defineSTART 1#defineEND 0intStatus =START;void* Queuewhile (void*);void* DoSomething (void*);p thread_mutex_t mutexes;intMain () {//int status = START;cout <<"Please,enter Something, if the number is larger.the process ends"<<Endl; pthread_t Thread1, Thread2; Sleep (2); Pthread_mutex_init (&mutex, NULL); stringSTRMSG ="Hello,world"; Pthread_create (&thread1, NULL, Queuewhile, (void*) Strmsg.c_str ()); Pthread_create (&thread2, NULL, dosomething, NULL); Pthread_join (Thread1, NULL); Pthread_mutex_destroy (&mutex); cout<<"The process is closing"<<Endl; return 0;}void* DoSomething (void*){ intA = -; intb =0; while(Cin >>b) {Pthread_mutex_lock (&mutex); cout<<"something happened"<<Endl; if(b>a) {cout<<"Now,we shut down!"<<Endl; Status=END; }Else{cout<<"Ops, we just keep go on!"<<Endl; } pthread_mutex_unlock (&mutex); }}void* Queuewhile (void*msg) { stringSTRMSG = (Char*) msg; if(Strmsg.empty ()) {cout<<"there no thing"<<Endl; }Else { while(status) {Pthread_mutex_lock (&mutex); cout<<strMsg<<Endl; Pthread_mutex_unlock (&mutex); Sleep (1); } //cout << strmsg<<endl; }}
g++-O test domything.cpp-lpthread
Pthread Linux Mutet:example1