Windows via C/C ++ Study Notes-"waiting function" for "thread synchronization" of kernel objects"

Source: Internet
Author: User

The thread synchronization mechanism in user mode is highly efficient. If you need to consider thread synchronization, you should first consider the thread synchronization method in user mode.

However, user-mode thread synchronization is limited. For thread synchronization between multiple processes, the user-mode thread synchronization method is powerless. At this time, you can only consider using the kernel mode.

 

Windows provides many kernel objects for thread synchronization. For thread synchronization, these kernel objects have two very important states: "notified" and "not notified" (also translated as: trusted, not trusted ). Windows provides the following kernel objects: process, thread, job, file, console input/output/error stream, event, wait timer, semaphore, mutex object.

 

You can notify a kernel object to make it in the "notified" state, and then let other threads waiting for execution on the kernel object. You can use the API functions provided by windows to wait for the function to wait for one or more kernel objects to become notified.

You can use the waitforsingleobject function to wait for a kernel object to become notified:

DWORD waitforsingleobject (
Handle hobject, // specifies the handle of a kernel object
DWORD dwmilliseconds); // wait time

 

This function needs to pass a kernel object handle, which identifies a kernel object. If the kernel object is in the not notified State, this function causes the thread to enter the blocking state; if the kernel object is in the notified State, the function returns wait_object_0 immediately. The second parameter specifies the time (in milliseconds) to wait. You can pass the infinite parameter to indicate that you want to wait for an indefinite period of time. If the wait times out, the function returns wait_timeout. If this function fails, wait_failed is returned. The following code can be used to determine whether:

 

Dword dw = waitforsingleobject (hprocess, 5000); // wait until a process ends.
Switch (DW)
{
Case wait_object_0:
// The process represented by hprocess stops in 5 seconds
Break;

Case wait_timeout:
// Wait for more than 5 seconds
Break;

Case wait_failed:
// Function call failed. For example, an invalid handle is passed.
Break;
}

 

You can also use the waitformulitpleobjects function to wait for Multiple kernel objects to become notified:

DWORD waitformultipleobjects (
DWORD dwcount, // Number of waiting kernel objects
Const handle * phobjects, // an array that stores the waiting kernel object handle
Bool bwaitall, // whether to wait until all kernel objects are notified.
DWORD dwmilliseconds); // wait time

 

The first parameter of this function indicates the number of waiting kernel objects, which can be a value ranging from 0 to maximum_wait_objects (64. The phobjects parameter is an array that stores the waiting kernel object handle. If the bwaitall parameter is true, the function is returned only when all the waiting kernel objects are in the notified State. If it is false, as long as one kernel object is in the notified State, then this function is returned. The fourth parameter is similar to the dwmilliseconds parameter in waitforsingleobject.

If the function fails, wait_failed is returned. If the function times out, wait_timeout is returned. If the bwaitall parameter is true, wait_object_0 is returned if the function succeeds. If bwaitall is false, if the function succeeds, the return value indicates which kernel object receives the notification.

You can use this function as follows:

 

Handle H [3]; // handle Array

// Three process handles
H [0] = hprocess1;
H [1] = hprocess2;
H [2] = hprocess3;

Dword dw = waitformultipleobjects (3, H, false, 5000); // wait until the end of the three processes

Switch (DW)
{
Case wait_failed:
// Function call failed
Break;

Case wait_timeout:
// Timeout
Break;

Case wait_object_0 + 0:
// H [0] (hprocess1) indicates that the process has ended.
Break;

Case wait_object_0 + 1:
// H [1] (hprocess2) indicates that the process has ended.
Break;

Case wait_object_0 + 2:
// H [2] (hprocess3) indicates that the process ends.
Break;
}

 

You can also notify a kernel object and wait for another kernel object at the same time. These two operations are performed in an atomic way:

DWORD signalobjectandwait (
Handle hobjecttosignal, // notify the Kernel Object
Handle hobjecttowaiton, // The waiting Kernel Object
DWORD dwmilliseconds, // wait time
Bool balertable); // parameters related to the IO completion port.

 

This function internally changes the kernel object specified by the hobjecttosignal parameter to the notified State, and waits for the kernel object represented by the hobjecttowaiton parameter. The usage of the dwmilliseconds parameter is similar to that of the waitforsingleobject function.
This function returns the following: wait_object_0, wait_timeout, wait_failed, wait_io_completion.

When you need to notify a mutex Kernel Object and wait for an event kernel object, you can write it like this:

Releasemutex (hmutex );
Waitforsingleobject (hevent, infinite );

However, such code does not manipulate these two kernel objects in an atomic way. Therefore, you can change it as follows:

Signalobjectandwait (hmutex, hevent, infinite, false );

 

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.