我需要在c ++线程中使用整数锁定 [英] Do I need to use locking with integers in c++ threads

查看:112
本文介绍了我需要在c ++线程中使用整数锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在多个线程中访问单个整数类型(例如long,int,bool等),我需要使用同步机制(如互斥锁)来锁定它们。我的理解是,作为原子类型,我不需要锁定访问一个线程,但我看到很多代码在那里使用锁定。分析这样的代码表明,使用锁有显着的性能损失,所以我宁愿不。因此,如果我访问的项目对应一个总线宽度整数(例如在32位处理器上的4个字节),我需要锁定访问它,当它被使用跨多线程?换句话说,如果线程A在从同一个变量读取线程B的同时写入整数变量X,那么线程B是否有可能结束前面值的几个字节与几个字节的混合该值是否写入?这种架构是否依赖于确定对32位系统上的4个字节整数,但对64位系统上的8个位元整数不安全?



编辑: =http://stackoverflow.com/questions/2751965/pseudo-atomic-operations-in-c>相关帖子,这有助于公平。

解决方案

您从不锁定值 - 您正在锁定对某个值的操作。



C ++没有明确提到线程或原子操作 - 所以看起来像他们可以或应该是原子的操作不被语言规范保证为原子。



当然是一个漂亮的异常编译器管理非原子读取int:如果你有一个操作读取一个值 - 可能不需要保护它。



操作就像 m_counter ++ 一样简单,涉及到一个提取,增量和存储操作 - 竞争条件:另一个线程可以在提取后但在存储之前更改值 - 因此需要由互斥体保护 - 或找到您的编译器对互锁操作的支持。 MSVC具有类似于_InterlockedIncrement()的函数,只要所有其他写入类似地使用互锁的apis来更新存储器位置 - 这比调用甚至临界区更轻量级。_ / _>

GCC具有 __ sync_add_and_fetch 的内在函数,也可用于对机器字值执行互锁操作。


If I am accessing a single integer type (e.g. long, int, bool, etc...) in multiple threads, do I need to use a synchronisation mechanism such as a mutex to lock them. My understanding is that as atomic types, I don't need to lock access to a single thread, but I see a lot of code out there that does use locking. Profiling such code shows that there is a significant performance hit for using locks, so I'd rather not. So if the item I'm accessing corresponds to a bus width integer (e.g. 4 bytes on a 32 bit processor) do I need to lock access to it when it is being used across multiple threads? Put another way, if thread A is writing to integer variable X at the same time as thread B is reading from the same variable, is it possible that thread B could end up a few bytes of the previous value mixed in with a few bytes of the value being written? Is this architecture dependent, e.g. ok for 4 byte integers on 32 bit systems but unsafe on 8 byte integers on 64 bit systems?

Edit: Just saw this related post which helps a fair bit.

解决方案

You are never locking a value - you are locking an operation ON a value.

C & C++ do not explicitly mention threads or atomic operations - so operations that look like they could or should be atomic - are not guaranteed by the language specification to be atomic.

It would admittedly be a pretty deviant compiler that managed a non atomic read on an int: If you have an operation that reads a value - theres probably no need to guard it. However- it might be non atomic if it spans a machine word boundary.

Operations as simple as m_counter++ involves a fetch, increment, and store operation - a race condition: another thread can change the value after the fetch but before the store - and hence needs to be protected by a mutex - OR find your compilers support for interlocked operations. MSVC has functions like _InterlockedIncrement() that will safely increment a memory location as long as all other writes are similarly using interlocked apis to update the memory location - which is orders of magnitude more lightweight than invoking a even a critical section.

GCC has intrinsic functions like __sync_add_and_fetch which also can be used to perform interlocked operations on machine word values.

这篇关于我需要在c ++线程中使用整数锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆