Re: Does 32 bits value assignment needs locking operation
- From: lxconan@xxxxxxxxx
- Date: 15 May 2007 21:57:32 -0700
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
.
- Follow-Ups:
- Re: Does 32 bits value assignment needs locking operation
- From: David Schwartz
- Re: Does 32 bits value assignment needs locking operation
- From: Markus Elfring
- Re: Does 32 bits value assignment needs locking operation
- References:
- Does 32 bits value assignment needs locking operation
- From: lxconan
- Re: Does 32 bits value assignment needs locking operation
- From: David Schwartz
- Does 32 bits value assignment needs locking operation
- Prev by Date: Re: Does 32 bits value assignment needs locking operation
- Next by Date: Re: should I declare them volatile
- Previous by thread: Re: Does 32 bits value assignment needs locking operation
- Next by thread: Re: Does 32 bits value assignment needs locking operation
- Index(es):
Relevant Pages
|