Multithreaded Programming (12)

Source: Internet
Author: User
Tags mutex semaphore thread

There have been two multi-threaded methods of synchronization before:

CriticalSection (critical area) and mutex (mutex), these two synchronization methods are similar, but the scope is different;

The CriticalSection (critical area) is similar to a public toilet with only one squatting position;

A mutex (mutex) object is similar to a baton in a relay race, which can only be held by one person at a time, and who is holding who runs.

What is semaphore (signal or semaphore)?

For example, to the bank to do business, or to the station to buy tickets, originally only a waiter, no matter how many people waiting in line, the business can only come one by one.

If added business window, can accept several business at the same time?

This is similar to the semaphore object, where semaphore can simultaneously handle several threads requested by the wait function (such as: WaitForSingleObject).

Semaphore's working ideas are as follows:

1, first, through the CreateSemaphore (security settings, initial signal number, total number of signals, signal name) to establish the signal object;

Parameter four: Like a Mutex, it can have a name, or it can not, in this case there is no name (nil); Names are generally used across processes.

Parameter three: The total number of signals, is the semaphore maximum processing capacity, just as the bank altogether has how many business window;

Parameter two: The initial signal number, which is like a lot of Bank business window, but open a few may not necessarily, if not open and no the same;

Parameter one: The security setting is the same as before, using the default (nil).

2, to accept the semaphore service (or called coordination) of the thread, the same need to use the wait function (such as: WaitForSingleObject) in line waiting;

3, when a thread to use a signal, should use ReleaseSemaphore (signal handle, 1, nil) to give out the available signals to other threads;

Parameter three: Generally is nil, if give a digital pointer, can accept to at this time (before) total idle how many signals;

Parameter two: Generally 1, which means adding an available signal;

If you want to increase the initial signal when CreateSemaphore, you can also pass ReleaseSemaphore.

4, finally, as the system kernel object, to use CloseHandle shutdown.

Also, in the case where the total number of semaphore is 1, it is the same as the mutex (mutex).

In this example, each click of the button will create a signal with a total of 5 signal objects, the initial signal from EDIT1; There are 5 threads to line up at the same time.

This example also attaches the Delphi Tsemaphore class example, but not too much entanglement in detail, is in order to get the whole idea of multithreading as soon as possible.

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.