I am new to swift and I am trying my hands with multithreading, a concept which doesn't seem to be famous in swift. Based on this example of java's synchronized
implementation I tried to do the same for swift based on examples given for swift on another SO post. Here is my implementation:
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
public class TestThread {
var name : String;
var theDemo : TheDemo;
init(_ name : String, _ theDemo : TheDemo) {
self.theDemo = theDemo;
self.name = name;
run()
}
public func run() {
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
self.theDemo.testSynced(self.name)
}
}
}
}
public class TheDemo {
private func synced(_ lock : Any, _ name : String, _ closure : (_ name : String) -> ()){
objc_sync_enter(lock)
defer { objc_sync_exit(lock) }
closure(name)
}
public func testSynced(_ name : String){
synced(self, name, test)
}
public func test(_ name : String) {
for i in 0..<4 {
let str = "\(name) :: \(i)"
let theDeadline = DispatchTime.now() + .seconds(i/2)
DispatchQueue.main.asyncAfter(deadline: theDeadline, execute: {
print(str)
})
}
}
}
var theDemo = TheDemo()
TestThread("THREAD 1", theDemo)
TestThread("THREAD 2", theDemo)
TestThread("THREAD 3", theDemo)
TestThread("THREAD 4", theDemo)
When I run the above code in the playground the result I get looks like the following
THREAD 1 :: 0
THREAD 1 :: 1
THREAD 2 :: 0
THREAD 2 :: 1
THREAD 3 :: 0
THREAD 3 :: 1
THREAD 4 :: 0
THREAD 4 :: 1
THREAD 1 :: 2
THREAD 1 :: 3
THREAD 2 :: 2
THREAD 2 :: 3
THREAD 3 :: 2
THREAD 3 :: 3
THREAD 4 :: 2
THREAD 4 :: 3
But I was expecting a result that looks like the following.
THREAD 1 :: 0
THREAD 1 :: 1
THREAD 1 :: 2
THREAD 1 :: 3
THREAD 2 :: 0
THREAD 2 :: 1
THREAD 2 :: 2
THREAD 2 :: 3
THREAD 3 :: 0
THREAD 3 :: 1
THREAD 3 :: 2
THREAD 3 :: 3
THREAD 4 :: 0
THREAD 4 :: 1
THREAD 4 :: 2
THREAD 4 :: 3
I wish to understand what I am doing wrong. Have I set my expectation also on the right path considering the concept of synchronisation and swift threads. I appreciate the help. thanks.
EDIT
Since I believe that I am misunderstood, I want to explain a bit more of what I want to do. What I basically want to achieve is a bit different than what I have written here in the code. This is a simplified version. I want to test it if synchronized
works in a simple example and then I want to use it in another project. I am translating a project written for android to ios. In java multithreading I am using synchronized
so that only one thread at a time can use that function. let's say there is a variable or a function that multiple threads can work on. When I put synchronized before the function name or the variable name, threads will have a lock. Now in my project I have several threads, that come and go, based on some sensor input - and before they die they make use of a common shared function. They could come at any point. For several reasons, we set that common function synchronized
so that no two threads enter the function at the same time. Thus, when I am translating the code I looked for what would be similar in swift and I found one of the links that is linked to this question. And I tried to use that - and it didn't work for me. To err is human - and I probably have made a mistake somewhere. But I have spent time to read based on the schedule I have. (I partly said that multithreading isn't very famous in swift because of some places I have read for example this)