views:

141

answers:

3

I am implementing some speed critical multithreaded code. I can avoid having some critical sections if I know for certain that some basic write operations are atomic. I just read an academic paper in which I saw the following:

"Writes of the basic types size t, int, float and pointer must be atomic. Writes by one thread must be seen by other threads in the same order. The IA-32 and Intel-64 CPU architectures, which are used in most modern standard computers, guarantee these assumptions."

What I would like to do is to be able to detect at run-time whether the processor is of a type in which these operations are atomic. - I'd like this to work for AMD processors too.

+2  A: 

That sounds redundant. The .EXE for this could simply be int main() { return true; }. Either it runs, and the answer is correct, or the OS is unable to run the .EXE at all becaus the processor type doesn't match the .EXE type.

MSalters
But I could make two different versions - a slow one with critical sections and a fast one without. I would like to include a test in the fast one to be able to gracefully exit rather than crash or produce wrong answers.
Mick
No need to. Just use `InterlockedIncrementAcquire`, that uses the fastest algorithm everywhere. Even on Itanic.
MSalters
InterlockedIncrementAcquire is Win32 specific.
AlexKR
No it isn't. Win32, Win64-x64 and Win64-IPF. See also comment on `InterlockedIncrement`. It's Windows-specific though, but check the tags: Visual C++.
MSalters
A: 

I know this is off topic but if you are planning to write lock free code you should definatly read this first Lock-Free Code: A False Sense of Security by Herb Sutter

Quote from the article:

Lock-free code has two major drawbacks. First, it's not broadly useful for solving typical problems—lots of basic data structures, even doubly linked lists, still have no known lock-free implementations. Coming up with a new or improved lock-free data structure will still earn you at least a published paper in a refereed journal, and sometimes a degree.

iain
A: 

To avoid getting into these CPU/platform specific issues you could consider:

Waiting for std::atomic in the c++0x standard (already available for GCC)

or

Using Intel TBB

or

Using the ACE_Atomic_Op

Stuart