seem don't wait the end of the group
let oneSem_1 = DispatchSemaphore(value: 1)
let semaphore = DispatchSemaphore(value: 4)
let semaphoreEND = DispatchSemaphore(value: 0)
var p=0
let group_2 = DispatchGroup()
var t:[Int]=[]
let MAX=1000000
for _ in 0..<MAX {
group_2.enter()
DispatchQueue.global().async{
//group_2.enter()
semaphore.wait()
oneSem_1.wait()
p+=1
t.append(p)//car ressource critique, sinon pas rempli à fond
oneSem_1.signal()
if p == MAX{
print("p == MAX")
semaphoreEND.signal()
}
semaphore.signal()
//group_2.leave()
}
group_2.leave()
}
group_2.wait()
// semaphoreEND.wait()
// while(p != MAX){
// usleep(1_00_000)
// print("p=",p)
// }
print("END p=\(p) t.count=\(t.count)")
I expect the output of ND p=1000000 t.count=1000000
I can get this result if i uncomment // semaphoreEND.wait()
, but the actual output is
END p=999871 t.count=999881
Other problem : t.count != p
With Group i expect the end of all task. Why i have to uncomment // semaphoreEND.wait() ?
Thanks
Project here for download : https://github.com/fredOnGitHub/semaphore_to_modify_1
AGAIN ANOTHER SOLUTION FOR THIS PROBLEM (without waiting group)
using Apple Processes and Threads > RunLoop : : according to the council of vadian
See waiting until the task finishes , Fundation > Processes-And-Threads > RunLoop
REMARQUE:
here Apple say "The RunLoop class is generally not considered to be thread-safe and its methods should only be called within the context of the current thread. You should never try to call the methods of an RunLoop object running in a different thread, as doing so might cause unexpected results."
and give an example. Here the changes are :
I would like to know the best between both these another solutions because even if you have more variable it is herited from Threads > RunLoop