在 C++ 的多线程编程中,pthread 库提供了强盛的功能来管理线程。此中,线程分离和线程属性是两个重要的概念,它们对于优化线程的行为和资源管理有着关键作用。
线程分离
1.1 什么是线程分离
在 pthread 库中,线程有两种状态:可联合(joinable)和分离(detached)。默认情况下,新创建的线程是可联合的。可联合的线程在结束时,需要其他线程调用pthread_join函数来回收其资源,否则会造成资源泄漏。而分离的线程则会在结束时自动释放资源,无需其他线程来回收。
1.2 为什么要使用线程分离
在某些场景下,我们创建的线程执行完使命后,不需要获取其返回值,也不需要关心它的结束状态,此时将线程设置为分离状态可以制止资源浪费和不必要的同步开销。例如,在一个服务器程序中,为每个客户端连接创建一个线程来处理请求,这些线程处理完请求后就可以自动释放资源,不需要主线程逐个等待它们结束。
1.3 如何实现线程分离
在 pthread 库中,可以使用pthread_detach函数来将一个线程设置为分离状态。该函数的原型如下:
- int pthread_detach(pthread_t thread);
复制代码 thread是要设置为分离状态的线程标识符。
以下是一个简单的 C++ 代码示例,展示了如何创建一个分离的线程:
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- // 线程函数
- void* thread_function(void* arg) {
- sleep(2);
- std::cout << "This is a detached thread." << std::endl;
- return nullptr;
- }
- int main() {
- pthread_t thread;
- // 创建线程
- if (pthread_create(&thread, nullptr, thread_function, nullptr)!= 0) {
- std::cerr << "Failed to create thread" << std::endl;
- return 1;
- }
- // 将线程设置为分离状态
- if (pthread_detach(thread)!= 0) {
- std::cerr << "Failed to detach thread" << std::endl;
- return 1;
- }
- std::cout << "Detached thread created successfully." << std::endl;
- // 主线程继续执行其他任务
- sleep(3);
- return 0;
- }
复制代码 创建了一个线程,并使用pthread_detach将其设置为分离状态。主线程在创建和分离线程后,继续执行其他使命,而分离的线程在完成使命后会自动释放资源,其他线程不能再使用 pthread_join 来等待它结束。
pthread_detach 可以在主函数调用,也可以在thread_function函数调用
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- using namespace std;
- int j = 2;
- void* pthread_fun(void *arg)
- {
-
- //自己设置为detached状态
- int ret = pthread_detach(pthread_self());
- while(j--)
- {
- cout << " in pthread_task" << endl;
- cout << *(int*)arg << endl;
- sleep(1);
- }
- // 线程返回一个整数值
- int* result = new int(42);
- return static_cast<void*>(result);//pthread_exit(reinterpret_cast<void*>(result));
- }
- int main()
- {
- int ret = 0;
- pthread_t tid = 0;
- int argsend = 99;
- int i = 3;
- ret = pthread_create(&tid, NULL, pthread_fun, &argsend);
- if(ret != 0)
- {
- cout << " pthread_create error" << endl;
- return -1;
- }
- while(i--)
- {
- cout << "pthread _create success" << endl;
- sleep(2);
- }
-
-
- return 0;
- }
复制代码 线程属性
2.1 线程属性概述
线程属性定义了线程的一些特性,如栈大小、调理计谋、优先级等。通过设置线程属性,可以根据具体的应用需求来优化线程的行为。在 pthread 库中,使用pthread_attr_t类型来表示线程属性。
2.2 常见的线程属性
栈大小:线程的栈用于存储局部变量、函数调用信息等。可以通过pthread_attr_setstacksize函数来设置线程的栈大小。
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- size_t stack_size = 1024 * 1024; // 1MB栈大小
- pthread_attr_setstacksize(&attr, stack_size);
复制代码 调理计谋:pthread 库支持多种调理计谋,如 SCHED_OTHER(普通调理计谋)、SCHED_FIFO(先进先出调理计谋)、SCHED_RR(时间片轮转调理计谋)。可以使用pthread_attr_setschedpolicy函数来设置调理计谋。
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setschedpolicy(&attr, SCHED_RR);
复制代码 优先级:线程的优先级决定了它在 CPU 竞争中的优先级。可以通过pthread_attr_setschedparam函数来设置线程的优先级。
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- struct sched_param param;
- param.sched_priority = 50; // 设置优先级
- pthread_attr_setschedparam(&attr, ¶m);
复制代码 2.3 使用线程属性创建线程
在创建线程时,可以将设置好的线程属性作为参数传递给pthread_create函数。
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- // 线程函数
- void* thread_function(void* arg) {
- std::cout << "Thread is running." << std::endl;
- return nullptr;
- }
- int main() {
- pthread_t thread;
- pthread_attr_t attr;
- // 初始化线程属性
- pthread_attr_init(&attr);
- // 设置栈大小为2MB
- size_t stack_size = 2 * 1024 * 1024;
- pthread_attr_setstacksize(&attr, stack_size);
- // 设置调度策略为时间片轮转
- pthread_attr_setschedpolicy(&attr, SCHED_RR);
- // 设置优先级
- struct sched_param param;
- param.sched_priority = 50;
- pthread_attr_setschedparam(&attr, ¶m);
- // 使用设置好的属性创建线程
- if (pthread_create(&thread, &attr, thread_function, nullptr)!= 0) {
- std::cerr << "Failed to create thread" << std::endl;
- return 1;
- }
- // 等待线程结束
- if (pthread_join(thread, nullptr)!= 0) {
- std::cerr << "Failed to join thread" << std::endl;
- return 1;
- }
- // 销毁线程属性
- pthread_attr_destroy(&attr);
- return 0;
- }
复制代码 首先初始化了线程属性,然后设置了栈大小、调理计谋和优先级,末了使用这些属性创建了一个线程
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- using namespace std;
- int j = 2;
- void* pthread_fun(void *arg)
- {
-
- //自己设置为detached状态
- //int ret = pthread_detach(pthread_self());
- while(j--)
- {
- cout << " in pthread_task" << endl;
- cout << *(int*)arg << endl;
- sleep(1);
- }
- // 线程返回一个整数值
- int* result = new int(42);
- return static_cast<void*>(result);//pthread_exit(reinterpret_cast<void*>(result));
- }
- int main()
- {
- int ret = 0;
- pthread_t tid = 0;
- int argsend = 99;
- int i = 3;
- pthread_attr_t attr;
- // 初始化线程属性
- pthread_attr_init(&attr);
- ret = pthread_create(&tid, NULL, pthread_fun, &argsend);
- if(ret != 0)
- {
- cout << " pthread_create error" << endl;
- return -1;
- }
- sched_param param;
- // 获取线程属性中的调度参数
- if (pthread_attr_getschedparam(&attr, ¶m) != 0) {
- std::cerr << "Failed to get scheduling parameters." << std::endl;
- return 1;
- }
- // 打印调度参数中的优先级
- std::cout << "Scheduling priority: " << param.sched_priority << std::endl;
- // 设置线程属性的分离状态为分离
- ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (ret != 0) {
- cerr << "Failed to set detach state." << endl;
- return -1;
- }
- while(i--)
- {
- cout << "pthread _create success" << endl;
- sleep(2);
- }
- int detachstate;
- // 获取线程属性的分离状态
- if (pthread_attr_getdetachstate(&attr, &detachstate) != 0) {
- std::cerr << "Failed to get detach state." << std::endl;
- // 销毁线程属性对象
- pthread_attr_destroy(&attr);
- return 1;
- }
- // 根据分离状态输出相应信息
- if (detachstate == PTHREAD_CREATE_JOINABLE) {
- std::cout << "Thread is joinable." << std::endl;
- } else if (detachstate == PTHREAD_CREATE_DETACHED) {
- std::cout << "Thread is detached." << std::endl;
- } else {
- std::cout << "Unknown detach state." << std::endl;
- }
-
- // 销毁线程属性
- pthread_attr_destroy(&attr);
- return 0;
- }
复制代码 pthread_attr_getdetachstate(&attr, &detachstate) 用于获取线程属性对象 attr 中设置的分离状态。它查询的是线程属性对象的配置信息,而不是pthread_detach线程现实运行时的分离状态。
线程分离和线程属性是 pthread 库中非常实用的功能。通过公道使用线程分离,可以制止资源浪费和同步开销;而机动设置线程属性,则可以根据应用需求优化线程的行为。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |