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

c语言 头文件封装跨平台线程

c语言线程类

第一章 跨平台线程


文章目录

  • c语言线程类
  • 前言
  • 一、如何实现?
  • 二、完整代码
  • 三、使用示例
    • 1、创建线程并等待返回
    • 2、创建线程并分离
  • 总结


前言

c语言的标准库是没有线程类的,不同平台的使用方式都有所不同,Windows上通常用win32 api,其他平台则是pthread。当想要写通用的跨平台代码时,涉及到多线程操作就会很不方便。笔者以前写过跨平台线程类的封装,自定义了一套接口和实现,能使用但是还不是最方便的。比如写项目的时候还是需要在cmakelist中加入.c文件的。本文的做法是进一步简化,只需要一个头文件就能实现跨平台的线程封装。


一、如何实现?

我们其实只需要通过宏定义一套接口就可以了,而不需要自定义线程对象以及函数和实现。
定义一个宏方法在Windows上宏展开是调用win32 api,其他平台则是调用pthread。
如下:

#ifdef _WIN32
#include <windows.h>
/*** @brief 创建线程* @param thread 线程句柄,类型为Thread** @param callback 回调方法,格式:TINT thread_callback(void* arg);  thread_callback中的返回类型是整型int,不同平台长度不同所以只能统一为int。* @param arg 回调参数,类型为void** @return 0成功,其他值失败*/
#define thread_create(thread, callback, arg) (*(thread) = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)callback,arg, 0, NULL))==0
#else
#include <pthread.h>
#define thread_create(tid, callback, arg) pthread_create(tid, NULL, (void*(*)(void*))callback, arg)
#endif

二、完整代码

thread.h

/************************************************************************************************************************* Copyright (C): 2025-2025, codeofcc. All rights reserved.* @file : thread.h* @brief : 通用线程类封装,在Windows上使用win32api,其他平台使用pthread。* @author : codeofcc* @email : * @version : 1.0.0* @date : 2025/6/6 16:34:27
******************************************************************************************************************/
#ifndef THREAD_H
#define THREAD_H
#ifdef _WIN32
#ifndef _WIN32_WINNT
//使用mingw编译时需要此定义。
#define _WIN32_WINNT 0x0600
#endif
#include <windows.h>
#include <synchapi.h>
/// @brief 线程句柄
typedef HANDLE Thread;
/// @brief 线程回调的返回定义,仅作为函数定义,不能作为类型。
#define TINT int WINAPI
/*** @brief 创建线程* @param thread 线程句柄,类型为Thread** @param callback 回调方法,格式:TINT thread_callback(void* arg);  thread_callback中的返回类型是整型int,不同平台长度不同所以只能统一为int。* @param arg 回调参数,类型为void** @return 0成功,其他值失败*/
#define thread_create(thread, callback, arg) (*(thread) = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)callback,arg, 0, NULL))==0
/*** @brief 等待线程结束,结束后会销毁线程句柄* @param thread 线程句柄,类型为Thread* @param ret 返回值,类型未int** @return 0成功,其他值失败*/
#define thread_join(thread,ret) WaitForSingleObject(thread, INFINITE); if(ret)GetExitCodeThread(thread,(LPDWORD) ret);CloseHandle(thread)
/*** @brief 分离线程,会销毁线程句柄,但线程依然执行* @param thread 线程句柄,类型为Thread* @return 0成功,其他值失败*/
#define thread_detach(thread) CloseHandle(thread)==0
/*** @brief 获取当前线程id* @return 线程id,类型为unsigned long*/
#define thread_self_id() (unsigned long)GetCurrentThreadId()
/*** @brief 休眠当前线程* @param ms 毫秒数* @return void无返回*/
#define thread_sleep(ms) Sleep(ms)
#else
#include <pthread.h>
#include <sys/time.h>
#include <semaphore.h>
#include <stdint.h>
typedef pthread_t Thread;
#define TINT intptr_t
#define thread_create(tid, callback, arg) pthread_create(tid, NULL, (void*(*)(void*))callback, arg)
#define thread_join(tid,ret) { intptr_t retval;  pthread_join(tid, (void**)&retval); if (ret){*(int*)ret = (int)retval; } }
#define thread_detach(tid) pthread_detach(tid)
#define thread_self_id() (unsigned long)pthread_self()
#define thread_sleep(ms){struct timeval delay = {ms / 1000, (ms % 1000) * 1000}; select(NULL, NULL, NULL, NULL, &delay);}      
#endif
#endif

三、使用示例

1、创建线程并等待返回

#include"thread.h"
#include<stdint.h>
TINT thread_proc(void *arg)
{intptr_t num = (intptr_t)arg;printf("pass arg : %d , thread id : %d\n", (int)num,thread_self_id());//sleep200毫秒thread_sleep(200);// 返回线程idreturn thread_self_id();
}int main()
{//线程句柄Thread thread;int ret;// 创建线程thread_create(&thread, thread_proc, (void *)1);// 等待线程结束,并获取返回值thread_join(thread, &ret);printf("return thread id : %d \n",ret);return 0;
}

在这里插入图片描述

2、创建线程并分离

#include"thread.h"
#include<stdint.h>
TINT thread_proc(void *arg)
{intptr_t num = (intptr_t)arg;printf("pass arg : %d , thread id : %d\n", (int)num,thread_self_id());//sleep200毫秒thread_sleep(200);printf("thread exit\n");return 0;
}int main()
{//线程句柄Thread thread;int ret;//创建线程thread_create(&thread, thread_proc, (void *)1);//分离线程thread_detach(thread);printf("detach thread \n");thread_sleep(1000);printf("program exit\n");return 0;
}

在这里插入图片描述


总结

以上就是今天要讲的内容,本文只是简单不同平台的线程方法进行了一个整合,对函数的参数和返回值做了一个统一,由于在所有实现在头文件中,使用起来是很方便的,不需要额外编写makefile或cmakelist,头文件拷贝到项目就能直接用。

http://www.xdnf.cn/news/12552.html

相关文章:

  • SATA3.0接口PCB布局走线注意事项
  • 【Redis】Cluster集群
  • C++11 右值引用:从入门到精通
  • Vue3 卡片绑定滚动条 随着滚动条展开效果 GSAP动画库 ScrollTrigger滚动条插件
  • 2025/6/4—halcon算子及知识点总结
  • windows11右键取消二次加载
  • 网络编程之服务器模型与UDP编程
  • CVPR 2025 | 港中文 MMLab 提出文生图模型 T2I-R1,文生图进入R1时刻!
  • 让敏感数据在流转与存储中始终守护在安全范围
  • mysql的分页场景下,页数越大查询速度越慢的解决方法
  • K-Means颜色变卦和渐变色
  • linux系统终端远程控制和传输方式
  • 【 *p取出内容 a得到地址】
  • clickhouse 学习总结
  • KuiperInfer跟学第二课——张量的构建与实现
  • 什么是质量管理系统?质量管理系统都有哪些功能?
  • 5G网络中频段的分配
  • 无公网IP外网可访问项目研发管理软件 codes
  • AI界的“专家会诊”:MoE模型如何让AI变得更聪明?
  • Java多态机制深度解析
  • 在网页加载时自动运行js的方法(2025最新)
  • Linux 前后端项目问题排查命令手册
  • 量化面试绿皮书:5. 扑克牌游戏概率与期望值
  • 深入理解Java单例模式:确保类只有一个实例
  • android app 一个 crash的解决过程!
  • count() + case when统计问题
  • Next.js+prisma开发二
  • 【LLMs篇】14:扩散语言模型的理论优势与局限性
  • H_Prj06 8088单板机的串口
  • 贝叶斯网络_TomatoSCI分析日记