下面是几个C++线程的小例子
Code
1
2#include "stdafx.h"
3#include <windows.h>
4#include <iostream>
5using namespace std;
6
7#include <time.h>
8
9DWORD WINAPI Func(LPVOID p);
10
11typedef struct
12{
13 int firstArgu;
14 long secArgu;
15
16}MyType,* pMyType;
17
18//几个线程共享全局变量repeat
19BOOL repeat=TRUE;
20
21DWORD WINAPI MyThread1(LPVOID lpParameter)
22{
23 while(repeat)
24 {
25 printf("How are you? -----1-------MyThread1\n");
26 Sleep(2000); /**/////
27 }
28 DWORD exitCode;
29 ExitThread(exitCode);
30
31 return 0;
32}
33
34DWORD WINAPI MyThread2(LPVOID lpParameter)
35{
36 while(repeat)
37 {
38 printf("Very Well! ------2------MyThread2\n");
39 Sleep(1000);/**////
40 }
41 DWORD exitCode;
42 ExitThread(exitCode);
43
44 return 0;
45}
46
47DWORD WINAPI MyThread(LPVOID lpParameter)
48{
49 for(int i=0;i<100;i++)
50 {
51 printf("hello,everybody! ------------MyThread\n");
52 cout<<"My Thread ID:"<<GetCurrentThreadId()<<endl;
53 Sleep(100); /**/////
54 }
55// DWORD exitCode;
56// ExitThread(exitCode);
57
58 return 0;
59}
60
61DWORD WINAPI KillThread(LPVOID lpParameter)
62{
63 repeat=FALSE;
64 return 0;
65}
66
67int main(int argc, char* argv[])
68{
69 //Example 1
70// MyType MyTypeArgu={0};
71// MyTypeArgu.firstArgu=1;
72// MyTypeArgu.secArgu=100000000;
73// CreateThread(NULL,0,Func,&MyTypeArgu,0,NULL);
74// Sleep(10000);
75// cout<<"2-------------------------";
76// cout<<"该线程已删除……"<<endl;
77// Sleep(10000);
78// ExitThread(0);
79//----------------------------------------
80
81 //Example 2
82// HANDLE handle1,handle2,handle3;
83// DWORD dw1,dw2,dw3;
84//
85// handle1=CreateThread(NULL,0,MyThread1,NULL,0,&dw1);
86// if(handle1==NULL)
87// {
88// printf("Create Thread 1 Failed!\n");
89// return -1;
90// }
91//
92// handle2=CreateThread(NULL,0,MyThread2,NULL,0,&dw2);
93// if(handle2==NULL)
94// {
95//
96// printf("Create Thread 2 Failed!\n");
97// return -1;
98// }
99//
100// Sleep(6000); /////
101//
102// handle3=CreateThread(NULL,0,KillThread,NULL,0,&dw3);
103// if(handle3==NULL)
104// {
105// printf("Create Thread 3 Failed!\n");
106// return -1;
107// }
108// CloseHandle(handle1);
109// CloseHandle(handle2);
110// CloseHandle(handle3);
111// return 0;
112
113 //Example 3
114// HANDLE handle;
115// DWORD dw;
116//
117// handle=CreateThread(NULL,0,MyThread,NULL,0,&dw);
118//
119// if(handle==NULL)
120// {
121// printf("Create Thread Failed!\n");
122// return -1;
123// }
124//
125// Sleep(1000);
126//
127// for(int i=0;i<10;i++)
128// {
129// printf("*****Now,Suspended!****\n");
130// SuspendThread(handle); /////
131//
132// for(int j=0;j<3;j++)
133// {
134// printf("Good Thank you !\n");
135// Sleep(1000);
136// }
137// printf(">>>>Now Resumed!>>>>\n");
138// ResumeThread(handle); /////
139// Sleep(3000);
140// }
141//
142// GetExitCodeThread(handle,&dw);
143// cout<<"Thread Exit ID:"<<GetCurrentThreadId()<<endl;
144// CloseHandle(handle);
145 system("pause");
146 return 0;
147}
148
149DWORD WINAPI Func(LPVOID p)
150{
151 cout<<"1--------------------------"<<endl;
152 MyType param={0};
153 MoveMemory(¶m,p,sizeof(param));
154 int intValue=param.firstArgu;
155 long longValue=param.secArgu;
156 cout <<"Thread is running,线程正在运行,10秒钟后该线程将自动删除"<<endl;
157 cout <<"现在输出该线程函数传递的两个参数:"<<endl;
158 cout <<"firstArgu="<<intValue<<endl<<"secArgu="<<longValue<<"."<<endl;
159 cout<<"请等待……"<<endl<<endl;
160 return 0;
161}
162
163
164
165
166
C++ [线程学习小记]
下面是几个C++线程的小例子
Code