Tech and Academic things for Chianshin

Monday, January 04, 2010

C++ multi-thread与volatile关键字 - C++软件架构知识 - Stephen_Du

C++ multi-thread与volatile关键字 - C++软件架构知识 - Stephen_Du: "Summary

When writing multithreaded programs, you can use volatile to your advantage. You must stick to the following rules:

* Define all shared objects as volatile.
* Don't use volatile directly with primitive types.
* When defining shared classes, use volatile member functions to express thread safety.

If you do this, and if you use the simple generic component LockingPtr, you can write thread-safe code and worry much less about race conditions, because the compiler will worry for you and will diligently point out the spots where you are wrong.

A couple of projects I've been involved with use volatile and LockingPtr to great effect. The code is clean and understandable. I recall a couple of deadlocks, but I prefer deadlocks to race conditions because they are so much easier to debug. There were virtually no problems related to race conditions. But then you never know."

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home