Andrei alexandrescu
I don't want to destroy your emotions, but this column is the most terrible problem in multi-threaded programming. If it is, as the previous generic
It's just a small keyword although C and C standards clearly keep the threads silent, they still have a small concession for multi-threads, which makes a volatile keyword. As its more known partner const, Volatile is a Type Modifier. . Its role is that the variable is used to enable variables to access and modify different threads. At all, if there is no volatile, it is either to write a multi-threaded program, or the compiler is wasting a great optimization opportunity. Now explain why this is this. Consider the following code:
Class gadget {public: void Wait () {while (! Flag_) {Sleep (1000); // Sleep 1000 millisecond}} void wakeup () {flag_ = true;} ... private: BOOL FLAG_;};
The role of gadget :: wait is to check the FLAG_ member variable per second, returns when that variable is set to True by other threads. At least this is the original intention of the programmer, but, hey, the wait function is wrong. If the compiler discsses the SLEEP (1000) is a call to the external library, and this call cannot modify the member variable flag_. Then the compiler will decide to cache FLAG_ in the register and replace slower memory with that register. This is very good to optimize the single-threaded code, but in this case, this optimization has destroyed correctness: After calling Wait on a gadget object, despite another thread to call Wakeup, Wait will always cycle Go down. This is because modifications to FLAG_ are not reflected to the registers of the cache FLAG_. This optimization is really ... over optimized. Caching variables into registers Most of the time is a very useful optimization, and it is a pity to waste. C and C give you the opportunity to explicitly disable this optimization. If you identify a variable with a volatile, the compiler will not cache that variable into the accumulator - each access to the variable is directly through the actual memory location. So let the gadget's wait / wakeup work properly as long as it is correctly modified FLAG_
Class gadget {public: ... is above ... private: Volatile Bool Flag_;
Most of the explanation of Volatile uses and usage is suggesting that you add a Volatile identifier for the basic type in the multi-thread. However, you can do more things with volatile because it is part of the C wonderful type system.
Using Volatile, you can use Volatile, you can add a Volatile identifier before the basic type, and can also be added before the user-defined type. under these circumstances. Volatile is modified as constrays (you can also simultaneously add const and volatile at the same time) but are unlike const, Volatile different types of basic types and user-defined types. That is to say, unlike class, the basic type is still supported after the Volatile identifier is still supporting all of them (plus, multiplied, assignment, etc.). For example, you can assign a non-Volatile Int to a Volatile Int, but you can't assign a non-Volatile object to a volatile object. Let us exemplify how Volatile acts on the user-defined type. Class gadge {public: void foo () volatile; void bar (); ... private: string name_; int state _;}; ... gadget regulagadget; volatile gadget volatilegadget;
If you think volatile doesn't work for objects, it is ready to be scared.
Volatilegadget.foo (); // Success, call the volatile object to call the volatile function without problem regulagadget.foo (); // Success, call the volatile function for non-Volatile objects No problem volatilegadget.bar (); // Failed! Can't call the volatile object to call non-Volatile functions
Converting the identified type to the corresponding Volatile object is simple. However, you can't turn the volatile to no logo. You must use CAST:
Gadget & ref = const_cast
A class with a Volatile identifier can only access the subset of its interface, a subset of control by the implementation of the class. Users can only use const_cast to get full access to the type interface. Also, like Const, Volatile will pass from class to its member (for example, volatilegadget.name_ and volatilegadget.state_ is volatile variable)
The easiest way to use the most synchronous facilities in the multi-threaded program of Volatile, Race Conditions, is MUTEX, a MUTEXT provides the basic features of Acquire and Release. Once you call acquire in a thread, any other call acquire is getting clogged. Sowing of the thread calling Release, there will be a thread that is previously blocked by Acquire is released. In other words, there is a MUTEX, only one thread can get the processor time between the acquire call and the release call. The execution code between the acquire call and the release call itself is a critical area. (Windows terminology is a bit confused because it makes Mutex itself a Critical Section (critical area). Although "MuteXT" is actually a process range MUTEX, but calling their thread MUTEX and process MUTX will be better.) Mutex It is used to protect data and prevent the actual conditions. Depending on the definition, when the result of the multi-thread pair data processing is determined by the thread, a active condition is generated. When two or more thread competition uses the same data, the actual conditions appear. Because the thread may be interrupted at any point in time, the data being processed may be destroyed or missed. As a result, the modification of the data must be carefully protected by the critical area when reading a modification operation or sometime. In object-oriented programming, this usually means that you store a MUTEX in a class as a member variable, when you use it when you access the data. Experienced multithreaded programmers may already beaten in the top two paragraphs, but the purpose of that paragraph is to provide a warm-up, because now we have to connect multi-threaded programming and volatile. We do this by taught the intersection of C 's type world and thread semantic world. * In addition to any thread, any thread can be interrupted at any time, there is no control, so the result is Volatile that is accessed by multiple threads. This also maintains the original intent of Volatile - prevents the compiler unclear cache the value used by multiple threads. * Define a MUTEX within the critical area, only one thread can be accessed. As a result, in a critical area, the execution code has a single-threaded environment. The variables used cannot be volatile - you can remove the Volatile identifier. In short, the data shared by multiple threads is volatile outside the critical area. It is non-volatile in the critical area. You can enter a critical area by locking a MUTEX. You remove the Volatile identifier by using a const_cast. If you put these two operations together, we have established a connection between the C type system and the application's thread. We can let the compiler to check our instance conditions for us.
LockingPtr We need a tool to focus on a MUTEX acquisition operation and a const_cast. Let's develop the LockingPTR template class, you can initialize this template class with a Volatile object OBJ and a MUTEX object MTX. During this template class, a LockingPtr keeps MTX always occupied. At the same time, LockingPtr provides access to OBJ to remove Volatile. This access is to provide with smart pointer, available via Operator-> and Operator *. Perform const_castr in LockingPTR, this conversion semantic is valid because LockingPtr keeps MUTEX in the survival period is occupied. First let's define the skeleton of the Mutex class used by LockingPtr:
Class Mutex {public: void acquire (); void release (); ...};
In order to use LockingPtr, you have to use the data structure and basic functions used in your operating system to implement MUTEX. LockingPTR uses controlled variables as templates. For example, if you want to manage a Widget, you use a LockingPtr
Although simple, LockingPtr is very helpful to write the correct multi-threaded code. You should define the objects shared by several threads as Volatile and cannot use const_cast - should always use the LockingPTR auto object. We use an example to explain: Suppose you have two threads sharing a vector
Class syncbuf {public: void thread1 (); void thread2 (); private: typef vector
In a thread function, you simply use a LockingPtr
Void syncbuf :: thread1 () {LockingPtr
These code is very easy to write very easy to understand - any time you need to use buffer_, you must create a LockingPtr
Void syncbuf :: thread2 () {// error, you can't call Begin () BUFT :: Iterator i = Buffer_.begin (); // Error! You can't call end () for (; i! = Lpbuf-> end (); i) {... use * i ...}}
You can't call any functions of Buffer_ unless you either use a const_cast or use LockingPtr. The difference is that LockingPTR provides an ordered way to use const_cast to volatile variables. LockingPTR is very expressive. If you only need to call a function, you can create an unknown temporary LockingPtr object and use it directly: unsigned int syncbuf :: size () {return lockingptr
Back to the Basic Type We have seen how good the Volatile protection object is not accessed uncontrolled, and it has also seen how LockingPTR provides a simple and efficient way to write the thread security code. Let us return to the basic type, those with different types of Volatile behavior and user custom types, we consider an example, multiple threads share a variable of INT.
Class count {public: ... void increment () { CTR_;} void decrement () {--ctr_;} private: int CTR_;
If Increment and Decrement are called by different threads, the above code snippet is problematic. First, CTR_ must be Volatile, secondly, even if it is like CTR_, it is actually a three-step operation. Memory itself has no arithmetic ability, when incrementing a variable, the processor: * reads that variable to the register * Add value in the register * write the result back memory
This three-step operation is called RMW (Read-ModifyWrite Read - Change - Write). When performing an "change" operation of an RMW operation, most processors will release the memory bus in order to access the memory. If another processor performs an RMW operation to the same variable, we have a statement; the second write operation covers the first result. You can also avoid this situation with LockingPtr:
Class counter {public: ... void increment () { * lockingptr
The code is now correct, but the code quality is compared to the code of SyncBuf, and there is a lot of words. why? Because in counters, if you are incorrectly accessing CTR_ (no first to lock it first) The compiler will not warn you. If CTR_ is Volatile, CTR_ can also be compiled, but the resulting code is obviously wrong. The compiler is no longer your helper, just paying attention to your own attention to avoid such acts. Then what should you do? Simply put the basic data you use as a higher level, for those structures. It's ridiculous, although the use of Volatile is used in the internal type of type, it is actually doing this is not a good idea!
So far, we already have classes containing Volatile data members. Now let's consider designing a class part of a larger object, these classes are also shared by multi-threaded sharing. There is a great help with the Volatile member function here. When designing your class, you only add a VoALTILE logo for member functions of thread secure. You must assume that the external code will call the volatile function at any time with any code. Don't forget: volatile is equal to freely for multi-threaded code without using critical regions, non-Volatile is equal to a single-threaded environment or in a critical area. For example, you define a widget class to implement two changes in a function - a thread is safe and a fast, no protection. Class widget {public: void Operation () volatile; void Operation (); ... private: mutex mtx_;};
Note that overload is used. Now Widget users can call Operation with the same syntax, regardless of the Operation of the thread security call Volatile object or to get the speed calling the regular object. However, users must carefully define the Widget objects shared by multi-threaded to volatile. When a Volatile member function is implemented, the first operation is usually to lock with a LockingPtr to this. The rest of the work can be handed over to the corresponding function of nonivata:
Void widget :: Operation () Volatile {LockingPtr
Summary When writing multithreaded programs, you can get benefits with Volatile. You must abide by the following rules: * Define all shared objects to volatile. * Do not use the Volatile * when the basic type can be shared when the definition can be shared, using the Volatile member function to represent thread security.
If you do this, and if you use the simple return component LockingPtr, you can write out the thread secure code without having to consider the actual conditions, because the compiler can pay attention to you, will take the initiative to take the initiative to tell you wrong. local. Several people I have participated in a good effect using Volatile and LockingPtr. The code is clear and easy to understand. I remember to meet a few dead locks, but I am willing to encounter a deadlock or not, because the dead lock is more prone to more. In fact, no problem is about actual conditions.
Acknowledgment very thanked James Kanze and Sorin Jianu, they gave me a very helpful insight.
Supplement: Volatile is actually abused? I received many feedback on the "return
Author Blog:
http://blog.9cbs.net/merced1976/
related articles