线程的创建,退出及获取线程信息API代码

哈哈,这个弄完了之后,下一步就到线程的挂起,恢复,切换,终止啦。。

代码:

 1 #include <Windows.h>
 2 #include <stdio.h>
 3 
 4 #define MAX_THREADS 5
 5 
 6 DWORD WINAPI ThreadProc(LPVOID);
 7 
 8 typedef struct _THREAD_PARAM{
 9     DWORD i;
10     DWORD dwRandom;
11     DWORD dwData;
12 } THREAD_PARAM, *LPTHREAD_PARAM;
13 
14 DWORD WINAPI ThreadProc(LPVOID lpParam)
15 {
16     LPTHREAD_PARAM pData;
17     pData = (LPTHREAD_PARAM)lpParam;
18     printf("TID = %u,\t Parameters = %u, %u, %u\n",
19         GetCurrentThreadId(),
20         pData->i, pData->dwRandom, pData->dwData);
21     HeapFree(GetProcessHeap(), 0, pData);
22     return 0;
23 }
24 
25 void main()
26 {
27     LPTHREAD_PARAM pData;
28     DWORD dwThreadId[MAX_THREADS];
29     HANDLE hThread[MAX_THREADS];
30     int i;
31 
32     for(i = 0; i < MAX_THREADS; i++)
33     {
34         pData = (LPTHREAD_PARAM)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
35             sizeof(THREAD_PARAM));
36         if(pData == NULL)
37         {
38             printf("HeapAlloc error;\n");
39             ExitProcess(2);
40         }
41 
42         pData->i = i;
43         pData->dwRandom = rand();
44         pData->dwData = 100;
45 
46         hThread[i] = CreateThread(
47             NULL,
48             0,
49             ThreadProc,
50             pData,
51             0,
52             &dwThreadId[i]);
53         if(hThread[i] == NULL)
54         {
55             ExitProcess(i);
56         }
57     }
58     WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);
59     for(i = 0; i < MAX_THREADS; i++)
60     {
61         CloseHandle(hThread[i]);
62     }
63 }

 

转载于:https://www.cnblogs.com/aguncn/archive/2013/03/31/2992519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值