当前位置: 首页 > news >正文

pthread_create函数

函数原型

pthread_create 是 POSIX 线程(pthread)库中的一个函数,用于在程序中创建一个新线程。

#include <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_create 会将其设置为新线程的标识符。
  2. const pthread_attr_t *attr: 这是一个指向 pthread_attr_t 结构的指针,用于指定线程的属性。如果设置为 NULL,则使用默认属性。常见的属性包括线程的调度策略、优先级、堆栈大小等。
  3. void *(*start_routine) (void *): 这是一个指向线程函数的指针。这个函数是新线程启动后要执行的函数。它的返回类型是 void*,并接受一个 void* 类型的参数。
  4. void *arg: 这是传递给线程函数的参数。该参数的类型是 void*,因此可以传递任何类型的数据,只需在线程函数中进行适当的类型转换。

返回值

  • 成功时,pthread_create 返回 0
  • 失败时,返回一个非零的错误码。

代码示例 

#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <thread>   //包含C++标准线程头文件
#include <sstream>  //包含字符串流头文件void inputCurPthreadId()
{//通过C++标准库获取当前线程号std::thread::id threadID = std::this_thread::get_id();//将 std::thread::id 转换为一种 QDebug 能够处理的类型std::ostringstream oss;oss << threadID;std::string threadIDStr = oss.str();qDebug() << "[C++] This Thread Id = " << QString::fromStdString(threadIDStr);//Qt QThread类获取当前线程qDebug() << "[Qt] This Thread = " << QThread::currentThread() << "\n";
}// 线程函数
void* threadFunction(void* arg) 
{qDebug() << "In pthread_create:";inputCurPthreadId();int threadNumber = *((int*)arg);std::cout << "Hello from thread " << threadNumber << "!\n";sleep(2); // 模拟一些工作std::cout << "Thread " << threadNumber << " is exiting.\n";pthread_exit(NULL); // 退出线程
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);qDebug() << "before pthread_create:";inputCurPthreadId();pthread_t thread;int threadNumber = 1;// 创建线程if (pthread_create(&thread, NULL, threadFunction, (void*)&threadNumber) != 0) {std::cerr << "Error creating thread\n";return 1;}// 等待线程完成if (pthread_join(thread, NULL) != 0) {std::cerr << "Error joining thread\n";return 2;}qDebug() << "after pthread_create:";inputCurPthreadId();std::cout << "Main thread is exiting.\n";return a.exec();
}

注意事项

  1. 线程同步:多个线程可能会访问共享资源,因此需要使用同步机制(如互斥锁、条件变量等)来避免竞争条件。
  2. 线程属性:如果需要设置特定的线程属性(如堆栈大小、调度策略等),可以使用 pthread_attr_set* 函数族来配置 pthread_attr_t 结构。
  3. 线程退出:线程可以通过调用 pthread_exit 函数来退出,或者在线程函数中返回。主线程在所有其他线程完成之前不应该退出,否则会导致程序异常终止。
  4. 线程清理:使用 pthread_join 函数等待线程完成并回收其资源。如果不需要等待线程完成,可以使用 pthread_detach 函数将线程分离。
http://www.lryc.cn/news/522418.html

相关文章:

  • C# 并发和并行的区别--16
  • Java日志配置
  • python中的RPA->playwright自动化录制脚本实战案例笔记
  • Linux查看日志命令
  • (8)ERC20详细介绍
  • opencv projectPoints函数 computeCorrespondEpilines函数 undistortPoints函数
  • springboot集成websocket实现实时大量数据,效率性能高
  • 游戏引擎学习第80天
  • Windows 上的 MySQL 8.4.3 和 WSL(Ubuntu)的 MySQL 8.0.40 之间配置 主从同步
  • 【狂热算法篇】探秘图论之 Floyd 算法:解锁最短路径的神秘密码(通俗易懂版)
  • Sentinel配置流控规则详解
  • 解锁动态规划的奥秘:从零到精通的创新思维解析(6)
  • Qwen2.5 3B、7B、14B在文本按照规范进行标准化改写任务上的表现
  • Oracle报错ORA-01078、LRM-00109
  • 免费为企业IT规划WSUS:Windows Server 更新服务 (WSUS) 之快速入门教程(一)
  • Titans 架构中的记忆整合:Memory as a Context;Gated Memory;Memory as a Layer
  • 无缝过渡:将 Ansys 子结构模型转换为 Nastran
  • 小哆啦的跳跃挑战:能否突破迷宫的极限?
  • KubeSphere部署安装,接入KubeKey安装的k8s集群
  • Object常用的方法及开发中的使用场景
  • SQL2000在win10上安装的方法
  • Windows图形界面(GUI)-QT-C/C++ - QT 对话窗口
  • Java语言的数据结构
  • 【12】Word:张老师学术论文❗
  • 大疆最新款无人机发布,可照亮百米之外目标
  • 《小迪安全》学习笔记05
  • 56_多级缓存实现
  • 每日进步一点点(网安)
  • 宝塔php7.4安装报错,无法安装,php8以上可以安装,以下的不行,gd库什么的都正常
  • SDL2:PC端编译使用