0 Basic Reverse engineering 36_win32_10_ Mutex _forsingleobject_waitformultipleobjects

Source: Internet
Author: User
Tags mutex

1 Introduction

Tells the second kernel object, the mutex. A kernel object, a thread, has been learned before. This section speaks of two functions, WaitForSingleObject () and WaitForMultipleObjects (). So these two functions operate according to the state of the kernel object.

2 Forsingleobject ()
DWORD WaitForSingleObject(  HANDLE hHandle,        // handle to object  DWORD dwMilliseconds   // time-out interval);

Function Description:
The wait function allows a thread to voluntarily enter a wait state until a particular kernel object becomes a notified state.

Hhandle:
The kernel object handle, which can be either a process or a thread.

Dwmilliseconds:
Wait time, Unit is milliseconds INFINITE (-1) has been waiting

return value:
WAIT_OBJECT_0 (0) Wait for the object to become notified
Wait_timeout (0x102) timeout

Special Note:
1. Each object in the kernel object can be said to be in a notified or not notified state
2. The switchover of this state is determined by the set of rules that Microsoft establishes for each object
3. Thread kernel object is in an unspecified state when thread is running
4. When the thread terminates, it becomes a notified state
5, in the kernel is a bool value, run-time false End True

Code Demo

DWORD WINAPI ThreadProc1(LPVOID lpParameter)    {        for(int i=0;i<5;i++)    {        printf("+++++++++\n");      Sleep(1000);   }     return 0;   }           int main(int argc, char* argv[])    {              //创建一个新的线程     HANDLE hThread1 = ::CreateThread(NULL, 0, ThreadProc1,         NULL, 0, NULL);         DWORD dwCode = ::WaitForSingleObject(hThread1, INFINITE);            MessageBox(0,0,0,0);          return 0;   }     
3 and WaitForMultipleObjects ()
DWORD WaitForMultipleObjects(  DWORD nCount,             // number of handles in array  CONST HANDLE *lpHandles,  // object-handle array  BOOL bWaitAll,            // wait option  DWORD dwMilliseconds      // time-out interval);

Feature Description: View the notification status of several kernel objects at a time

Ncount: To see the number of kernel objects

Lphandles: Array of Kernel objects

bWaitAll: Wait until the type is TRUE until all becomes notified FALSE as long as there is a notification

Dwmilliseconds: Timeout period

Infinite has been waiting

return value:
When bWaitAll is true, returns the WAIT_OBJECT_0 (0) code so the kernel object becomes notified
When bWaitAll is false, returns the index of the kernel object that first became notified in the array

Wait_timeout (0x102)

Code Demo:

DWORD WINAPI ThreadProc1(LPVOID lpParameter)    {        for(int i=0;i<5;i++)    {        printf("+++++++++\n");      Sleep(1000);   }     return 0;   }           DWORD WINAPI ThreadProc2(LPVOID lpParameter)    {        for(int i=0;i<3;i++)    {        printf("---------\n");      Sleep(1000);   }           return 0;   }                 int main(int argc, char* argv[])    {              HANDLE hArray[2];          //创建一个新的线程     HANDLE hThread1 = ::CreateThread(NULL, 0, ThreadProc1,         NULL, 0, NULL);         //创建一个新的线程     HANDLE hThread2 = ::CreateThread(NULL, 0, ThreadProc2,         NULL, 0, NULL);         hArray[0] = hThread1;      hArray[1] = hThread2;            DWORD dwCode = ::WaitForMultipleObjects(2, hArray,FALSE,INFINITE);            MessageBox(0,0,0,0);          return 0;   }     
4 cross-process threading control and mutexes

Process one:
HANDLE G_hmutex = CreateMutex (Null,false, "XYZ");

Process two:
HANDLE G_hmutex = OpenMutex (Mutex_all_access,false, "XYZ");
WaitForSingleObject (G_hmutex,infinite);

Logic code
ReleaseMutex (G_hmutex);

Process three:
HANDLE G_hmutex = OpenMutex (Mutex_all_access,false, "XYZ");
WaitForSingleObject (G_hmutex,infinite);

Logic code
ReleaseMutex (G_hmutex);

The difference between a mutex and a critical section:
1. The critical section can only be used for thread control between individual processes.
2, the mutex can be set to wait timeout, but the critical section cannot.
3. When the thread ends unexpectedly, the mutex can avoid infinite wait.
4. The efficiency of the mutex is not high in the critical area.

5 Practice Items

To do a red envelopes project, the requirements are as follows

第一步:在第一个文本框中输入一个值,比如1000         第二步:点击抢红包,同时创建3个线程,每个线程循环进行抢红包的操作,每次抢50    第三步:使用Mutex进行线程控制,当第一个文本框中的值<50时,强红包线程结束.       特别说明:        1、四个文本框中的值总和应该为1000  2、强红包线程每次延时50毫秒. 3、使用WaitForMultipleObjects监听所有线程,当线程全部结束后          

0 Basic Reverse engineering 36_win32_10_ Mutex _forsingleobject_waitformultipleobjects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.