一、 简单实例
比较简单的代码,创建10个线程,其中使第4个线程在一创建就挂起,等到其他的线程执行的差不多的时候再使第4个线程恢复执行
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
windows.h
>
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
#define
THREAD_NUM 10
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
DWORD WINAPI PrintThreads (LPVOID);
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
int
main ()
{
HANDLE hThread[THREAD_NUM];
DWORD dwThreadID[THREAD_NUM];
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
for (int i=0; i<THREAD_NUM; ++i)
{
int isStartImmediate = 0;
if(3 == i)
isStartImmediate = CREATE_SUSPENDED;
hThread[i]=CreateThread(NULL, // security attributes that should be applied to the new thread,
// this is for NT. Use NULL to get the default security attributes. Use NULL for win95
0, // default size of 1MB can be passed by passing zero.
PrintThreads, // function name:address of the function where the new thread starts.
(LPVOID)i, // parameter(void pointer): pointer to the 32 bit parameter that will be passed into the thread
isStartImmediate, // flags to control the creation of the thread. Passing zero starts the thread immediately.
// Passing CREATE_SUSPENDED suspends the thread until the ResumeThread( ) function is called.
&dwThreadID[i] // pointer to a 32-bit variable that receives the thread identifier.
);
if (hThread[i])
{
printf ("Thread launched successfully\n");
}
}
printf("Start sleep 100, and let other thread excute\n");
Sleep (100);
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
printf("Start sleep 100, and thread 3 excute\n");
ResumeThread(hThread[3]);
Sleep(100);
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
for(int i = 0; i<THREAD_NUM; ++i)
{
if (hThread[i])
{
CloseHandle(hThread[i]); // You need to use this to release kernel objects when you are done using them.
// If a process exits without closing the thread handle,
// the operating system drops the reference counts for those objects.
// But if a process frequently creates threads without closing the handles,
// there could be hundreds of thread kernel objects lying around and these resource leaks can have a big hit on performance.
}
}
return (0);
}
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
//
function PrintThreads
DWORD WINAPI PrintThreads (LPVOID num)
{
for (int i=0; i<10; i++)
printf ("Thread Number is %d%d%d\n", num,num,num);
return 0;
}
比较简单的代码,创建10个线程,其中使第4个线程在一创建就挂起,等到其他的线程执行的差不多的时候再使第4个线程恢复执行
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
![多线程(C++)WIN <wbr>API 多线程(C++)WIN <wbr>API](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
二、 其他基本API的说明
CreateThread() 调用成功返回句柄和一个id。
CloseHandle()
SuspendThread(HANDLE) 允许开发人员将HANDLE指定的线程挂起,如果要挂起的线程占有共享资源,则可能导致死锁。
ResumeThread(HANDLE)
TerminateThread() 立即终止线程的工作,不做任何清理工作。
ExitThread() 线程函数返回时回调用次函数,所以一般我们不去显示的调用。
ExitThread是推荐使用的结束一个线程的方法,当调用该函数时,当前线程的栈被释放,然后线程终止,相对于TerminateThread 函数来说,这样做能够更好地完成附加在该线程上的DLL的清除工作. 但是ExitThread()会导致线程在清处构造器/自动变量之前就终止,所以我们最好不要显示的调用ExitThread()。
三、多线程的入口函数都是全局函数,如果有类,应该写在类的外面。还有共享资源也应该是全局的。