Using Dispatch Semaphores to Regulate the Use of Finite Resources
2014-08-29 14:42
330 查看
The semantics for using a dispatch semaphore are as follows:
When you create the semaphore (using the
dispatch_semaphore_create
function), you can specifya positive integer indicating the number of resources available.
In each task, call
dispatch_semaphore_wait
to wait on the semaphore.
When the wait call returns, acquire the resource and do your work.
When you are done with the resource, release it and signal the semaphore by calling the dispatch_semaphore_signal
function.
When you create the semaphore, you specify the number of available resources. This value becomes the initialcount variable for the semaphore. Each time you wait on the semaphore, the
dispatch_semaphore_waitfunction decrements that count variable by 1. If the resulting value
is negative, the function tells the kernel toblock your thread. On the other end, the
dispatch_semaphore_signal
function increments the countvariable by 1 to indicate that a resource has been freed up. If there are tasks blocked and waiting for a resource,one of them is subsequently unblocked and allowed to do
its work.
When you create the semaphore (using the
dispatch_semaphore_create
function), you can specifya positive integer indicating the number of resources available.
In each task, call
dispatch_semaphore_wait
to wait on the semaphore.
When the wait call returns, acquire the resource and do your work.
When you are done with the resource, release it and signal the semaphore by calling the dispatch_semaphore_signal
function.
// Create the semaphore, specifying the initial pool size dispatch_semaphore_t fd_sema = dispatch_semaphore_create(getdtablesize() / 2); // Wait for a free file descriptor dispatch_semaphore_wait(fd_sema, DISPATCH_TIME_FOREVER); fd = open("/etc/services", O_RDONLY); // Release the file descriptor when done close(fd); dispatch_semaphore_signal(fd_sema);
When you create the semaphore, you specify the number of available resources. This value becomes the initialcount variable for the semaphore. Each time you wait on the semaphore, the
dispatch_semaphore_waitfunction decrements that count variable by 1. If the resulting value
is negative, the function tells the kernel toblock your thread. On the other end, the
dispatch_semaphore_signal
function increments the countvariable by 1 to indicate that a resource has been freed up. If there are tasks blocked and waiting for a resource,one of them is subsequently unblocked and allowed to do
its work.
相关文章推荐
- niagara编译错误:using type arguments to access generic types requires the use of "-source 1.5"
- How to enable the use of 'Ad Hoc Distributed Queries' by using sp_configure
- A Guide to Blocks & Grand Central Dispatch (and the Cocoa API's making use of them)
- attempting to bokeyaunrun eclipse useing the jre instead of jdk,to run eclipse using
- How to change the background color of form using C++ - 如何用C++改变窗体的颜色
- How to find the correct version of JDeveloper to use with eBusiness Suite 11i or Release 12.x
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- (NOT CLEAR)Question 21: Which of the following statements provide a valid reason not to use RTTI for distributed (i.e. networked
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- Item 39: Use Dynamic to Leverage the Runtime Type of Generic Type Parameters(Effective C#)
- Are you looking for a way to get the entire text of a word document into a RichEdit without using the Clipboard?
- use ItemCreated to set the page style of DataGrid
- How to use Events in the Context of C#
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- How to use "man" effectively in the development of Linux
- Photoshop CS2 RAW : Using Adobe Camera Raw, Bridge, and Photoshop to Get the Most out of Your Digita
- A step-by-step guide to the use of the Intel OpenCV library and the Microsoft DirectShow technology
- Use javascript to detect the type of the file
- How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock