--新线程先停止,然后作为其清理过程的一部分,等待与另一个线程合并或“连接”
?pthread_join(pid, NULL);
?cout << "ok!" << endl;
?return 0;
}
//终端执行:$ g++ -o test_create_thread test_create_thread.cpp -lpthread
//? ? $ ./test_create_thread
2. test_thread_mutex.cpp:
#include
#include
using namespace std;
pthread_t tid[2];
int counter = 0;
pthread_mutex_t lock;
void* run(void* arg)
{
?pthread_mutex_lock(&lock);
?unsigned long i = 0;
?counter += 1;
?cout << "Job " << counter << " started!" << endl;
?for (i = 0; i<(0xFFFFFFFF); i++);
?cout << "Job " << counter << " finished!" << endl;
?pthread_mutex_unlock(&lock);
?return NULL;
}
int main()
{
?int i = 0, err = -1;
?if (pthread_mutex_init(&lock, NULL) != 0) {
? cout << "mutex init failed" << endl;
? return -1;
?}
?while (i < 2) {
? err = pthread_create(&(tid[i]), NULL, &run, NULL);
? if (err != 0)
? ?cout << "can't create thread!" << endl;
? i++;
?}
?pthread_join(tid[0], NULL);
?pthread_join(tid[1], NULL);
?pthread_mutex_destroy(&lock);
?cout << "ok!" << endl;
?return 0;
}
//终端执行:$ g++ -o test_thread_mutex test_thread_mutex.cpp -lpthread
//? ? $ ./test_thread_mutex
3. test_thread_cond.cpp:
#include
#include
#include
using namespace std;
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count = 0;
void* decrement_count(void* arg)
{
?pthread_mutex_lock(&count_lock);
?cout << "decrement_count get count_lock" << endl;
?while (count == 0) {
? cout << "decrement_count count == 0" << endl;
? cout << "decrement_count before cond_wait" << endl;
? pthread_cond_wait(&count_nonzero, &count_lock);
? cout << "decrement_count after cond_wait" << endl;
?}
?count = count + 1;
?pthread_mutex_unlock(&count_lock);
?return NULL;
}
void* increment_count(void* arg)
{
?pthread_mutex_lock(&count_lock);
?cout << "increment_count get count_lock" << endl;
?if (count == 0) {
? cout << "increment_count before cond_signal" << endl;
? pthread_cond_signal(&count_nonzero);
? cout << "increment_count after cond_signal" << endl;
?}
?count = count + 1;
?pthread_mutex_unlock(&count_lock);
?return NULL;
}
int main()
{
?pthread_t tid1, tid2;
?pthread_mutex_init(&count_lock, NULL);
?pthread_cond_init(&count_nonzero, NULL);
?pthread_create(&tid1, NULL, decrement_count, NULL);
?sleep(2);
?pthread_create(&tid2, NULL, increment_count, NULL);
?sleep(2);
?pthread_join(tid1, NULL);
?pthread_join(tid2, NULL);
?pthread_mutex_destroy(&count_lock);
?pthread_cond_destroy(&count_nonzero);
?cout << "ok!" << endl;
}
//终端执行:$ g++ -o test_thread_cond test_thread_cond.cpp -lpthread
//? ? $ ./test_thread_cond
4. test_thread_cond1.cpp:
#include
#include
#include
using namespace std;
pthread_mutex_t counter_lock;
pthread_cond_t counter_nonzero;
int counter = 0;
void* decrement_counter(void* argv);
void* increment_counter(void* argv);
int main()
{
?cout << "counter: " << counter << endl;
?pthread_mutex_init(&counter_lock, NULL);
?pthread_cond_init(&counter_nonzero, NULL);
?pthread_t thd1, thd2;
?int ret = -1;
?ret = pthread_create(&thd1, NULL, decrement_counter, NULL);
?if (ret){
? cout << "create thread1 fail" << endl;
? return -1;
?}
?ret = pthread_create(&thd2, NULL, increment_counter, NULL);
?if (ret){
? cout << "create thread2 fail" << endl;
? return -1;
?}
?int counter = 0;
?while (counter != 10) {
? cout << "counter(main): " << counter << endl;
? sleep(1);
? counter++;
?}
?pthread_join(thd1, NULL);
?pthread_join(thd2, NULL);
?pthread_mutex_destroy(&counter_lock);
?pthread_cond_destroy(&counter_nonzero);
?cout << "ok!" << endl;
}
void* decrement_counter(void* argv)
{
?cout << "counter(decrement): " << counter << endl;
?pthread_mutex_lock(&counter_lock);
?while (counter == 0)
? pthread_cond_wait(&counter_nonzero, &counter_lock); //进入阻塞(wait),等待激活(signal)
?cout << "counter--(decrement, before): " << counter << endl;
?counter--; //等待signal激活后再执行?
?cout << "counter--(decrement, after): " << counter << endl;
?pthread_mutex_unlock(&counter_lock);
?return NULL;
}
void* increment_counter(void* argv)
{
?cout << "counter(increment): " << counter << endl;
?pthread_mutex_lock(&counter_lock);
?if (counter == 0)
? pthread_cond_signal(&counter_nonzero); //激活(signal)阻塞(wait)的线程(先执行完signal线程,然后再执行wait线程)?
?cout << "counter++(increment, before): " << counter << endl;
?counter++;
?cout << "counter++(increment, after): " << counter << endl;
?pthread_mutex_unlock(&counter_lock);
?return NULL;
}
//终端执行:$ g++ -o test_thread_cond1 test_thread_cond1.cpp -lpthread
//? ? $ ./test_th