pthread_create()函数样例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xh_xinhua/article/details/72467626
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void* client_pthread(void* arg)
{
    printf("call client_pthread()\n");
    while(1)
    {   
        sleep(3);
        printf("call client_pthread()\n");
    }
}
void* client_pthread_two(void* date_two)
{
    printf("call client_pthread_two()\n");
    while(1)
    {   
        sleep(3);
        printf("call client_pthread_two()\n");
    }
}
void test_pthread_client(void)
{
    int ret = 0;
    pthread_t pthread_id;
    pthread_t pthread_id_two;
    printf("call test_pthread_client()\n");
    ret = pthread_create(&pthread_id, NULL, client_pthread, NULL);
    if(0 != ret)
    {
        printf("create pthread false\n");
        return;
    }
    ret = pthread_create(&pthread_id_two, NULL, client_pthread_two, NULL);
    if(0 != ret)
    {
        printf("create pthread false\n");
        return;
    }
    pthread_join(pthread_id,NULL);  //加入pthread_join后,主线程会一直等待直到等待的线程结束自己才结束,使创建的线程有机会执行。
    pthread_join(pthread_id_two,NULL);  
}

int main()
{
    printf("call main()\n");
    test_pthread_client();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xh_xinhua/article/details/72467626