Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf2bd619

Browse files
author
Alex Belozierov
committed
- refactor PThreadPool
- fix deinitialization of PThreadPool
1 parent8d2c7c4 commitf2bd619

File tree

3 files changed

+23
-39
lines changed

3 files changed

+23
-39
lines changed

‎PosixDispatch/DispatchQueue/PDispatchConcurrentQueue.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PDispatchConcurrentQueue: PDispatchQueueBackend {
3333
privateletthreadPool:PThreadPool
3434

3535
privatefunc performAsync(){
36-
threadPool.async(block: asyncBlock)
36+
threadPool.perform(block: asyncBlock)
3737
}
3838

3939
privatefunc asyncBlock(){

‎PosixDispatch/DispatchQueue/PDispatchQueue.swift‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ class PDispatchQueue: PDispatchQueueBackend {
5353

5454
staticfunc concurrentPerform(iterations:Int, execute work:@escaping(Int)->Void){
5555
letgroup=PDispatchGroup(count: iterations)
56-
letblocks=(0..<iterations).map{ iin{work(i); group.leave()}}
5756
letpool=PThreadPool(count:Sysconf.processorsNumber)
58-
pool.async(blocks:blocks)
57+
pool.perform(blocks:(0..<iterations).map{ iin{work(i); group.leave()}})
5958
group.wait()
60-
// pool.deallocate() //fix crash
6159
}
6260

6361
}

‎PosixDispatch/PThreadPool.swift‎

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@
88

99
classPThreadPool{
1010

11-
typealiasBlock=()->Void
11+
typealiasBlock=PThread.Block
1212

1313
staticletglobal=PThreadPool(count:64)
14+
1415
privateletthreads:[PThread],condition=PCondition()
1516
privatevarqueue=FifoQueue<Block>()
16-
privatevarperform=true
1717

1818
init(count:Int){
19-
unownedvarpool:PThreadPool!
20-
varstarted=0
21-
threads=(0..<count).map{ _inPThread{ pool.threadBlock(started:&started)}}
19+
letgroup=PDispatchGroup(count: count)
20+
weakvarpool:PThreadPool?
21+
threads=(0..<count).map{ _in
22+
PThread{
23+
group.leave()
24+
pool?.condition.lock()
25+
whilelet condition= pool?.condition{
26+
condition.wait()
27+
pool?.runloop()
28+
}
29+
}
30+
}
2231
pool=self
2332
threads.forEach{ $0.start()}
24-
waitStart(started: started)
25-
}
26-
27-
privatefunc waitStart(started:@autoclosure()->Int){
28-
condition.lockedPerform{ condition.wait(while:started()!= threads.count)}
33+
group.wait()
2934
}
3035

3136
@inlinablevarthreadCount:Int{
@@ -34,36 +39,24 @@ class PThreadPool {
3439

3540
// MARK: - RunLoop
3641

37-
privatefunc threadBlock(started:inoutInt){
38-
condition.lock()
39-
started+=1
40-
condition.broadcast()
41-
condition.wait(while: started!= threads.count)
42-
runloop()
43-
}
44-
4542
privatefunc runloop(){
46-
while perform{
47-
condition.wait()
48-
whilelet block= queue.pop(){
49-
condition.unlock()
50-
block()
51-
condition.lock()
52-
}
43+
whilelet block= queue.pop(){
44+
condition.unlock()
45+
block()
46+
condition.lock()
5347
}
54-
condition.unlock()
5548
}
5649

5750
// MARK: - Perfrom Block
5851

59-
funcasync(blocks:[Block]){
52+
funcperform(blocks:[Block]){
6053
condition.lock()
6154
queue.push(blocks)
6255
condition.broadcast()
6356
condition.unlock()
6457
}
6558

66-
funcasync(block:@escapingBlock){
59+
funcperform(block:@escapingBlock){
6760
condition.lock()
6861
if queue.isEmpty{
6962
queue.push(block)
@@ -74,11 +67,4 @@ class PThreadPool {
7467
condition.unlock()
7568
}
7669

77-
func deallocate(){
78-
condition.lock()
79-
perform=false
80-
condition.broadcast()
81-
condition.unlock()
82-
}
83-
8470
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp