Windows Programming-synchronization of threads and kernel objects-beacon (semaphore) kernel objects

Source: Internet
Author: User

The beacon kernel object is used to count resources. Like all kernel objects, they contain one usage quantity, but they also contain two other signed 3-2 bits.

One isMaximum number of resourcesTo identify the maximum number of resources that can be controlled by the beacon.

One isCurrent resource quantityTo identify the number of resources currently available.

(Fangsh Note: Some books call it information kernel objects)

To correctly illustrate this problem, let's take a look at how the application uses the beacon. For example, I am developing a server process in which I have allocated a buffer that can be used to store client requests. I hardcoded the buffer size so that it can store up to five client requests at a time. If a new client tries to contact the server when five requests have not been processed, the request of the new client will be rejected and an error will occur, indicating that the server is busy now, the client should try again later.

When my server process is initialized, it creates a thread pool containing five threads, each of which is ready to process when client requests arrive.

At the beginning, no client made any request, so my server does not allow any thread in the thread pool to become a schedulable thread. However, if three client requests come at the same time, three threads in the thread pool should be in the schedulable status. By using the beacon, we can monitor resources and schedule threads well. The maximum number of resources is set to 5, because this is the size of the buffer that I hardcoded. The current resource quantity is initially set to 0 because no client initiates any request. When client requests are accepted, the current number of resources increases. When client requests are submitted to the server's thread pool, the current number of resources decreases.

 

The usage rules of the beacon are as follows:

• If the number of current resources is greater than 0, a beacon signal is sent.

• If the current number of resources is 0, no beacon signal is sent.

• The system will never allow the current number of resources to be negative.

• The current resource quantity cannot exceed the maximum resource quantity.

Note::

When using the beacon object, do notQuantity UsedWith itsCurrent resource quantity.

 

The following functions are used to create a beacon kernel object:

 

 HANDLECreatesemaphore(
PSECURITY_ATTRIBUTE psa, // Security Attribute pointer
LONG lInitialCount, // the initial value of the currently available resource. The value must be greater than or equal to 0 and less than or equal to lMaximumCount.
LONGlMaximumCount, // maximum number of available resources. The value must be greater than 0.
PCTSTRpszName); // Object Name Pointer

 

 

The security attributes and names of kernel objects are two parameters: Psa and pszName.

MaximumcountThe parameter indicates the maximum number of resources processed by an application. Because this is a signed 32-bit value, you can have up to 2 147 483 647 resources.

LinitialcountThe parameter specifies how many resources are available at the beginning (current.

By callingOpensemaphoreFunction, another process can obtain its own Process Handle related to the existing Beacon:

 

 HANDLE OpenSemaphore(
DWORDfdwAccess,
BOOL bInheritHandle,
PCTSTRpszName);

 

 

When my server process is initialized, there is no client request, so I call the following createsemaph ore function:

HANDLE hsem = CreateSemaphore (NULL, 0, 5, NULL );

 

This function creates a beacon. the maximum number of resources is 5, but the resource that can be used at the beginning is 0, so no beacon signal is sent. All threads waiting for the beacon enter the waiting state.

(FangSH Note: When a resource is accessed, the current available resource count is reduced by 1. As long as the current available resource count is greater than 0, a semaphore signal can be sent.
However, when the current available count is reduced to 0, it indicates that the number of threads currently occupying resources has reached the maximum allowed number, and other threads cannot enter
At this time, the semaphore signal will not send a beacon signal.

)

By callingReleasesemaphoreFunction, the thread can increase the number of current resources of the Beacon:

 

 Bool releasesemaphore (
Handle hsem, // semaphore handle
Longlreleasecount, // increase the count
Plongplpreviuscount); // previous count

 

 

This function onlyLreleasecountThe current number of resources added to the beacon. Generally, 1 is passed for the lreleasecount parameter. However, you do not have to pass this value. I often pass 2 or greater values. This function can also* PlpreviuscountReturns the original value of the current resource quantity. In fact, almost no application cares about this value, so it can be passedNull, Ignore it.

 

Summary:

Waitforsingleobject ()AndWaitForMultipleObjects ()This function is mainly used to determine whether the current available resource count of a semaphore allows the entry of a thread. Only when the current available resource count is greater than 0 will the monitored semaphore kernel object be notified.

 

The PSA of the createsemaphore function is null, linitialcount is the initial value of the currently available resources, lmaximumcount is the maximum number of available resources, and pszname is the name. When the value of linitialcount is equal to 0, the beacon object is in the non-signal state, and the kernel puts the thread that calls the waiting function in the sleep state. If the value of linitialcount is greater than 0, the beacon object is in the signal State. At this time, the kernel puts the thread that calls the waiting function in the running state, and reduces the number of currently available resources of the beacon object by 1.

.

The hsem of the releasesemaphore function is the handle of the tag object, lreleasecount is the number of resources to be released, and plpreviuscount returns the original number of available resources. Call this function to add the value of lreleasecount to the current number of available resources. After a thread has accessed available resources, call the releasesemaphore function to increase the number of available resources. To access the same beacon object in different processes, call the createsemaphore function and pass the name of the beacon object to obtain the handle of the beacon object created in other processes. Note that the number of currently available resources of the beacon object is reduced by 1 by default, but the thread may use multiple resources at a time, which may cause problems. To avoid problems, we should follow the principle that one thread only uses one resource.

 

Fangsh 2011-01-04

Related Article

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.