forked from tanhaogg/THWebService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTHDispatchQueue.m
More file actions
42 lines (36 loc) · 967 Bytes
/
THDispatchQueue.m
File metadata and controls
42 lines (36 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// THDispatchQueue.m
// ApplicationStatus
//
// Created by Hao Tan on 12-4-11.
// Copyright (c) 2012年 http://www.tanhao.me All rights reserved.
//
#import "THDispatchQueue.h"
#import "THWebDefine.h"
@implementation THDispatchQueue
- (id)initWithConcurrentCount:(int)count
{
self = [super init];
if (self)
{
if (count < 1) count = kTHDispatchQueueDefaultConcurrentCount;
semaphore = dispatch_semaphore_create(count);
queue = dispatch_queue_create(NULL, NULL);
}
return self;
}
- (id)init
{
return [self initWithConcurrentCount:kTHDispatchQueueDefaultConcurrentCount];
}
- (void)addOperation:(THQueueOperationBlock)block
{
dispatch_async(queue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
block();
dispatch_semaphore_signal(semaphore);
});
});
}
@end