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. -开发者_Go百科 I'd like this to work for AMD processors too.
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.
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.
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
精彩评论