pthread

阅读更多
#include 
#include 
#include 
using namespace std;

void *thread1(void *)
{
    for(int i=0;i<5;i++)
    {
        cout << i << endl;
        usleep(1000*1000);
    }
    //回收资源
    pthread_detach(pthread_self());
    return NULL;
}

void *thread2(void *)
{
    cout << "thread2" << endl;
    pthread_detach(pthread_self());
    return NULL;
}

int main()
{
    char hello[32] = "hello world";
    int i = 0;
    pthread_t pid1,pid2;
    pthread_create(&pid1, NULL, thread1, hello);
    //等待thread1结束
    pthread_join(pid1, (void **)&i);
    cout << "thread1 finished" << endl;
    pthread_create(&pid2,NULL,thread2,&i);
}


0
1
2
3
4
thread1 finished

你可能感兴趣的:(pthread)