线程的资源
在同一个进程中,大部分资源都是所有线程所共享的,比如:
可执行程序的代码
程序的全局内存
堆内存
栈
文件描述符
但是线程还有一些独有的资源,包含:
线程 ID
线程自己的一套寄存器值
线程运行栈
调度优先级和策略
信号屏蔽字
errno 变量
线程私有数据
进程与线程的区别根本区别:
进程是操作系统资源分配的基本单位
线程是任务调度和执行的基本单位
在开销方面:
每个进程都有独立的代码和数据空间(程序上下文),程序之间的切换会有较大的开销;
线程可以看做轻量级的进程,同一类线程共享代码和数据空间,每个线程都有自己独立的运行栈和程序计数器(PC),线程之间切换的开销小。
所处环境:
在操作系统中能同时运行多个进程(程序);
而在同一个进程(程序)中有多个线程同时执行(通过CPU调度,在每个时间片中只有一个线程执行)
内存分配方面:
系统在运行的时候会为每个进程分配不同的内存空间;
而对线程而言,除了CPU外,系统不会为线程分配内存(线程所使用的资源来自其所属进程的资源),线程组之间只能共享资源。
包含关系:没有线程的进程可以看做是单线程的,如果一个进程内有多个线程,则执行过程不是一条线的,而是多条线(线程)共同完成的;
线程是进程的一部分,所以线程也被称为轻权进程或者轻量级进程。
pthread 线程库
pthread 是 POSIX.1 中定义的一套线程函数接口,所以这种线程也被称为 POSIX 线程。在我们后面的学习中,都会使用 pthread 线程库来操作线程,包括线程的创建、终止、互斥与同步等等。
要看你的的系统是否支持 POSIX 线程,可以判断宏 _POSIX_THREADS 是否被定义。
线程创建
函数原型:
typedef void *(*start_routine) (void *);
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, start_routine th_fn, void *arg);
参数1:
pthread_t 类型是线程 id 的类型,它可能是个整数(大多数是这样),也可能是结构体,这取决于具体实现。
thread 参数是一个传出参数,它主要用来接收被创建的线程的 id 号。在线程执行过程中调用函数pthread_self函数,可以获取到自己的线程 id 号。
参数2:
pthread_attr_t 类型是用来保存线程属性的结构体
参数 attr 可以传 NULL,表示创建具有默认属性的线程。有关线程的属性结构体,后文会作具体讲解。
参数3:
start_routine 是用来声明函数型如 void* th_fn(void* arg) 这样的函数的,它是线程过程函数。线程被创建后,一旦被调度,就会进入到函数 th_fn 中执行。
参数4:
最后一个参数 arg,是线程过程函数 th_fn 的参数。
返回值:
pthread_create 函数的返回值是一个错误码,返回 0 表示成功。和其它 POSIX 函数不同的是,pthread_create 函数执行错误并不会设置 errno 变量。
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
// 线程入口函数
void* th_fn(void *arg) {
char* name = (char*)arg;
int i;
for (i = 1; i <= 10; ++i) {
printf("%s : %d\n", name, i);
// 判断是 张三 还是 李四
if (strcmp("张三", name) == 0){
usleep(1000*10); // 10 ms
}
else{
usleep(1000*5); // 5ms
}
}
return NULL;
}
int main() {
int err;
pthread_t p1, p2;
char *name1 = "张三";
char *name2 = "李四";
// 创建张三线程。
err = pthread_create(&p1, NULL, th_fn, (void*)name1);
if (err != 0) {
perror("pthread_create1 error\n");
return -1;
}
// 创建李四线程
err = pthread_create(&p2, NULL, th_fn, (void*)name2);
if (err != 0) {
perror("pthread_create2 error\n");
return -1;
}
//如果没有此行,线程效果无法显示
sleep(3);
// 打印线程 id 号
printf("张三 id: %lx\n", p1);
printf("李四 id: %lx\n", p2);
return 0;
}
编程和运行
// 注意需要链接 pthread 库
gcc thread.c -o thread -lpthread
./hellothread
线程终止
线程终止方式线程的终止包括两大类,主动终止和被动终止。
主动终止
线程过程函数执行 return 正常返回,返回值是线程的退出码
线种过程函数执行 pthread_exit 函数退出,其参数是线程退出码
被动终止
在其它线程中调用 pthread_cancel 函数
任意线程调用 exit、_Exit 或者 _exit 导致整个进程终止
pthread_exit 函数
pthread_exit 函数原型
void pthread_exit(void *rval_ptr);
该函数的参数是线程的退出码,它是无类型的指针。通过函数 pthread_join 可以获取到这个指针的值,它可以回收线程所使用的资源。
int pthread_join(pthread_t tid, void **rval_ptr);
调用pthread_join 会阻塞当前线程,直到指定的线程 tid 返回(return)、执行 pthread_exit 或者被其它线程取消(pthread_cancel)。rval_ptr 是一个输出参数,它用来接收线程退出码(是一个 void* 类型)。
pthread_cancel 函数
int pthread_cancel(pthread_t tid);
默认情况下,该函数的效果相当于在线程 tid 中调用 pthread_exit((void*)PTHREAD_CANCEL) 函数。
线程主动退出实例:
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
void* th_fn1(void* arg) {
puts("thread 1 returning");
return (void*)10;
}
void* th_fn2(void* arg) {
puts("thread 2 exiting");
pthread_exit((void*)20);
puts("thread 2 exited");
}
int main() {
pthread_t tid1, tid2;
int err;
void* ret;
err = pthread_create(&tid1, NULL, th_fn1, NULL);
if (err != 0) {
perror("pthread_create1 error\n");
return -1;
}
err = pthread_create(&tid2, NULL, th_fn2, NULL);
if (err != 0) {
perror("pthread_create2 error\n");
return -1;
}
sleep(2);
// 主线程阻塞直到线程 tid1 退出
err = pthread_join(tid1, &ret);
if (err != 0) {
perror("pthread_join1 error\n");
return -1;
}
printf("thread 1 exit code %d\n", (int)ret);
// 主线程阻塞直到线程 tid2 退出
err = pthread_join(tid2, &ret);
if (err != 0) {
perror("pthread_join2 error\n");
return -1;
}
printf("thread 2 exit code %d\n", (int)ret);
return 0;
}
被动终止实例:
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
void* th_fn1(void* arg) {
while(1) {
puts("thread 1 running");
sleep(1);
}
return (void*)10;
}
void* th_fn2(void* arg) {
while(1) {
puts("thread 2 running");
sleep(1);
}
pthread_exit((void*)20);
}
int main() {
pthread_t tid1, tid2;
int err;
void* ret;
err = pthread_create(&tid1, NULL, th_fn1, NULL);
if (err != 0) {
perror("pthread_create1 error\n");
return -1;
}
err = pthread_create(&tid2, NULL, th_fn2, NULL);
if (err != 0) {
perror("pthread_create2 error\n");
return -1;
}
sleep(5);
// 通知 tid1 和 tid2 退出。
pthread_cancel(tid1);
pthread_cancel(tid2);
err = pthread_join(tid1, &ret);
// 线程退出码都是 PTHREAD_CANCELED
printf("PTHREAD_CANCELED = %d\n", (int)PTHREAD_CANCELED);
if (err != 0) {
perror("pthread_join1 error\n");
return -1;
}
printf("thread 1 exit code %d\n", (int)ret);
err = pthread_join(tid2, &ret);
if (err != 0) {
perror("pthread_join2 error\n");
return -1;
}
printf("thread 2 exit code %d\n", (int)ret);
return 0;
}