pthread 之thread[线程]

来源:互联网 发布:淘宝店铺实战宝典pdf 编辑:程序博客网 时间:2024/05/21 21:46

线程有三个操作
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;}
原创粉丝点击