Windows Thread Synchronization With Kernel Objects

来源:互联网 发布:知柏地黄丸 编辑:程序博客网 时间:2024/05/20 06:05
<script type="text/javascript">google_ad_client="pub-6065469188450680";google_ad_width=728;google_ad_height=90;google_ad_format="728x90_as";google_ad_type="text_image";google_ad_channel="6872543818";</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

The following kernel objects can be in a signaled or nonsignaled state:

    Processes    Threads    Jobs    Files    Console input    File change notifications

    Events    Waitable timers    Semaphores    Mutexes       

The rules for a semaphore are as follows:

    If the current resource count is greater than 0, the semaphore is signaled.

    If the current resource count is 0, the semaphore is nonsignaled.

    The system never allows the current resource count to be negative.

    The current resource count can never be greater than the maximum resource count.

 

Mutex objects are different from all other kernel objects because they have a notion of "thread ownership."

Characteristic                                 Mutex                                         Critical Section
Performance                                    Slow                                          Fast
Can be used across process boundaries          Yes                                           No
Declaration                                    HANDLE hmtx;                                  CRITICAL_SECTION cs;
Initialization                                 hmtx= CreateMutex(NULL, FALSE, NULL);         InitializeCriticalSection(&cs);
Cleanup                                        CloseHandle(hmtx);                            DeleteCriticalSection(&cs);
Infinite wait                                  WaitForSingleObject(hmtx, INFINITE);          EnterCriticalSection(&cs);
0 wait                                         WaitForSingleObject(hmtx, 0);                 TryEnterCriticalSection(&cs);
Arbitrary wait                                 WaitForSingleObject(hmtx, dwMilliseconds);    Not possible
Release                                        ReleaseMutex(hmtx);                           LeaveCriticalSection(&cs);
Can be waited on with other kernel objects     Yes                                           No

原创粉丝点击