创建线程

pthread_self函数

获取线程ID。其作用对应进程中 getpid() 函数。

       pthread_t pthread_self(void);      返回值:成功:0;     失败:无!

       线程ID:pthread_t类型,本质:在Linux下为无符号整数(%lu),其他系统中可能是结构体实现

       线程ID是进程内部,识别标志。(两个进程间,线程ID允许相同)

       注意:不应使用全局变量 pthread_t tid,在子线程中通过pthread_create传出参数来获取线程ID,而应使用pthread_self。

pthread_create函数

创建一个新线程。             其作用,对应进程中fork() 函数。

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

       返回值:成功:0;     失败:错误号      -----Linux环境下,所有线程特点,失败均直接返回错误号。

参数:  

       pthread_t:当前Linux中可理解为:typedef  unsigned long int  pthread_t;

参数1:传出参数,保存系统为我们分配好的线程ID

       参数2:通常传NULL,表示使用线程默认属性。若想使用具体属性也可以修改该参数。

       参数3:函数指针,指向线程主函数(线程体),该函数运行结束,则线程结束。

       参数4:线程主函数执行期间所使用的参数。

在一个线程中调用pthread_create()创建新的线程后,当前线程从pthread_create()返回继续往下执行,而新的线程所执行的代码由我们传给pthread_create的函数指针start_routine决定。start_routine函数接收一个参数,是通过pthread_create的arg参数传递给它的,该参数的类型为void *,这个指针按什么类型解释由调用者自己定义。start_routine的返回值类型也是void *,这个指针的含义同样由调用者自己定义。start_routine返回时,这个线程就退出了,其它线程可以调用pthread_join得到start_routine的返回值,类似于父进程调用wait(2)得到子进程的退出状态,稍后详细介绍pthread_join。

pthread_create成功返回后,新创建的线程的id被填写到thread参数所指向的内存单元。我们知道进程id的类型是pid_t,每个进程的id在整个系统中是唯一的,调用getpid(2)可以获得当前进程的id,是一个正整数值。线程id的类型是thread_t,它只在当前进程中保证是唯一的,在不同的系统中thread_t这个类型有不同的实现,它可能是一个整数值,也可能是一个结构体,也可能是一个地址,所以不能简单地当成整数用printf打印,调用pthread_self(3)可以获得当前线程的id。

attr参数表示线程属性,本节不深入讨论线程属性,所有代码例子都传NULL给attr参数,表示线程属性取缺省值,感兴趣的读者可以参考APUE。

【练习】:创建一个新线程,打印线程ID。注意:链接线程库 -lpthread                                            【pthrd_crt.c】

由于pthread_create的错误码不保存在errno中,因此不能直接用perror(3)打印错误信息,可以先用strerror(3)把错误码转换成错误信息再打印。如果任意一个线程调用了exit或_exit,则整个进程的所有线程都终止,由于从main函数return也相当于调用exit,为了防止新创建的线程还没有得到执行就终止,我们在main函数return之前延时1秒,这只是一种权宜之计,即使主线程等待1秒,内核也不一定会调度新创建的线程执行,下一节我们会看到更好的办法。

【练习】:循环创建多个线程,每个线程打印自己是第几个被创建的线程。(类似于进程循环创建子进程)                                                                                                                                                          【more_pthrd.c】

拓展思考:将pthread_create函数参4修改为(void *)&i, 将线程主函数内改为 i=*((int *)arg) 是否可以?

/***
pthread_create.c
***/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
void *thrd_func(void *arg)
{
    printf("In thread : thread id = %lu,pid = %u
",pthread_self(),getpid());
    return NULL;
    
}

int main()
{
    pthread_t tid;
    int ret;

    printf("In main 1 : thread id = %lu,pid = %ui
",pthread_self(),getpid());

    ret = pthread_create(&tid,NULL,thrd_func,NULL);
    if(0 != ret)
    {
        fprintf(stderr,"pthread_create error:%s
",strerror(ret));
        exit(1);    
    }

    sleep(1);
    printf("In main 2 : thread id = %lu,pid = %u
",pthread_self(),getpid());
    
    return 0;
}

运行结果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190814$ ./pthread_create

In main 1 : thread id = 140573795596032,pid = 3648i

In thread : thread id = 140573787256576,pid = 3648

In main 2 : thread id = 140573795596032,pid = 3648

循环创建多个子线程
/***
mul_pthread.c
***/
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>

void *thrd_func(void *arg)
{
    int i = (int)arg;
    sleep(i);
    printf("%dth thread: thread id = %lu,pid = %u
",i+1,pthread_self(),getpid());
    return NULL;
}

int main()
{
    pthread_t tid;
    int ret,i;

    for(i = 0; i < 5; i++)
    {
        ret = pthread_create(&tid,NULL,thrd_func,(void *)i);
        if(0 != ret)
        {
            fprintf(stderr,"pthrea_create error:%s
",strerror(ret));
            exit(1);
        }
    }

    sleep(i);
    return 0;
}

运行结果:

ubuntu1604@ubuntu:~/wangqinghe/linux/20190814$ ./mul_pthread

1th thread: thread id = 140132717160192,pid = 4026

2th thread: thread id = 140132708767488,pid = 4026

3th thread: thread id = 140132700374784,pid = 4026

4th thread: thread id = 140132691982080,pid = 4026

5th thread: thread id = 140132683589376,pid = 4026

原文地址:https://www.cnblogs.com/wanghao-boke/p/11376360.html