Kisser Leon

这个kisser不太冷
posts - 100, comments - 102, trackbacks - 0, articles - 0

Win32 Thread API学习之Critical Sections

Posted on 2007-03-30 14:58 kk 阅读(529) 评论(0)  编辑 收藏 引用 所属分类: IT

Critical Sections 并不是核心对象。因此没有 handle 这样的东西。它和核心对象不同,它存在于进程的内存空间中。你不需要使用想“ Create ”这样的 API 函数获得一个 critical section handle 。你应该做的是将一个类型为 CRITICAL_SECTION 的局部变量初始化,方法是调用 InitializeCriticalSection():

The InitializeCriticalSection function initializes a critical section object.

VOID InitializeCriticalSection(

    LPCRITICAL_SECTION lpCriticalSection                // address of critical section object 

   );         

 Parameters

lpCriticalSection

Points to the critical section object.

Return Values

This function does not return a value.


当你用毕
critical section 时,你必须调用 DeleteCriticalSection() 清除它。这个函数并没有“释放对象”的意义在里头,不要把它和 C++ delete 运算符混淆了。

The DeleteCriticalSection function releases all resources used by an unowned critical section object.

VOID DeleteCriticalSection(

    LPCRITICAL_SECTION lpCriticalSection                // pointer to critical section object 

   );         

Parameters

lpCriticalSection

Points to the critical section object.

Return Values

This function does not return a value.


一旦
critical section 被初始化,每一个线程就可以进入其中——只要他通过了 EnterCriticalSection() 这一关:

The EnterCriticalSection function waits for ownership of the specified critical section object. The function returns when the calling thread is granted ownership.

VOID EnterCriticalSection(

    LPCRITICAL_SECTION lpCriticalSection                // pointer to critical section object 

   );         

Parameters

lpCriticalSection

Points to the critical section object.

Return Values

This function does not return a value.


当线程准备好离开
critical section 的时候,他必须调用 LeaveCriticalSection():

The LeaveCriticalSection function releases ownership of the specified critical section object.

VOID LeaveCriticalSection(

    LPCRITICAL_SECTION lpCriticalSection                // address of critical section object 

   );         

Parameters

lpCriticalSection

Points to the critical section object.

Return Values

This function does not return a value.


下面是一个很简单的使用例子,只是为了演示如何使用,没有其它目的:

#include <stdio.h>

#include <windows.h>

const int numThreads = 3;

//¶¨ÒåÒ»¸öcritical section±äÁ¿

CRITICAL_SECTION gCS;

 

DWORD WINAPI helloFunc(LPVOID pArg)

{

       //½øÈëcritical section

       EnterCriticalSection(&gCS);

      

       int num = (int) pArg;

       printf("Hello Thread %d\n", num);

 

       //Ïß³ÌÓñϣ¬¼ÇµÃµ÷Óô˺¯ÊıÀ뿪critical section¡££¨Enter**ÓëLeave**µÄÊıÁ¿ÒªÅä¶Ô£©

       LeaveCriticalSection(&gCS);

       return 0;

}

 

int main()

{     

       //³õʼ»¯critical section

       InitializeCriticalSection(&gCS);

       HANDLE hThread[numThreads];

       DWORD begin = GetTickCount();

 

       for (int i = 0; i < numThreads; i++)

       {

              hThread[i] = CreateThread(NULL, 0, helloFunc, (LPVOID)i, 0, NULL);

       }

 

       WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE);

       DWORD elapse = GetTickCount() - begin;

       printf("elapse time: %d\n", elapse);

 

       //ËùÓĞÏß³ÌÖ´ĞĞÍê±Ïºó£¬deleteµôcritical section

       DeleteCriticalSection(&gCS);

       return 0;

}

 

 

Google

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理