当前位置:网站首页>Swift GCD Notify after concurrent execution Lock barrier
Swift GCD Notify after concurrent execution Lock barrier
2022-06-09 22:11:00 【HandsomeDanielWu】








nslock
let queue = DispatchQueue(label: "Queue1", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
let queue2 = DispatchQueue(label: "queue2", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
let lock = NSLock()
var array = Array(0...10000)
func getLastItem() -> Int?{
lock.lock()
var temp: Int? = nil
let count = array.count
if count > 0{
temp = array[count - 1]
}
lock.unlock()
return temp
}
func removeLastItem(){
lock.lock()
array.removeLast()
lock.unlock()
}
queue.async {
for _ in 0 ..< 1000{
removeLastItem()
}
}
queue2.async {
for _ in 0..<1000{
print(getLastItem() ?? 0)
}
}Lock wastes too much perfomance in the case of very few write opertions. Use Barrier instead.
Barrier will not execute until other threads finished executing
let queue = DispatchQueue(label: "Queue1", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
let queue2 = DispatchQueue(label: "queue2", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
let arrayQueue = DispatchQueue(label: "arrayQueue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
var array = Array(0...1000)
func getLastItem() -> Int?{
return arrayQueue.sync {
if array.count > 0 {
return array[array.count - 1]
}
return -1
}
}
func removeLastItem(){
let workItem = DispatchWorkItem(qos: DispatchQoS.default, flags: DispatchWorkItemFlags.barrier) {
array.removeLast()
}
arrayQueue.async(execute: workItem)
}
queue.async {
for _ in 0 ..< 1000{
removeLastItem()
}
}
queue2.async {
for _ in 0..<1000{
print(getLastItem() ?? 0)
}
}边栏推荐
- [Title brushing] longest increasing subsequence
- Analyzing native crash using addr2line
- Find My产品|新款TWS耳机支持Find My功能,苹果Find My在第三方厂家应用更广泛
- GUI user login
- [reprint] six aspects of async/await that are superior to promise
- 【刷题篇】跳跃游戏
- Im instant messaging development: mobile protocol UDP or TCP?
- lua学习笔记(4)-- 搭建mobdebug 远程开发环境
- 这家估值25亿的公司,破产了?
- 还在怀疑数字藏品么?国家队都开始入局了
猜你喜欢

Implementing Lmax disruptor queue from scratch (II) analysis of consumption dependency principle among multiple consumers and consumer groups

持续集成、持续交付、持续部署(CI/CD)详细介绍

【滤波器】基于时变维纳滤波器实现语音去噪含Matlab源码

Find My产品|新款TWS耳机支持Find My功能,苹果Find My在第三方厂家应用更广泛

Leetcode(力扣)超高频题讲解(一)

At present, I am 28 years old, and I have successfully won the 15K salary since I started to wander to the north in 20 years and started to test the entry software

【刷题篇】最长递增子序列

AI助力,释放法务势能——法大大iTerms合同智审系统正式发布

MongoDB的使用及CRUD操作

Continuous integration, continuous delivery and continuous deployment (ci/cd) details
随机推荐
这家估值25亿的公司,破产了?
ERC20协议API接口规范
【图像去噪】基于高斯、均值、中值、双边滤波实现图像去噪含Matlab源码
Who is the slowest child in C language test 169
Is it time to buy a house now that the mortgage interest rate has been lowered?
常见SQL语句
Block certification in Mina
常见的嵌入式端流媒体服务器开源项目!
M-Arch(雅特力M4)【AT-START-F425测评】No.06 驱动段码LCD
[BP prediction] BP neural network based on AdaBoost realizes data regression prediction with matlab code
先睹为快!Benji Bananas 第一季奖励活动数据一览!
Fire tongs Liu Ming ~ a little interview suggestion summarized from the three-year small test of Dai Zhuan
MKDIR create directory command
After the naked resignation in the post epidemic era, the interview with the software test engineer was rejected. Write down some insights and interview experiences
[question brushing] jumping game
Web3 in the distant future? No, it has come!
M-arch (yateli M4) [at-start-f425 evaluation] No.05 flash
Laravel upload file information acquisition
如何实现高效的IM即时通讯长连接自适应心跳保活机制
Some thoughts on matrix chain of design memo solution (dynamic programming method)