多线程同步(C++)WIN API

一、 简单实例
比较简单的代码,创建10个线程,其中使第4个线程在一创建就挂起,等到其他的线程执行的差不多的时候再使第4个线程恢复执行
多线程(C++)WIN <wbr>API #include  < stdio.h >  
多线程(C++)WIN <wbr>API#include 
< stdlib.h >  
多线程(C++)WIN <wbr>API#include 
< windows.h >  
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>API
#define  THREAD_NUM 10
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>APIDWORD WINAPI PrintThreads (LPVOID);
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>API
int  main () 
多线程(C++)WIN <wbr>API

多线程(C++)WIN <wbr>API    HANDLE hThread[THREAD_NUM]; 
多线程(C++)WIN <wbr>API    DWORD dwThreadID[THREAD_NUM]; 
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>API    
for (int i=0i<THREAD_NUM; ++i) 
多线程(C++)WIN <wbr>API    

多线程(C++)WIN <wbr>API        
int isStartImmediate = 0;
多线程(C++)WIN <wbr>API        
if(3 == i)
多线程(C++)WIN <wbr>API            isStartImmediate 
= CREATE_SUSPENDED;
多线程(C++)WIN <wbr>API        hThread[i]
=CreateThread(NULL,                // security attributes that should be applied to the new thread, 
多线程(C++)WIN <wbr>API                                                                                 
// this is for NT. Use NULL to get the default security attributes. Use NULL for win95 
多线程(C++)WIN <wbr>API
                                0                                           // default size of 1MB can be passed by passing zero. 
多线程(C++)WIN <wbr>API
                                PrintThreads,                     // function name:address of the function where the new thread starts.
多线程(C++)WIN <wbr>API
                                (LPVOID)i,                         // parameter(void pointer): pointer to the 32 bit parameter that will be passed into the thread
多线程(C++)WIN <wbr>API
                                isStartImmediate,             // flags to control the creation of the thread. Passing zero starts the thread immediately. 
多线程(C++)WIN <wbr>API                                                                          
 // Passing CREATE_SUSPENDED suspends the thread until the ResumeThread( function is called.
多线程(C++)WIN <wbr>API
                                &dwThreadID[i]           // pointer to 32-bit variable that receives the thread identifier.
多线程(C++)WIN <wbr>API
                                ); 
多线程(C++)WIN <wbr>API        
if (hThread[i])
多线程(C++)WIN <wbr>API        

多线程(C++)WIN <wbr>API            printf (
"Thread launched successfully\n");                
多线程(C++)WIN <wbr>API        }
         
多线程(C++)WIN <wbr>API    }
 
多线程(C++)WIN <wbr>API    printf(
"Start sleep 100, and let other thread excute\n");
多线程(C++)WIN <wbr>API    Sleep (
100);    
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>API    printf(
"Start sleep 100, and thread excute\n");
多线程(C++)WIN <wbr>API    ResumeThread(hThread[
3]);
多线程(C++)WIN <wbr>API    
多线程(C++)WIN <wbr>API    Sleep(
100);
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>API    
for(int = 0i<THREAD_NUM; ++i)
多线程(C++)WIN <wbr>API    
{
多线程(C++)WIN <wbr>API        
if (hThread[i])
多线程(C++)WIN <wbr>API        
           
多线程(C++)WIN <wbr>API            CloseHandle(hThread[i]);    
// You need to use this to release kernel objects when you are done using them. 
多线程(C++)WIN <wbr>API                                        
// If process exits without closing the thread handle, 
多线程(C++)WIN <wbr>API                                        
// the operating system drops the reference counts for those objects. 
多线程(C++)WIN <wbr>API                                        
// But if process frequently creates threads without closing the handles, 
多线程(C++)WIN <wbr>API                                        
// there could be hundreds of thread kernel objects lying around and these resource leaks can have big hit on performance.
多线程(C++)WIN <wbr>API
        }
 
多线程(C++)WIN <wbr>API    }

多线程(C++)WIN <wbr>API    
return (0); 
多线程(C++)WIN <wbr>API}
 
多线程(C++)WIN <wbr>API
多线程(C++)WIN <wbr>API
// function PrintThreads 
多线程(C++)WIN <wbr>API
DWORD WINAPI PrintThreads (LPVOID num)
多线程(C++)WIN <wbr>API
{
多线程(C++)WIN <wbr>API    
for (int i=0i<10i++
多线程(C++)WIN <wbr>API        printf (
"Thread Number is %d%d%d\n"num,num,num); 
多线程(C++)WIN <wbr>API    
return 0;
多线程(C++)WIN <wbr>API}


二、 其他基本API的说明
CreateThread() 调用成功返回句柄和一个id。
CloseHandle()  关闭一个打开的对象句柄,该对象句柄可以是线程句柄,也可以是进程、信号量等其他内核对象的句柄,如果对象不需要使用了,一定要使用这个函数释放内核对象。

SuspendThread(HANDLE) 允许开发人员将HANDLE指定的线程挂起,如果要挂起的线程占有共享资源,则可能导致死锁。
ResumeThread(HANDLE)  恢复指定的线程。

TerminateThread() 立即终止线程的工作,不做任何清理工作。
ExitThread() 线程函数返回时回调用次函数,所以一般我们不去显示的调用。

ExitThread是推荐使用的结束一个线程的方法,当调用该函数时,当前线程的栈被释放,然后线程终止,相对于TerminateThread 函数来说,这样做能够更好地完成附加在该线程上的DLL的清除工作. 但是ExitThread()会导致线程在清处构造器/自动变量之前就终止,所以我们最好不要显示的调用ExitThread()。

三、多线程的入口函数都是全局函数,如果有类,应该写在类的外面。还有共享资源也应该是全局的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值