0 Basic Reverse Engineering 37_win32_10_ Event _ Thread synchronization

Source: Internet
Author: User

1 Kernel objects

The thread and mutex two kernel objects have been learned before. This section tells the event of this kernel object. The concept of kernel objects, which is probably not very clear, is simply that the kernel object is the system level thing.

1.1 Summary Kernel objects:

Processes, threads, events, mutexes, files, file mappings, and so on.

1.2 Creation of event kernel objects
HANDLE g_hEvent = CreateEvent(NULL, TRUE, FALSE, "XYZ");HANDLE g_hMutex = CreateMutex(NULL,FALSE, "XYZ");
1.3 Getting the Event kernel object
HANDLE OpenEvent(  DWORD dwDesiredAccess,  // access  BOOL bInheritHandle,    // inheritance option  LPCTSTR lpName          // object name);HANDLE g_hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, "XYZ");HANDLE g_hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE, "XYZ");
1.4 Destruction of Kernel objects

BOOL CloseHandle (HANDLE hobj);

(1), when there is no other program reference, the system destroys the kernel object (number of uses).
(2), the life cycle of a kernel object may be longer than the object that created it.

2 creation of Event object 2.1 event object
HANDLE CreateEvent(  LPSECURITY_ATTRIBUTES lpEventAttributes, // 安全属性 NULL时为系统默认  BOOL bManualReset,                       // TRUE 通过调用ResetEvent将事件对象标记为未通知  BOOL bInitialState,                      // TRUE 已通知状态  FALSE未通知状态  LPCTSTR lpName                           // 对象名称 以NULL结尾的字符串);
2.2 Control of Event objects
BOOL SetEvent(HANDLE hEvent);
2.3 Closing the time object handle
CloseHandle();
2.4 Threading Control Experiment: Read-only form of line programming
HANDLE g_hevent; HWND hEdit1; HWND HEdit2; HWND hEdit3; HWND HEDIT4; HANDLE HThread1; HANDLE hThread2; HANDLE hThread3;  HANDLE Hthread4;dword WINAPI ThreadProc1 (lpvoid lpparameter) {//Create Event//default security attribute manually set not notification status (TRUE) initial state not notified No name g_hevent =  CreateEvent (null, TRUE, FALSE, NULL);  HANDLE Hthread[3];  Create 3 threads Hthread[0] =:: CreateThread (NULL, 0, THREADPROC2, NULL, 0, NULL);  HTHREAD[1] =:: CreateThread (NULL, 0, THREADPROC3, NULL, 0, NULL);  HTHREAD[2] =:: CreateThread (NULL, 0, ThreadProc4, NULL, 0, NULL);  Sets the value of the text box SetWindowText (hEdit1, "1000");  Set event as notified SetEvent (g_hevent);  Wait for thread to end destroy Kernel object WaitForMultipleObjects (3, Hthread, TRUE, INFINITE);  CloseHandle (Hthread[0]);  CloseHandle (hthread[1]);  CloseHandle (hthread[2]);  CloseHandle (g_hevent); return 0;}  DWORD WINAPI ThreadProc2 (lpvoid lpparameter) {TCHAR szbuffer[10] = {0};  When the event becomes notified WaitForSingleObject (G_hevent, INFINITE);  Read Content GetWindowText (hedit1,szbuffer,10);  SetWindowText (Hedit2,szbuffer); return 0;} DWORD WINAPI ThreadproC3 (LPVoid lpparameter) {TCHAR szbuffer[10] = {0};  When the event becomes notified WaitForSingleObject (G_hevent, INFINITE);  Read Content GetWindowText (hedit1,szbuffer,10);  SetWindowText (Hedit3,szbuffer); return 0;}  DWORD WINAPI ThreadProc4 (lpvoid lpparameter) {TCHAR szbuffer[10] = {0};  When the event becomes notified WaitForSingleObject (G_hevent, INFINITE);  Read Content GetWindowText (hedit1,szbuffer,10);  SetWindowText (Hedit4,szbuffer); return 0;}
3 Thread synchronization 3.1 What is thread synchronization?

Synchronization is a coordinated pace, in order to operate in a predetermined sequence. such as: You finish, I say.

such as process, thread synchronization, can be understood as a process or thread A and b together, a to a certain extent to rely on a result of B, so stop, motioned B to run, B according to the words, and then the result to A;a to continue operation.

[Excerpt from Baidu Encyclopedia]

3.2 Events and critical areas
HANDLE G_hset, G_hclear;int g_max = 10;int g_number = 0;//producer thread function DWORD WINAPI threadproduct (LPVOID PM) {for (int i = 0; i < G_max;        i++) {WaitForSingleObject (G_hset, INFINITE);        G_number = 1;        DWORD id = getcurrentthreadid ();        printf ("Producer%d puts data%d into buffer \ n", id, g_number);    SetEvent (g_hclear); } return 0;} The consumer thread function DWORD WINAPI threadconsumer (LPVOID PM) {for (int i = 0; i < G_max; i++) {WaitForSingleObject (g_h        Clear, INFINITE);        G_number = 0;        DWORD id = getcurrentthreadid ();        printf ("----consumer%d put data%d into buffer \ n", id, g_number);    SetEvent (G_hset); } return 0;}    int main (int argc, char* argv[]) {HANDLE hthread[2];    G_hset = CreateEvent (null, FALSE, TRUE, NULL);    G_hclear = CreateEvent (null, FALSE, FALSE, NULL);    Hthread[0] =:: CreateThread (NULL, 0, threadproduct, NULL, 0, NULL);    HTHREAD[1] =:: CreateThread (NULL, 0, threadconsumer, NULL, 0, NULL); WaitForMultipleObjects (2, Hthread, TRUE, INFINITE);    CloseHandle (Hthread[0]);    CloseHandle (hthread[1]);    Destruction of CloseHandle (G_hset);  CloseHandle (g_hclear); return 0;}

0 Basic Reverse Engineering 37_win32_10_ Event _ Thread synchronization

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.