pthread linux mutet:example1
#include<iostream> #include<unistd.h> #include<pthread.h> #include<string> using namespace std; #define START 1 #define END 0 int status = START; void* queuewhile(void *); void* dosomething(void *); pthread_mutex_t mutex; int main() { //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); string strMsg = "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 *) { int a = 16; int b = 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) { string strMsg = (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
#include<iostream> #include<unistd.h> #include<pthread.h> #include<string> using namespace std; #define START 1 #define END 0 int status = START; class MSG{ public: int m_state; string m_msg; }; static MSG g_msg; void* queuewhile(void *); void* dosomething(void *); pthread_mutex_t mutex; int main() { //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); string strMsg = "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 *) { string a = "q"; string b = ""; while(cin >> b) { pthread_mutex_lock(&mutex); cout<< "something happened"<< endl; if(a.compare(b) == 0) { cout<< "now,we shut down!"<<endl; status = END; }else if(b.compare("w") ==0) { cin >>b; //cout<< "ops, we just keep go on!"<<endl; g_msg.m_state += 1; g_msg.m_msg = b; b.clear(); } pthread_mutex_unlock(&mutex); } } void* queuewhile(void *msg) { string strMsg = (char*)msg; if(strMsg.empty()) { cout<< "there no thing"<< endl; }else { while(status) { pthread_mutex_lock(&mutex); cout<<strMsg<<endl; if(g_msg.m_state > 0) { cout <<"the message is:"<< g_msg.m_msg<<endl; g_msg.m_state --; } pthread_mutex_unlock(&mutex); sleep(1); } //cout << strMsg<<endl; } }
//exec child process in the thread
#include<iostream> #include<cstdlib> #include<unistd.h> #include<pthread.h> #include<string> using namespace std; #define START 1 #define END 0 int status = START; class MSG{ public: int m_state; string m_msg; }; static MSG g_msg; void* queuewhile(void *); void* dosomething(void *); pthread_mutex_t mutex; int main() { //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); string strMsg = "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 *) { string a = "q"; string b = ""; while(cin >> b) { pthread_mutex_lock(&mutex); cout<< "something happened"<< endl; if(a.compare(b) == 0) { cout<< "now,we shut down!"<<endl; status = END; }else if(b.compare("w") ==0) { cin >>b; //cout<< "ops, we just keep go on!"<<endl; g_msg.m_state += 1; g_msg.m_msg = b; b.clear(); } pthread_mutex_unlock(&mutex); } } void* queuewhile(void *msg) { string strMsg = (char*)msg; if(strMsg.empty()) { cout<< "there no thing"<< endl; }else { while(status) { pthread_mutex_lock(&mutex); cout<<strMsg<<endl; if(g_msg.m_state > 0) { string strPath = getenv("PWD"); //cout << "the path is :" << strPath<< endl; strPath.append("/child "); strPath.append(g_msg.m_msg.c_str()); system(strPath.c_str()); cout <<"the message is:"<< g_msg.m_msg<<endl; g_msg.m_state --; } pthread_mutex_unlock(&mutex); sleep(1); } //cout << strMsg<<endl; } }