Synchronization of threads to kernel objects-2

Source: Internet
Author: User
Tags filetime

Wait for timer kernel event
CreateWaitableTimer (
Psecurity_attributes PSA,
BOOL Fmanualreset,
Pctstr pszname);
The process can get its own handle to the existing waiting timer associated with the process.
HANDLE Openwaitabletimer (
DWORD dwDesiredAccess,
BOOL bInheritHandle,
Pctstr pszname);

The wait timer object is always created in the non-notification state, and you must call the SetWaitableTimer function to tell the timer when you want to let
It becomes the notified state.
BOOL SetWaitableTimer (
HANDLE Htimer,//Timer
Const Large_integer *pduetime,//pduetime and lperiod, used to indicate when the timer should be the first time to chime
Long lperiod,//indicates how long the timer should be time to chime.
Ptimerapcroutine Pfncompletionroutine,
PVOID Pvargtocompletionroutine,
BOOL fresume);

FILETIME and Large_integer
The FILETIME begins with a 32-bit boundary.
The Large_integer begins with a 64-bit boundary.
The system moves the value to consider the alignment problem.

Allow the wait timer to queue APC.

Beacon Kernel Object
If the current number of resources is greater than 0, a beacon signal is emitted.
If the current number of resources is 0, no beacon signal is emitted.
The system never allows the number of current resources to be negative.
The current number of resources must never be greater than the maximum number of resources.
Do not confuse the number of beacons with the number of its current resources.
HANDLE Createsemphore (
Psecurity_attribute PSA,
LONG lInitialCount,
LONG lMaximumCount,
Pctstr pszname);

HANDLE OpenSemaphore (
DWORD fdwaccess,
BOOL bInheritHandle,
Pctstr pszname);

ReleaseSemaphore (
HANDLE Hsem,
LONG lReleaseCount,
Plong Plpreviouscount)

Beacons can perform test and setup operations in an atomic manner. Equivalent to relase the number +1, while the side effect of successfully waiting for the beacon is that its number is decreasing by 1

Mutex Object Kernel Object
A mutex object contains a usage quantity, a thread ID, and a recursive counter.
The mutex is slower than the key code (the former is a kernel object) but can cross-process

Rules:
If the thread ID is 0, the mutex is not owned by any thread and emits a notification signal for the mutex object.
If the ID is a non-0 number, then a thread has a mutex and does not emit a notification signal for that mutex object.
Unlike all other kernel objects, mutex objects have special code in the operating system that allows them to violate normal rules.
HANDLE CreateMutex (
Psecurity_attributes PSA,
BOOL Finitialowner,
Pctstr pszname);
With OpenMutex, another process can get its own handle on the process associated with the existing mutex object
HANDLE OpenMutex (
DWORD fdwaccess,
BOOL bInheritHandle,
Pctstr pszname);

The Finitialowner controls the initial state of the mutex object, and if set to True, the thread ID is set to the calling thread ID, and the recursive counter is set to 1.
If False, both the thread ID and the recursive counter are set to 0, which means that the mutex is not owned by any thread and therefore emits its notification signal.

By invoking a wait function and passing a handle to the mutex that is responsible for securing the resource, the thread is able to gain access to the shared resource.
Internally, the wait function checks the ID of the thread to see if it is 0, and if the thread ID is 0, then the thread ID is set to the calling thread ID.
The recursive count is set to 1, and the calling thread remains in a scheduled state.
If the wait function Discovery ID is not 0, then the calling thread enters the wait state, and the system remembers the condition, and the ID of the mutex
0 o'clock, set the thread ID to wait for the thread ID, set the recursive counter to 1, and allow the waiting thread to become a scheduled thread again.
Both inspection and modification are carried out in an atomic manner.
In special cases, a thread tries to wait for an unnamed mutex object, the system views the request thread ID and the thread ID recorded in the mutex object
Similarly, the system allows the thread to remain in a scheduled state even if the mutex is in an unnamed state. Recursive counter plus 1.

BOOL Relasemutex (HANDLE Humtex);
The recursive counter minus one, when the recursive counter is 0, the thread ID resets 0, and the object becomes the notification state.

Also check that the thread ID is the same when you release it.
When the object is disposed, the thread that owns the mutex is terminated, and the mutex is considered to have been discarded.
reinitialization.
The difference is that the wait function gets a return value that is not wait_object_0 but wait_abandoned.

Synchronization of threads to kernel objects-2

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.