定时锁定GCD?(Timed locks in GCD?)
使用案例 :
- 应该处理很多请求。
- 处理每个请求所花费的时间各不相同。
- 请求同时处理(现在它们被推送到
dispatch_get_global_queue
的全局队列中)。- 来自同一客户端的请求优选地应该同步处理,例如,应该在开始处理rca2之前完成对来自ca的rca1的处理,同时处理rcb1 , rcd3等。
rca i :来自客户的请求a。- 如果处理请求花费的时间太长,即超过
TIME_OUT
,则来自同一客户端的其他请求可以开始处理。当rca1处理完成但尚未到达
void dispatch_after( dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block)
使用dispatch_get_global_queue
和串行队列void dispatch_after( dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block)
对于每个客户端将是次优的。每个客户拥有一个串行队列不会满足5。
我知道GCD是基于不同的范例,但在GCD中是否有任何等效的pthread_mutex_timedlock或lockBeforeDate或Lock.tryLock ?
如何使用GCD实现定时锁提供的功能?
Use case:
- There is a lot of requests that should be processed.
- The time taken to process each request varies.
- Request are meant to be processed concurrently (now they are pushed into the global queue got by
dispatch_get_global_queue
).- Requests coming from the same client preferably should be processed synchronously, e.g., processing of rca1 from ca should be finished before start processing rca2, at the same time rcb1, rcd3, etc, are being processed.
rca i : requests from client a.- If processing a request is taking too long, i.e., more than
TIME_OUT
, other requests from the same client can start processing.Using
dispatch_get_global_queue
and a serial queuevoid dispatch_after( dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block)
for each client would be suboptimal when rca1 processing is finished butwhen
has not arrived yet.Having a serial queue per customer wouldn't satisfy 5.
I'm aware that GCD is based on a different paradigm but is there any equivalent of pthread_mutex_timedlock or lockBeforeDate or Lock.tryLock in GCD?
How to implement the functionality provided by timed locks using GCD?
原文:https://stackoverflow.com/questions/13478496