Re: Does 32 bits value assignment needs locking operation



On 5月16日, 下午12时03分, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On May 15, 8:37 pm, lxco...@xxxxxxxxx wrote:





Hi everybody :)
I want to know that if a locking operation is needed when I want to
get a 32-bits value from shared resource. e.g

class simplesignal
{
priate:
volatile long member_signal;

public:
explicit simplesignal(bool value) : member_signal(value){}

public:
void SetSignal(void){ ::InterlockedExchange(&member_signal,
true); }
void ClearSignal(void){ ::InterlockedExchange(&member_signal,
false); }
bool GetSignal(void)
{
// should I use a lock here?
// or simply: return member_signal;
}

};

It depends what hardware you want your code to work on and what your
requirements are. For example, on x86, 32-bit aligned reads are
guaranteed atomic, so you won't get half of one value and half of
another. But it's not guaranteed that you will get a new value in any
particular time frame.

I would strongly urge you to implement it in a way that is guaranteed
to work and take shortcuts only as an optimization for cases where
there is demonstrated need for the optimization and the optimization
is known safe for the platform.

Please don't create another case of code that works on CPU X, but
breaks when snazzy new CPU Y comes out. (Unless this is low-level
library code that could reasonably be expected to be CPU-specific. In
that case, read the docs on the particular CPU.)

DS- 隐藏被引用文字 -

- 显示引用的文字 -

Hi David! Thank you very much for your explanation! Actually the code
is working on x86 32-bit platform as s simple sign.
You said that the the code should be implemented in a safe way, could
you briefly explain it? For example, would you please give me some
suggestion on the "simplesignal" class(Suppose that the code is aimed
at x86, 32bit platform). :)

Lxconan

.



Relevant Pages

  • Re: Understanding the BSP concept and terms
    ... A cpu vendor can put anything they like on a cpu. ... But in the general case (CE runs on a number of processors, not just X86) ... Got a BSP from VIA that supports the cpu and chipset etc. ... So as you can see the BSP was not "intended" for our platform directly due ...
    (microsoft.public.windowsce.embedded)
  • Re: Does 32 bits value assignment needs locking operation
    ... I want to know that if a locking operation is needed when I want to ... For example, on x86, 32-bit aligned reads are ... there is demonstrated need for the optimization and the optimization ... breaks when snazzy new CPU Y comes out. ...
    (comp.programming.threads)
  • Re: Understanding the BSP concept and terms
    ... For example if our new Intel CPU supported had some knid of super duper AD ... x86 architecture should be largely compatible. ... Got a BSP from VIA that supports the cpu and chipset etc. ... So as you can see the BSP was not "intended" for our platform directly ...
    (microsoft.public.windowsce.embedded)
  • Re: Understanding the BSP concept and terms
    ... We bought a PCM6892 board with a x86 VIA CPU. ... Got a BSP from VIA that supports the cpu and chipset etc. ... So as you can see the BSP was not "intended" for our platform directly due ...
    (microsoft.public.windowsce.embedded)
  • [git pull] x86 updates for v2.6.28, phase #6, misc
    ... tip/x86/core: various x86 updates independent of the sparse irqs support ... separate generic cpu disabling code from APIC writes in cpu_disable ... convert hardware exception 0 to an interrupt gate ... +void printk_address ...
    (Linux-Kernel)