Windows和Linux上分别怎样实现等待某个线程结束?---用WaitForSingleObject和pthread_join
来源:互联网 发布:mac mysql phpmyadmin 编辑:程序博客网 时间:2024/05/20 20:03
Windows和Linux上分别怎样实现等待某个线程结束呢, 我们先看Windows的情形, 直接上代码:
#include <windows.h>#include <iostream.h>DWORD WINAPI Fun1Proc(LPVOID lpParameter){int i = 0;for(i = 0; i < 100; i++){cout << "son thread" << i + 1 << endl;Sleep(20);}return 0;}int main(){HANDLE hThread1;hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);WaitForSingleObject(hThread1, INFINITE);int i = 0;for(i = 0; i < 100; i++){cout << "main thread " << i + 1 << endl;Sleep(20);}CloseHandle(hThread1);return 0;}结果是先打印100个son thread, 后打印100个main thread, 实现了预期的目标。
我们再看我们先看Linux的情形, 直接上代码:
#include <pthread.h>#include <stdio.h>void* threadFunc(void* p){while (1){printf("a");}return NULL;} int main (){pthread_t id;pthread_create(&id, NULL, threadFunc, NULL);pthread_join(id, NULL); // 阻塞地等待, 直到线程threadFunc退出printf("b"); // 永远无法打印, 因为线程函数threadFunc中有死循环, 无法退出return 0;}
好吧, 就这样。
0 0
- Windows和Linux上分别怎样实现等待某个线程结束?---用WaitForSingleObject和pthread_join
- pthread_join(等待另一个线程结束)
- 等待线程退出 MsgWaitForMultipleObjects和WaitForSingleObject
- linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结
- Linux线程pthread_exit 和 pthread_join
- 等待线程结束的高手--WaitForSingleObject
- linux线程,pthread_join和pthread_cleanup_push和pthread_cleanup_pop
- Linux线程中pthread_join和pthread_detach函数
- linux线程之pthread_join和pthread_detach
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值
- 线程等待WaitXXXXX()函数系列之WaitForSingleObject和WaitForMultipleObject
- WaitForSingleObject 和 WaitForMultipleObjects函数 (让线程挂起等待事件)
- 再谈线程等待函数WaitForSingleObject和CloseHandle
- Win32多线程之等待一个线程的结束(WaitForSingleObject)
- 等待线程退出pthread_join
- pthread_join pthread_exit 线程 Linux函数 线程退出 线程等待
- Linux--等待进程结束wait()和waitpid()
- pthread_join和pthread_detach---线程调用
- HDU 1180 诡异的楼梯
- poj1012
- android LinearLayout无法居中问题分析
- 十天学Linux内核之第十天---总结篇
- HDU2031 进制转换
- Windows和Linux上分别怎样实现等待某个线程结束?---用WaitForSingleObject和pthread_join
- 设计模式--命令模式
- socket属性设置与初始化操
- VC POST表单——登录验证新浪邮箱
- HDU 1226 超级密码
- HDU2032 杨辉三角
- lua中调用c++函数
- myeclipse 9.1 安装aptana 3.2 + FindBugs + SVN插件
- php5.3,5.4,5.5,5.6 中新特性