前言
大家好吖,欢迎来到 YY 滴Linux系列 ,热烈欢迎! 本章主要内容面向接触过C++的老铁
主要内容含:
欢迎订阅 YY滴C++专栏!更多干货持续更新!以下是传送门!
- YY的《C++》专栏
- YY的《C++11》专栏
- YY的《Linux》专栏
- YY的《数据结构》专栏
- YY的《C语言基础》专栏
- YY的《初学者易错点》专栏
- YY的《小小知识点》专栏
- YY的《单片机期末速过》专栏
- YY的《C++期末速过》专栏
- YY的《单片机》专栏
- YY的《STM32》专栏
- YY的《数据库》专栏
- YY的《数据库原理》专栏
目录
- 一.POSIX线程库
- 二.线程控制
- 1.pthread_t是什么类型
- 2.创建线程:pthread_create
- 【1】基本语法
- 【2】示例演示
- 3.线程终止:pthread_exit&pthread_cancel
- 【1】基本语法
- 【2】示例演示
- 4.线程等待:pthread_join
- 【1】为什么要进行线程等待
- 【2】基本语法
- 【3】示例演示
- 5.线程分离:pthread_detach
- 【1】为什么要进行线程分离&线程joinable状态与分离状态
- 【2】基本语法
- 【3】示例演示
一.POSIX线程库
- 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以“pthread_”打头的
- 要使用这些函数库,要通过引入头文件
include <pthread.h>
- 链接这些线程函数库时要使用编译器命令的“
-lpthread
”选项
gcc test.c -o test.o -lpthread
二.线程控制
1.pthread_t是什么类型
- pthread_t 的类型取决于实现。对于Linux目前实现的NPTL实现而言,pthread_t类型的线程ID,本质
就是一个进程地址空间上的一个地址。
2.创建线程:pthread_create
【1】基本语法
功能:创建一个新的线程
原型int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
参数thread:返回线程IDattr:设置线程的属性,attr为NULL表示使用默认属性start_routine:是个函数地址,线程启动后要执行的函数arg:传给线程启动函数的参数
返回值:成功返回0;失败返回错误码
【2】示例演示
- 创建完后,新线程执行rout函数去了,传入参数是NULL;
- 主线程继续往下跑
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
void *rout(void *arg) {int i;for( ; ; ) {printf("I'am thread 1\n");sleep(1);}
}
int main( void )
{pthread_t tid;int ret;if ( (ret=pthread_create(&tid, NULL, rout, NULL)) != 0 ) {fprintf(stderr, "pthread_create : %s\n", strerror(ret));exit(EXIT_FAILURE);}int i;for(; ; ) {printf("I'am main thread\n");sleep(1);}
}
3.线程终止:pthread_exit&pthread_cancel
【1】基本语法
pthread_exit
功能:线程终止
原型void pthread_exit(void *value_ptr);
参数value_ptr:value_ptr不要指向一个局部变量。
返回值:无返回值,跟进程一样,线程结束的时候无法返回到它的调用者(自身)pthread_cancel
功能:取消一个执行中的线程
原型int pthread_cancel(pthread_t thread);
参数thread:线程ID
返回值:成功返回0;失败返回错误码
【2】示例演示
// 线程函数
void* thread_function(void* arg) { int num_seconds = *((int*)arg); printf("Thread is running. Will sleep for %d seconds.\n", num_seconds); sleep(num_seconds); // 休眠一段时间以模拟工作 printf("Thread has finished execution.\n"); pthread_exit(NULL); // 退出线程
} int main() { pthread_t thread; int num_seconds = 5; int* arg = &num_seconds; // 创建线程 if (pthread_create(&thread, NULL, thread_function, (void*)arg) != 0) { perror("Failed to create thread"); exit(EXIT_FAILURE); } // 等待线程完成 if (pthread_join(thread, NULL) != 0) { perror("Failed to join thread"); exit(EXIT_FAILURE); } printf("Main thread has finished.\n"); return 0;
}
4.线程等待:pthread_join
【1】为什么要进行线程等待
- 为什么需要线程等待?——主线程等待其他线程
- 已经退出的线程,其空间没有被释放,仍然在进程的地址空间内。
- 创建新的线程不会复用刚才退出线程的地址空间
【2】基本语法
功能:等待线程结束
原型
int pthread_join(pthread_t thread, void **value_ptr);
参数thread:线程IDvalue_ptr:它指向一个指针,后者指向线程的返回值(没有返回值就填NULL)
返回值:成功返回0;失败返回错误码
【3】示例演示
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>void *thread1( void *arg )
{printf("thread 1 returning ... \n");int *p = (int*)malloc(sizeof(int));*p = 1;return (void*)p;
}int main( void )
{pthread_t tid;void *ret;pthread_create(&tid, NULL, thread1, NULL);pthread_join(tid, &ret);// pthread_join(tid, NULL);没有返回值就填NULLprintf("thread return, thread id %X, return code:%d\n", tid, *(int*)ret);free(ret);return 0;
}
5.线程分离:pthread_detach
【1】为什么要进行线程分离&线程joinable状态与分离状态
- 默认情况下,新创建的线程是 joinable 的,线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成系统泄漏。
- 如果不关心线程的返回值,join是一种负担,这个时候,我们可以告诉系统,当线程退出时, 自动释放线程资源 ,此时就是 分离状态
注意:
- joinable和分离是冲突的,一个线程不能既是joinable又是分离的
【2】基本语法
-- 线程组内其他线程对目标线程进行分离
int pthread_detach(pthread_t thread);
-- 线程内自己分离
pthread_detach(pthread_self());
【3】示例演示
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
void *thread_run( void * arg )
{pthread_detach(pthread_self());//线程内自己分离,进入分离状态printf("%s\n", (char*)arg);return NULL;
}
int main( void )
{pthread_t tid;if ( pthread_create(&tid, NULL, thread_run, "thread1 run...") != 0 ) {printf("create thread error\n");return 1;}//不需要调用 pthread_joinint ret = 0;return ret;
}