While I was working with an online downloadable video tutorial for developing 3D graphics and a game engine that works with modern OpenGL. We used volatile in one of our classes. The tutorial's website can be found here , and a video that works with the volatile keyword is found in the Shader Engine series 98 video. These works are not mine, but are accredited by Marek A. Krzeminski, MASc , and this is an excerpt from the video download page.
And if you are subscribed to his site and have access to his video in this video, he refers to this article regarding the use of volatile with multithreading programming.
volatile: Best friend with several programmers
Andrey Alexandrescu, February 01, 2001
The volatile keyword was developed to prevent compiler optimizations that could make code incorrect if certain asynchronous events are present.
I don’t want to spoil your mood, but this column touches on the terrible topic of multithreaded programming. , Generic, , - .
, , , , , , . , amok, .
, , , , . - - , , , , .
, C, ++ , , volatile.
const, volatile . , . , , . .
Consider the following code:
class Gadget { public: void Wait() { while (!flag_) { Sleep(1000);
Gadget:: Wait - -_ , ​​ true . , , , , , , .
, , Sleep (1000) , _. , _ . , : , Wait - Gadget, Wakeup, Wait . , _ , _. ... .
- , , . C ++ . volatile , - . , , , Gadget Wait/Wakeup, - :
class Gadget { public: ... as above ... private: volatile bool flag_; };
volatile - , . , , volatile, ++ .
volatile
volatile-qualify , . volatile , const. ( volatile .)
const, volatile . , , - (, , ..), . , int volatile int, .
, volatile .
class Gadget { public: void Foo() volatile; void Bar(); ... private: String name_; int state_; }; ... Gadget regularGadget; volatile Gadget volatileGadget;
, volatile , .
volatileGadget.Foo(); // ok, volatile fun called for // volatile object regularGadget.Foo(); // ok, volatile fun called for // non-volatile object volatileGadget.Bar(); // error! Non-volatile function called for // volatile object!
. , , . :
Gadget& ref = const_cast<Gadget&>(volatileGadget); ref.Bar();
, , , , . , const_cast. , , (, volatileGadget.name_ volatileGadget.state_ - ).
,
. Acquire Release. Acquire - , , Acquire, . , Release, , Acquire. , Acquire Release. Acquire Release . ( Windows , , "" . , mutex mutex .)
. , , . , . , . , . - , - , .
, , , , - , . , ++ .
- ; , , , . - , .
- , , . , . - .
, , , .
, . , const_cast. , ++ . .
LockingPtr
, const_cast. LockingPtr, obj mutex mtx. LockingPtr mtx. , LockingPtr . - > *. Const_cast LockingPtr. , LockingPtr , .
Mutex, LockingPtr:
class Mutex { public: void Acquire(); void Release(); ... };
LockingPtr, Mutex, .
LockingPtr templated . , , LockingPtr, volatile Widget.
LockingPtr . LockingPtr . const_cast .
template <typename T> class LockingPtr { public:
, LockingPtr - . , , const_cast - LockingPtr. .
, , :
class SyncBuf { public: void Thread1(); void Thread2(); private: typedef vector<char> BufT; volatile BufT buffer_; Mutex mtx_;
LockingPtr - buffer_:
void SyncBuf::Thread1() { LockingPtr<BufT> lpBuf(buffer_, mtx_); BufT::iterator i = lpBuf->begin(); for (; i != lpBuf->end(); ++i) { ... use *i ... } }
- , buffer_, LockingPtr, . , .
, , :
void SyncBuf::Thread2() { // Error! Cannot access 'begin' for a volatile object BufT::iterator i = buffer_.begin(); // Error! Cannot access 'end' for a volatile object for ( ; i != lpBuf->end(); ++i ) { ... use *i ... } }
- buffer_, const_cast LockingPtr. , LockingPtr const_cast .
LockingPtr . , LockingPtr :
unsigned int SyncBuf::Size() { return LockingPtr<BufT>(buffer_, mtx_)->size(); }
, volatile LockingPtr . , - .
, int.
class Counter { public: ... void Increment() { ++ctr_; } void Decrement() { —ctr_; } private: int ctr_; };
Increment Decrement , . -, ctr_ . -, , ++ ctr_, . . :
RMW (Read-Modify-Write). RMW , .
RMW , : .
, LockingPtr:
class Counter { public: ... void Increment() { ++*LockingPtr<int>(ctr_, mtx_); } void Decrement() { —*LockingPtr<int>(ctr_, mtx_); } private: volatile int ctr_; Mutex mtx_; };
, SyncBuf. What for? Counter , ctr_ ( ). ++ ctr_, ctr_ volatile, . , .
? , , . , , , !
-
, ; , , , . volatile member .
- -, . , . : volatile ; , .
, Widget, - - , .
class Widget { public: void Operation() volatile; void Operation(); ... private: Mutex mtx_; };
. Widget , , , . , Widget .
volatile member , , LockingPtr. :
void Widget::Operation() volatile { LockingPtr<Widget> lpThis(*this, mtx_); lpThis->Operation();
volatile . :
, LockingPtr, , , , , .
, , volatile LockingPtr. . , , . , , . .
, .
- RealNetworks Inc. (www.realnetworks.com), , , Modern ++ Design. : www.moderncppdesign.com. ++ (www.gotw.ca/cpp_seminar).
, , , , . OPs , volatile .