int main()
{
pthread_t pid;//thread handle
int err = -1;
int ival = 1;
float fval = 10.0F;
char buf[] = "func";
void* para[3] = { &ival, &fval, buf };
err = pthread_create(&pid, NULL, run, para);
if (err != 0) {
cout << "can't create thread!" << endl;
return -1;
}
//新线程创建之后主线程如何运行----主线程按顺序继续执行下一行程序
cout << "main thread!" << endl;
//新线程结束时如何处理----新线程先停止,然后作为其清理过程的一部分,等待与另一个线程合并或“连接”
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 <pthread.h>
#include <iostream>
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 <pthread.h>
#include <iostream>
#include <unistd.h>
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 <pthread.h>
#include <iostream>
#include <unistd.h>
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)