WaitForSingleObject、WaitForMultipleObjects、CreateThread .

///
用户模式的等待
WaitForSingleObject等待一个同步对象
DWORD WaitForSingleObject( 
  HANDLE hHandle,         //同步对象句柄
  DWORD dwMilliseconds    //等待时间 ms(毫秒)
  );

WaitForMultipleObjects等待多个同步对象
DWORD WaitForMultipleObjects(
  DWORD nCount,             //同步对象数组元素个数
  const HANDLE* lpHandles, //同步对象数组指针
  BOOL bWaitAll,            //是否等待全部同步对象
  DWORD dwMilliseconds      //等待时间
  );

用户模式CreateThread创建新线程
HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES lpThreadAttributes,   //安全属性
  DWORD dwStackSize,                          //初始化堆栰大小
  LPTHREAD_START_ROUTINE lpStartAddress,      //线程运行的函数指针
  LPVOID lpParameter,                         //传入函数中的参数
  DWORD dwCreationFlags,                      //开启线程时的状态
  LPDWORD lpThreadId                          //返回线程ID
    ); 
另外,创建线程的时候最好不使用CreateThread函数,而使用_beginthreadex函数。
_beginthreadex函数是对CreateThread函数的封装,其参数与CreateThread完全一致。
_beginthreadex函数的函数名前面有个下滑线,是因为它不是标准C语言提供的运行时函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 `CreateThread` 实现一个线程池,需要完成以下几个步骤: 1. 定义线程池结构体 ```c typedef struct { int thread_count; // 线程数量 HANDLE *threads; // 线程句柄数组 HANDLE *queue; // 任务队列 CRITICAL_SECTION queue_lock; // 任务队列锁 HANDLE signal_event; // 信号事件 bool terminate; // 是否终止 } thread_pool_t; ``` 2. 初始化线程池 ```c bool thread_pool_init(thread_pool_t *pool, int thread_count) { pool->thread_count = thread_count; pool->threads = (HANDLE*)malloc(thread_count * sizeof(HANDLE)); pool->queue = (HANDLE*)malloc(thread_count * sizeof(HANDLE)); InitializeCriticalSection(&pool->queue_lock); pool->signal_event = CreateEvent(NULL, FALSE, FALSE, NULL); pool->terminate = false; for (int i = 0; i < thread_count; i++) { pool->threads[i] = CreateThread(NULL, 0, thread_func, pool, 0, NULL); if (!pool->threads[i]) { return false; } } return true; } ``` 3. 线程函数 ```c DWORD WINAPI thread_func(LPVOID param) { thread_pool_t *pool = (thread_pool_t*)param; HANDLE task; while (true) { WaitForSingleObject(pool->signal_event, INFINITE); EnterCriticalSection(&pool->queue_lock); if (pool->terminate) { LeaveCriticalSection(&pool->queue_lock); break; } task = NULL; for (int i = 0; i < pool->thread_count; i++) { if (pool->queue[i]) { task = pool->queue[i]; pool->queue[i] = NULL; break; } } LeaveCriticalSection(&pool->queue_lock); if (task) { // 执行任务 } } return 0; } ``` 4. 添加任务 ```c bool thread_pool_add_task(thread_pool_t *pool, LPTHREAD_START_ROUTINE func, LPVOID arg) { EnterCriticalSection(&pool->queue_lock); for (int i = 0; i < pool->thread_count; i++) { if (!pool->queue[i]) { pool->queue[i] = CreateThread(NULL, 0, func, arg, 0, NULL); SetEvent(pool->signal_event); LeaveCriticalSection(&pool->queue_lock); return true; } } LeaveCriticalSection(&pool->queue_lock); return false; } ``` 5. 销毁线程池 ```c void thread_pool_destroy(thread_pool_t *pool) { pool->terminate = true; SetEvent(pool->signal_event); WaitForMultipleObjects(pool->thread_count, pool->threads, TRUE, INFINITE); for (int i = 0; i < pool->thread_count; i++) { CloseHandle(pool->threads[i]); } free(pool->threads); free(pool->queue); DeleteCriticalSection(&pool->queue_lock); CloseHandle(pool->signal_event); } ``` 完整代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值