Delphi multithreaded Programming (12)--Multithreading synchronization semaphore (signal object)

Source: Internet
Author: User

Reprinted from: In Case of blog

There are two ways to synchronize threads before:

CriticalSection (critical section) and mutex (mutex), the two synchronization methods are similar, but the scope of the different

CriticalSection is similar to a public toilet with only one squat position, which can only be entered

A Mutex object is similar to a baton in a relay race, a moment only one person holds, who runs with whom

  

What is semaphore (signal or semaphore)?

For example, to the bank to do business, or to the station to buy tickets, the original only one waiter, no matter how many people lined up to wait, business can only come

If you add a business window, can you accept several business at the same time?

  This is similar to the Semaphore object, Semaphore can simultaneously process the thread that the wait function (such as: WaitForSingleObject) Requested

Semaphore's working ideas are as follows:

1. First set up the Signal object by CreateSemaphore (security setting, initial semaphore, signal total, signal name)

Parameter four: Like a mutex, it can have a name or not, in this case there is no (nil); a name that is typically used for cross-process

Parameter three: The total number of signals, is the semaphore maximum processing capacity, just like the number of banks there are all the same business window

Parameter two: initial semaphore, which is like a lot of banking business window, but open a few may not necessarily, if not open and not the same

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

2. To accept a semaphore service (or called co-ordination) thread, it is also necessary to wait for a function (for example: WaitForSingleObject) to queue

3. When a thread finishes using a signal, it should use ReleaseSemaphore (signal handle, 1, nil) to make available signals to other threads

Parameter three: generally nil, if given a digital pointer, you can accept the total number of idle signals at this time (before)

Parameter two: typically 1, which indicates an increase in the available signal

If you want to increase the initial signal at CreateSemaphore, you can also use the ReleaseSemaphore

4. Finally, as a system kernel object, use CloseHandle to close

  Also, when the total number of Semaphore is 1, it is the same as the mutex (mutex).

  

In this example, each click of a button will create a signal object with a total signal of 5, the initial signal from Edit1, and 5 threads to queue.

This example also attaches the example of Tsemaphore class in Delphi, but does not dwell too much on the details in order to get the whole idea of multithreading as soon as possible.

Delphi Multithreaded Programming (12)-Multi-threaded synchronization semaphore (signal object)

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.