2025年3月26日 星期三 甲辰(龙)年 月廿五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 编程箴言

创建线程

时间:01-23来源:作者:点击数:232

前面章节中,我们通过调用 pthread_create() 函数成功创建了多个线程,本节就给大家详细讲解 pthread_create() 函数的用法。

pthread_create() 函数声明在<pthread.h>头文件中,语法格式如下:

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

各个参数的含义是:

1) pthread_t *thread:传递一个 pthread_t 类型的指针变量,也可以直接传递某个 pthread_t 类型变量的地址。pthread_t 是一种用于表示线程的数据类型,每一个 pthread_t 类型的变量都可以表示一个线程。

2) const pthread_attr_t *attr:用于手动设置新建线程的属性,例如线程的调用策略、线程所能使用的栈内存的大小等。大部分场景中,我们都不需要手动修改线程的属性,将 attr 参数赋值为 NULL,pthread_create() 函数会采用系统默认的属性值创建线程。

pthread_attr_t 类型以结构体的形式定义在<pthread.h>头文件中,此类型的变量专门表示线程的属性。关于线程属性,您可以阅读《Linux线程属性详解》一文做详细地了解。

3) void *(*start_routine) (void *):以函数指针的方式指明新建线程需要执行的函数,该函数的参数最多有 1 个(可以省略不写),形参和返回值的类型都必须为 void* 类型。void* 类型又称空指针类型,表明指针所指数据的类型是未知的。使用此类型指针时,我们通常需要先对其进行强制类型转换,然后才能正常访问指针指向的数据。

如果该函数有返回值,则线程执行完函数后,函数的返回值可以由 pthread_join() 函数接收。有关 phtread_join() 函数的用法,我们会在《获取线程函数的返回值》一节给大家做详细讲解。

4) void *arg:指定传递给 start_routine 函数的实参,当不需要传递任何数据时,将 arg 赋值为 NULL 即可。

如果成功创建线程,pthread_create() 函数返回数字 0,反之返回非零值。各个非零值都对应着不同的宏,指明创建失败的原因,常见的宏有以下几种:

  • EAGAIN:系统资源不足,无法提供创建线程所需的资源。
  • EINVAL:传递给 pthread_create() 函数的 attr 参数无效。
  • EPERM:传递给 pthread_create() 函数的 attr 参数中,某些属性的设置为非法操作,程序没有相关的设置权限。

以上这些宏都声明在 <errno.h> 头文件中,如果程序中想使用这些宏,需提前引入此头文件。

接下来通过一个样例,给大家演示 pthread_create() 函数的用法:

  • #include <stdio.h>
  • #include <unistd.h> //调用 sleep() 函数
  • #include <pthread.h> //调用 pthread_create() 函数
  • void *ThreadFun(void *arg)
  • {
  • if (arg == NULL) {
  • printf("arg is NULL\n");
  • }
  • else {
  • printf("%s\n", (char*)arg);
  • }
  • return NULL;
  • }
  • int main()
  • {
  • int res;
  • char * url = "https://www.cdsy.xyz";
  • //定义两个表示线程的变量(标识符)
  • pthread_t myThread1,myThread2;
  • //创建 myThread1 线程
  • res = pthread_create(&myThread1, NULL, ThreadFun, NULL);
  • if (res != 0) {
  • printf("线程创建失败");
  • return 0;
  • }
  • sleep(5); //令主线程等到 myThread1 线程执行完成
  • //创建 myThread2 线程
  • res = pthread_create(&myThread2, NULL, ThreadFun,(void*)url);
  • if (res != 0) {
  • printf("线程创建失败");
  • return 0;
  • }
  • sleep(5); // 令主线程等到 mythread2 线程执行完成
  • return 0;
  • }

假设程序编写在 thread.c 文件中,执行过程如下:

[root@localhost ~]# gcc thread.c -o thread.exe -lpthread
[root@localhost ~]# ./thread.exe
arg is NULL
https://www.cdsy.xyz

程序中共创建了 2 个线程,分别命名为 myThread1 和 myThread2。myThread1 和 myThread2 线程执行的都是 threadFun() 函数,不同之处在于,myThread1 线程没有给 threadFun() 函数传递任何数据,而 myThread2 线程向 threadFun() 函数传递了 "http://www.biancheng.net" 这个字符串。

从程序的执行过程不难看出, pthread_create() 函数成功创建的线程会自动执行指定的函数,不需要手动开启。此外,为了确保创建的线程能在主线程之前执行完,程序中调用 sleep() 函数延缓了主线程的执行速度。

您可以尝试将程序中的 sleep() 函数全部注释掉,然后重新编译、执行此程序。整个进程会随着主线程执行结束而立即终止,由于主线程执行太快,子线程可能尚未执行完就被强制终止。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门