资源互斥问题,这种情况有没有必要加锁?

_任我行_ 2012-12-19 11:00:23
有一个全局变量,只有任务A会写该变量,其它任务都只会读该变量,请问像这种情况有没有必要加锁保护,或是其它的互斥手段。
...全文
2815 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
holymaple 2013-01-04
  • 打赏
  • 举报
回复
好多大神,学习鸟~
良少 2013-01-03
  • 打赏
  • 举报
回复
变量长度在long类型以下的,不用加锁。 因为CPU在写数据时,会自动锁住内存总线,其他线程根本不可能修改该变量。 如果长度比long类型长,cpu写数据时,需要多次对内存写,此时如果其他线程同时读取数据,就可能读取到中间阶段的错误数据。
柔情铁汉h 2012-12-29
  • 打赏
  • 举报
回复
引用 13 楼 nice_cxf 的回复:
引用 11 楼 yuahua_hust 的回复:引用 9 楼 nice_cxf 的回复:一般来说要加,实际这是读写锁的典型应用,另外如果你全局变量是int类型的,并且修改时不需要读也不用加 能具体点吗?如果不加会怎样?只有一个任务会去修改该变量,那么它修改的时候,读不读,有什么要紧的。 sry,搞错了,只要是int就不用加锁
明显操作系统没学好,还是赵老师靠谱点
qiangorqiang 2012-12-26
  • 打赏
  • 举报
回复
引用 18 楼 ForestDB 的回复:
不怕脏就无所谓。
yujie_v 2012-12-26
  • 打赏
  • 举报
回复
看具体应用,你只要保证原子写就可以。 不加锁,容易引起读写状态不一致。同一时间既在读也在写。
human8848 2012-12-24
  • 打赏
  • 举报
回复
这个问题提的就不好,太宽泛了。首先,一般情况肯定要加锁。当然了,偶尔你的应用有特殊情况,有的时候不加锁也可以跑的好好的,也有。总之,具体问题具体分析,是加互斥量,条件变量,信号量,还是读写锁,抑或顺序锁,都要针对你的应用。
完美世界ssrs 2012-12-23
  • 打赏
  • 举报
回复
小白路过,支持下
边走边瞧 2012-12-21
  • 打赏
  • 举报
回复
引用 30 楼 kaizi318 的回复:
加不加锁,主要是看你这个变量或函数 是不是可重入的。。。。 不可重入,不管你是什么类型,都要加。。。。。 windows的一般都不是线程安全的,后来微软多加了一些函数,如后缀为_s的函数,表示线程安全或什么安全!
红色部分好象不对吧?比如sprintf。抄段MSDN给你看: Using sprintf, there is no way to limit the number of characters written, which means that code using sprintf is susceptible to buffer overruns.
keivin2006 2012-12-21
  • 打赏
  • 举报
回复
加不加锁,主要是看你这个变量或函数 是不是可重入的。。。。 不可重入,不管你是什么类型,都要加。。。。。 windows的一般都不是线程安全的,后来微软多加了一些函数,如后缀为_s的函数,表示线程安全或什么安全!
边走边瞧 2012-12-21
  • 打赏
  • 举报
回复
引用 26 楼 zhao4zhong1 的回复:
请问楼上,如果一条汇编指令不用加锁的话,要Lock指令干吗?
你楼上的兄弟说得没错,抄一段MSDN给你看。 Interlocked Variable Access Applications must synchronize access to variables that are shared by multiple threads. Applications must also ensure that operations on these variables are performed atomically (performed in their entirety or not at all.) Simple reads and writes to properly-aligned 32-bit variables are atomic operations. In other words, you will not end up with only one portion of the variable updated; all bits are updated in an atomic fashion. However, access is not guaranteed to be synchronized. If two threads are reading and writing from the same variable, you cannot determine if one thread will perform its read operation before the other performs its write operation. Simple reads and writes to properly aligned 64-bit variables are atomic on 64-bit Windows. Reads and writes to 64-bit values are not guaranteed to be atomic on 32-bit Windows. Reads and writes to variables of other sizes are not guaranteed to be atomic on any platform.
边走边瞧 2012-12-21
  • 打赏
  • 举报
回复
引用 26 楼 zhao4zhong1 的回复:
请问楼上,如果一条汇编指令不用加锁的话,要Lock指令干吗?
你楼上的兄弟说得没错,抄一段MSDN给你看。 Interlocked Variable Access Applications must synchronize access to variables that are shared by multiple threads. Applications must also ensure that operations on these variables are performed atomically (performed in their entirety or not at all.) Simple reads and writes to properly-aligned 32-bit variables are atomic operations. In other words, you will not end up with only one portion of the variable updated; all bits are updated in an atomic fashion. However, access is not guaranteed to be synchronized. If two threads are reading and writing from the same variable, you cannot determine if one thread will perform its read operation before the other performs its write operation. Simple reads and writes to properly aligned 64-bit variables are atomic on 64-bit Windows. Reads and writes to 64-bit values are not guaranteed to be atomic on 32-bit Windows. Reads and writes to variables of other sizes are not guaranteed to be atomic on any platform.
边走边瞧 2012-12-21
  • 打赏
  • 举报
回复
抛开加锁与不加锁会有什么后果不谈,问一下LZ:如果读值的线程读不到最新的数值,它们的行为是预期的结果吗?感觉你线程模型设计有问题。 比如A线程在写一个int,B和C去读。按照常规的思路,既然A去写这个int了,那么这个int一定有特定含义。一般要等A写好了数据,B和C才能读int的值,不然多数情况下,B和C表现出的行为并不是你想要的。 可以考虑InterlockedAdd函数,系统开销很小,代码量也不大。
赵4老师 2012-12-21
  • 打赏
  • 举报
回复
请问楼上,如果一条汇编指令不用加锁的话,要Lock指令干吗?
wzb56 2012-12-21
  • 打赏
  • 举报
回复
keivin2006 2012-12-21
  • 打赏
  • 举报
回复
引用 31 楼 go_and_see 的回复:
引用 30 楼 kaizi318 的回复:加不加锁,主要是看你这个变量或函数 是不是可重入的。。。。 不可重入,不管你是什么类型,都要加。。。。。 windows的一般都不是线程安全的,后来微软多加了一些函数,如后缀为_s的函数,表示线程安全或什么安全! 红色部分好象不对吧?比如sprintf。抄段MSDN给你看: Using sprintf, there i……
恩,对,有点问题,但也没有错,应该是数据安全。。。
WizardOz 2012-12-20
  • 打赏
  • 举报
回复
我记得有一种锁叫读写锁,读和读之间不会互斥,读和写,写和写之间会互斥。
book_3dgame 2012-12-20
  • 打赏
  • 举报
回复
如果是原子操作,也就是一条CPU MOV指令能搞定基本数据类型,不用中锁,因为不可能出现时钟周期内部分bit被修改,要么此变量的bit全被修改,要么全不被修改 所以不是大多数与不大多数的问题,先查看你那条修改变量的汇编指令,不能一步搞定的,就必须加锁,能一步搞定的,再加锁,不但是脱裤子放屁,而且影响效率,加锁是有代价的,研究一下操作系统、多级cache及CPU结构就知道了
sxcong 2012-12-20
  • 打赏
  • 举报
回复
大多数情况都不用加锁 在开源代码中,或各种书上,经常可见: while(!stop) { do.... } 在其他地方,退出的时候设置stop = 1; 当然,这里的while多执行两次不影响大局。
nice_cxf 2012-12-19
  • 打赏
  • 举报
回复
int写命令汇编是单条指令,所以不需要加,如果是数组或double类型,汇编不是单条指令,可能写一部分后被其他线程读,这样就有问题了
_任我行_ 2012-12-19
  • 打赏
  • 举报
回复
引用 14 楼 tianweishuiguo 的回复:
应该需要加锁吧 for example: int a = 0; 若没加锁,写的时候: mov r0, [r1] //如果此时来读,会读不到最新的值噢! add r0, r0, #0x02 str r0, [r1]
悲剧,看不懂汇编,能解释一下吗?
加载更多回复(20)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧