Using Dispatch Semaphores to Regulate the Use of Finite Resources
来源:互联网 发布:网络流行语大全 编辑:程序博客网 时间:2024/05/18 01:38
The semantics for using a dispatch semaphore are as follows:
When you create the semaphore (using thedispatch_semaphore_createfunction), you can specifya positive integer indicating the number of resources available.
In each task, calldispatch_semaphore_waitto 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_signalfunction.
// Create the semaphore, specifying the initial pool sizedispatch_semaphore_t fd_sema = dispatch_semaphore_create(getdtablesize() / 2);// Wait for a free file descriptordispatch_semaphore_wait(fd_sema, DISPATCH_TIME_FOREVER);fd = open("/etc/services", O_RDONLY);// Release the file descriptor when doneclose(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, thedispatch_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, thedispatch_semaphore_signalfunction 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.
- Using Dispatch Semaphores to Regulate the Use of Finite Resources
- Solution to the Producer-Consumer problem using Semaphores
- A Guide to Blocks & Grand Central Dispatch (and the Cocoa API's making use of them)
- How to enable the use of 'Ad Hoc Distributed Queries' by using sp_configure
- niagara编译错误:using type arguments to access generic types requires the use of "-source 1.5"
- How to use the function of assembly.
- Effective Objective-C 2.0: Item 44: Use Dispatch Groups to Take Advantage of Platform Scaling
- the right syntax to use near USING BTREE错误解决办法
- the resources of meteor
- Interpretation of jewelry in the use of color to l
- Use string::npos to indicate ``the rest of the string;''
- Use javascript to detect the type of the file
- The log use to debug the function of ChangeTemplate
- mongodb 避免Out of semaphores to get db connection error
- use ItemCreated to set the page style of DataGrid
- How to use Events in the Context of C#
- Use DLL to smash the constraint of C++ Class
- How to use "man" effectively in the development of Linux
- Quicklz压缩算法
- Android 自定义横向ListView实现无限级菜单导航类目
- MSN(Windows Live Messenger)即将正式寿终正寝
- linux记录用户操作命令日志
- 使用科大讯飞的语音技术实现语音识别
- Using Dispatch Semaphores to Regulate the Use of Finite Resources
- MongoDB技术博客翻译系列之 -- Replica Set 选举
- 面试手写简单的string
- php __autoload() 方法用于自动加载类
- Spring依赖注入的三种方式
- hibernate annotation
- gcc 的内存对齐
- Html5的placeholder属性(IE兼容)
- 安装x264