UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程
来源:互联网 发布:手机身份证合成软件 编辑:程序博客网 时间:2024/04/20 02:33
在进程控制三部曲中我们学习了进程的创建、终止以及获取终止状态等的进程控制原语。线程的控制与进程的控制有相似之处,在表 1中我们列出了进程和线程相对应的控制原语。
1 线程
每个线程都有一个线程 ID,线程只在它所属的进程环境中有效。线程ID 使用pthread_t 表示。可以通过调用pthread_self 函数获取线程自身的线程 ID,
#include <pthread.h>
pthread_t pthread_self(void);
返回值:调用线程的线程ID
线程 ID 不一定是一个非负整数,也有可能是一个结构体。所以,要对比两个线程是否相同,必须使用pthread_equal 函数来进行,
#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2);
返回值:若相等则返回非0值,否则返回0
2 线程的创建
可以通过调用pthread_create 来创建新的线程,
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
返回值:若成功则返回0,否则返回错误编码
其中,参数 tidp 用于返回成功创建的新线程的线程 ID。参数 attr 用于定制各种不同的线程属性,如果设置为 NULL,表示创建一个默认属性的线程。成功创建的新线程将从参数start_rtn 所指向的函数开始执行,该函数接收一个 void * 的参数 arg,并返回一个 void * 的返回值。
注意:pthread_create 失败的时候返回的是错误编码,表示我们可以通过调用 strerror 函数来获取具体的错误信息。
下面我们来看一个创建新进程的例子,
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <pthread.h>void *my_thread(void *arg){ printf("in new thread. tid: %u\n", (unsigned int)pthread_self()); return ((void *)0);}intmain(void){ int err; pthread_t tid; err = pthread_create(&tid, NULL, my_thread, NULL); if ( err != 0) { printf("can't create thread: %s\n", strerror(err)); exit(-1); } printf("in main thread: %u\n", (unsigned int)pthread_self()); sleep(1); exit(0);}
编译该程序,生成并执行文件pthread_create_demo。从下面的运行结果,我们可以看到在 main 函数和my_thread 函数打印的线程 ID 是不一样的。
lienhua34:demo$ gcc -o pthread_create_demo -pthread pthread_create_demo.clienhua34:demo$ ./pthread_create_demoin main thread: 3076478720in new thread. tid: 3076475712
3 线程终止
如果进程中的任一线程调用了exit、_Exit 或_exit 函数,那么整个进程都将会终止。那如果我们只是想单独的终止一个线程呢?有三种方式,
1. 线程从启动例程中返回,返回值即为线程的退出码。
2. 被同一个进程中的其他线程取消。
3. 线程调用函数pthread_exit。
pthread_exit 函数的声明如下,
#include <pthread.h>
void pthread_exit(void *rval_ptr);
进程中其它线程可以通过调用pthread_join 来获取指定线程的退出码。
#include <pthread.h>
int pthread_join(pthread_t tid, void **rval_ptr);
返回值:若成功则返回0,否则返回错误编号
pthread_join 函数会使调用线程一直阻塞,直到指定的线程终止。如果指定线程只是从它的启动例程中返回,rval_ptr 将包含返回码。如果线程被取消,由rval_ptr 指定的内存单元将被置为PTHREAD_CANCELED。
下面我们来看一个线程终止的例子,
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <pthread.h>void *thr_fn1(void *arg){ printf("in thread 1\n"); return ((void *)1);}void *thr_fn2(void *arg){ printf("in thread 2\n"); pthread_exit((void *)2);}intmain(void){ int err; pthread_t tid1, tid2; void *tret; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if ( err != 0) { printf("can't create thread 1: %s\n", strerror(err)); exit(-1); } err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != 0) { printf("can't create thread 2: %s\n", strerror(err)); exit(-1); } err = pthread_join(tid1, &tret); if (err != 0) { printf("can't join with thread 1: %s\n", strerror(err)); exit(-1); } printf("thread 1 exit code: %d\n", (int)tret); err = pthread_join(tid2, &tret); if (err != 0) { printf("can't join with thread 2: %s\n", strerror(err)); exit(-1); } printf("thread 2 exit code: %d\n", (int)tret); exit(0);}
编译该程序,生成并执行文件pthread_exit_demo,
lienhua34:demo$ gcc -o pthread_exit_demo -pthread pthread_exit_demo.clienhua34:demo$ ./pthread_exit_demoin thread 1in thread 2thread 1 exit code: 1thread 2 exit code: 2
(done)
- UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程
- UNIX环境编程学习笔记(27)——多线程编程(二):控制线程属性
- UNIX环境编程学习笔记(28)——多线程编程(三):线程的取消
- 《UNIX环境高级编程》笔记--线程的标识、创建和终止
- 《unix高级环境编程》线程——线程终止
- 《unix高级环境编程》线程——线程终止
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- 多线程编程-线程的创建和终止
- UNIX环境编程学习笔记(15)——进程管理之进程终止
- unix 环境高级编程 线程一 创建
- Posix多线程编程学习笔记(一)—线程基础
- UNIX环境编程学习笔记(21)——进程管理之获取进程终止状态的 wait 和 waitpid 函数
- UNIX环境编程学习笔记(21)——进程管理之获取进程终止状态的 wait 和 waitpid 函数
- Unity3D 攻击范围检测
- 微信小程序实例源码大全
- php authcode 混淆加密和解密
- 手动触发点击事件
- 过滤器(Filter)和拦截器(Interceptor)的区别
- UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程
- Spring boot 学习笔记(三)
- vim: vs sp 调整窗口高度和宽度
- Remote mongo
- mysql数据库的增、删、改、查(二)---查
- 在Xcode8.3中搭建python开发环境
- FreeRTOS系列第1篇---为什么选择FreeRTOS?
- 2017ssm整合入门示例
- 跟我学做另类图表