C++多线程的创建与使用
来源:互联网 发布:短信恢复软件免费版 编辑:程序博客网 时间:2024/06/06 02:12
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
DWORD WINAPI run(LPVOID p)
{
printf("three%d run../n",p);
Sleep((DWORD)p*3000);
return (DWORD)p;
}
int main()
{
HANDLE three1 = NULL; //线程
HANDLE three2 = NULL;
DWORD uExitCode1 = 0; // 返回值
DWORD uExitCode2 = 0;
DWORD dwThId = 0;
DWORD dwThId1 = 0; // 线程id号
int c = 0;
three1 = CreateThread (NULL, 0, run, (LPVOID)1, 0, &dwThId);
three2 = CreateThread (NULL, 0, run, (LPVOID)2, 0, &dwThId1);
for (;;)
{
puts ("press any key to check thread ...");
_getch ();
//'GetExitCodeThread'函数来判断一个线程是否还在执行 说明 获取一个已
// 中止线程的退出代码 返回值 bool,非零表示成功,零表示失败。...
GetExitCodeThread (three2, &uExitCode2);
if (!c)
{
GetExitCodeThread (three1, &uExitCode1);
if (uExitCode1 == STILL_ACTIVE)
{
puts ("Thread1 still active");
}
else
{
printf ("Thread1 over with: %d/n", dwThId);
CloseHandle (three1);
c = 1;
printf ("Thread1 over with: %d/n",dwThId);
}
}
if (uExitCode2 == STILL_ACTIVE)
{
puts("Thread2 still active");
}
else
{
printf ("Thread2 over with: %d/n",dwThId1);
}
if ((uExitCode1!=STILL_ACTIVE) && (uExitCode2!=STILL_ACTIVE))
{
break;
}
}
// CloseHandle(three1); // 关闭线程句柄并不影响线程的执行
CloseHandle(three2);
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
DWORD WINAPI run(LPVOID p)
{
printf("three%d run../n",p);
Sleep((DWORD)p*3000);
return (DWORD)p;
}
int main()
{
HANDLE three1 = NULL; //线程
HANDLE three2 = NULL;
DWORD uExitCode1 = 0; // 返回值
DWORD uExitCode2 = 0;
DWORD dwThId = 0;
DWORD dwThId1 = 0; // 线程id号
int c = 0;
three1 = CreateThread (NULL, 0, run, (LPVOID)1, 0, &dwThId);
three2 = CreateThread (NULL, 0, run, (LPVOID)2, 0, &dwThId1);
for (;;)
{
puts ("press any key to check thread ...");
_getch ();
//'GetExitCodeThread'函数来判断一个线程是否还在执行 说明 获取一个已
// 中止线程的退出代码 返回值 bool,非零表示成功,零表示失败。...
GetExitCodeThread (three2, &uExitCode2);
if (!c)
{
GetExitCodeThread (three1, &uExitCode1);
if (uExitCode1 == STILL_ACTIVE)
{
puts ("Thread1 still active");
}
else
{
printf ("Thread1 over with: %d/n", dwThId);
CloseHandle (three1);
c = 1;
printf ("Thread1 over with: %d/n",dwThId);
}
}
if (uExitCode2 == STILL_ACTIVE)
{
puts("Thread2 still active");
}
else
{
printf ("Thread2 over with: %d/n",dwThId1);
}
if ((uExitCode1!=STILL_ACTIVE) && (uExitCode2!=STILL_ACTIVE))
{
break;
}
}
// CloseHandle(three1); // 关闭线程句柄并不影响线程的执行
CloseHandle(three2);
return EXIT_SUCCESS;
}
- C++多线程的创建与使用
- C语言多线程基础-01-线程的创建与销毁
- 多线程的几中创建方法与基本使用
- Java多线程------多线程的创建与同步
- C#_多线程创建和使用
- 多线程的创建与运行
- 多线程的创建和使用
- 多线程的创建和使用
- 使用_beginThreadex创建多线程(C语言版多线程)
- 使用_beginThreadex创建多线程(C语言版多线程)
- C语言实现的创建多线程
- linux下C语言多线程(一)线程的创建与取消
- linux下C语言多线程(一)线程的创建与取消
- linux下C语言多线程(一)线程的创建与取消
- Linux下通用线程池的创建与使用(C++)
- [C++]数据结构:最大堆MaxHeap的创建与使用
- [C++]数据结构:最大堆MaxHeap的创建与使用
- C/C++ OpenCV滑动条的创建与使用
- 谋划(25)
- WebServices项目中的角色
- 取生产订单状态的逻辑
- C++模板用法:定义、使用 示例
- JavaScript FAQ(四)——导航功能增强
- C++多线程的创建与使用
- Java栈与堆
- oracle 日期处理大全
- Flex3初体验:)
- 用VC让程序不产生控制台界面
- 2440BSP 支持大于4G SD卡的修改方法
- VB 中用代码打开 Outlook 发Mail
- 12 movie
- subList