线程有三个操作
pthread_create
[线程创建],pthread_exit
[线程退出],pthread_join
[线程等待]
请看如下代码实例
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
void *my_print_1(void * ptr)
{
cout <<"I am thread " << pthread_self() << endl;
pthread_exit(0);
}
void *my_print_2(void *ptar)
{
sleep(1);
cout <<"I am thread " << pthread_self()<< endl;
pthread_exit(0);
}
int main()
{
pthread_t p1;
pthread_t p2;
int rec1 = pthread_create( &p1,NULL,my_print_1,NULL);
if(rec1)
{
cout <<"create p1 error!" << endl;
return 1;
}
int rec2 = pthread_create( &p2,NULL,my_print_2,NULL);
if(rec2)
{
cout <<"create p2 error!" << endl;
}
pthread_join(p1,NULL);
pthread_join(p2,NULL);
return 0;
}