Windows core programming-thread synchronization (2)

Source: Internet
Author: User

7.Wait for the timer Kernel Object

Wait for the timer kernel object to send its own signal to the kernel object at a certain time or a specified time interval. They are usually used to execute an operation at someone's time.

 

Create a timer kernel object:

 
Handle createwaitabletimer (
 
Psecurity_attributes PSA,
 
Bool fmanualreset, pctstr pszname );

Parameters have been described before and are not described here.

 

After a timer is created, the timer is in the "not notified" status. Note that the timer is always created in the "not notified" status. After the timer is complete, you need to set the time to change the object to the notified status, this requires:SetwaitabletimerFunction:

 

 
Bool setwaitabletimer (
 
Handle htimer,
 
Const large_integer * pduetime,
 
Long lperiod,
 
Ptimerapcroutine pfncompletionroutine,
 
Pvoid pvargtocompletionroutine,
Bool fresume );

 

Where,HtimerThe handle of the timer kernel object.PduetimeAndLperiodIs used together,PduetimerIndicates when the timer is reported for the first time,LperiodIt indicates the interval from which a message is reported. The detailed parameter settings and examples are clearly described in the book.

 

 

 

8. Beacon Kernel Object

The beacon kernel object is a kernel object used to count resources. In additionPSAIn addition to the attribute, there are two signed32Bit Value Attribute. One is the maximum number of resources, and the other is the current number of resources.The maximum number of resources is used to identify the maximum number of resources that can be controlled by the beacon, and the current number of resources is used to identify the number of resources currently available.

When can I use the beacon Kernel Object? For example, when I develop a server process, in which I allocate five buffers that can be used by the client, the server can process them.5Client requests. The maximum number of resources is5, The current resource quantity is0. Currently, there are three customer request buffers, and the server process can allocate three buffers for these three clients. The server creates three threads and puts these three threads in A schedulable state.

 

However, there is a paragraph in the book that is not quite clear:

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.

What is the difference between being accepted and submitting it to the server thread pool? Why does it increase progressively after a while?

The book also pointed out the usage rules of the Beacon:

• 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.

 

Also, do not confuse the number of beacon kernel objects with the current number of resources.

 

Create a beacon kernel object:

 
Handle createsemaphore (
 
Psecurity_attribute PSA,
 
Long linitialcount,
 
Long lmaximumcount, pctstr pszname );

 

By callingReleasesemaphoreFunction to increase the number of current resources:

Bool releasesemaphore (
 
Handle hsem,
 
Long lreleasecount, plong plpreviuscount );

 

 

9.Mutually Exclusive Kernel Object

The mutex kernel object can ensure that the thread has access to a single object. The mutex contains a count and a thread.ID, A recursive counter. Its functions and keyCodeSegments are the same. The difference is that a kernel object belongs to the user mode object, and another difference is that the mutex kernel object allows threads in different processes to access the same resource.

 

Rules for using mutex objects:

If the threadI dYes0(This is invalid.I d), The mutex object is not owned by any thread and sends a notification to the mutex object.

IfI dIs a non-0Number, so a thread has a mutex object and does not send a notification to the mutex object.

Unlike all other kernel objects, Mutex objects have special code in the operating system, allowing them to violate normal rules (this exception will be described later ).

 

Functions of mutex objects:

Create a mutex object:

 
Handle createmutex (
 
Psecurity_attributes PSA,
 
Bool finitialowner,
 
Pctstr pszname );

 

Open the mutex object:

 
Handle openmutex (
 
DWORD fdwaccess,
Bool binherithandle,
 
Pctstr pszname );

 

FinitialownerControls the initial status of mutex objects.FalseIndicates the thread of the mutex object.IDAnd recursive count are set0.It means that the resource is not occupied by any thread, so it must send a notification signal.

IfTrue, Then the object threadIDSet to call its threadID, Recursive count is set1The resource is occupied by this thread and no notification signal is sent.

Once a thread successfully waits for a mutex object, it has exclusive access to the resource, and any thread that wants to access the resource is placed in the waiting state.

When a thread does not need access to this resource, the mutex object must be released:

Bool releasemutex (handle hmutex );

 

Mutex objects are different from other objects:

Because it has the concept of thread ownership. No object can remember which thread successfully waits for the object, and only the mutex object can keep tracking.

 

Two tables are listed in the table. One is the difference between the mutex object and key code, and the other is the fast query table of the thread synchronization object. Useful,CopySo far:

Table 9-1 Comparison Between mutex objects and key code segments

Features

Mutex object

Key code segment

Running Speed

Slow

Fast

Whether it can be used across process boundaries

Yes

No

Statement

Handle hctx;

Critical_section CS;

Initialization

H m T x = C r e a t e m u t e x(N u L,Fa L S E,N u L);

I n I t I a l I z e c r I t I c a l e c t I o n (& E S);

Clear

C l o s e h a n d l e(H m T x);

D e l e t e c r I I c a l s e c t I o n(& C S);

Unlimited waiting

Wa I t f o r s I n g l e o B j e c t(H m t x, I n f I n I t e);

E n t e r c r I I c a l s e c t I o n(& C S);

0Wait

Wa I t f o r s I n g l e o B j e c t tr y(H m t x, 0);

E n t e r c r I I c a l s e c t I o n(& C S);

Arbitrary waiting

Wa I t f o r s I n g l e o B j e c t(H m T x, d w m I l I s e c o n d S);

No

Release

R e l e a s e m u t e x(H m T x);

L e a v e c r I t I c a l s e c t I o n(& C S);

Can I wait for other kernel objects?

Yes (UseWa I t f o r m u l t I p L e o B j e c T SOr similar functions)

No

Table9-2 Relationship between kernel objects and thread synchronization

Object

When is not notified

When is in the notified status

Side effects of waiting for success

Process

When the process is still active

When the process stops running (E x I t p r o C E S,Te r m I n a T E P R o C E S)

None

Thread

When the thread is still active

When the thread stops running (E x I t h r e a d,Te r m I n a t e t h r e a d)

None

Job

When the job time has not ended

When the job time has ended

None

File

WhenI/OWhen the request is being processed

WhenI/OWhen the request is processed

None

Console input

No input exists

When input exists

None

File Modification notification

No files modified

When the file system finds the modification

Reset notification

Auto reset event

R e s e t e v e n t, p u l s e-e v e n tOr wait for success

WhenS e t e v e n t/p u l s e v e n tHour

Reset event

Manual reset event

R e s e t e v e n tOrP u l s e v e n t

WhenS e t e v e n t/p u l s e v e n tHour

None

Automatic Reset wait Timer

C a n c e l wa I t a B L E ti m e rOr wait for success

When the time (S e t wa I t a B L E ti m e r)

Reset timer

Manual reset wait Timer

C a n c e l wa I t a B L E ti m e r

When the time (S e t wa I t a B L E ti m e r)

None

Beacon

Wait for success

When quantity> 0Hour (R e l e a s e m a p H o r e)

Decrease in quantity1

Mutex object

Wait for success

When not owned by a thread (R E L E A S EMutex object)

Assign ownership to the thread

key code segment (user mode)

wait for Success ( tr Y ) e n t e r c r I t I c a l s e c t I o n )

when not owned by a thread ( l e a v e c R I t I c a l s e c t I o n )

assign ownership to the thread

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.