Click here to Skip to main content
15,909,518 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Yes. Pin
CPallini28-Dec-07 23:45
mveCPallini28-Dec-07 23:45 
GeneralRe: Yes. Pin
George_George29-Dec-07 0:00
George_George29-Dec-07 0:00 
GeneralRe: Yes. Pin
Maxwell Chen28-Dec-07 4:25
Maxwell Chen28-Dec-07 4:25 
GeneralRe: Yes. Pin
JudyL_MD28-Dec-07 12:21
JudyL_MD28-Dec-07 12:21 
GeneralRe: Yes. Pin
George_George28-Dec-07 18:17
George_George28-Dec-07 18:17 
GeneralRe: Yes. [modified] Pin
JudyL_MD28-Dec-07 20:01
JudyL_MD28-Dec-07 20:01 
GeneralRe: Yes. Pin
George_George28-Dec-07 23:01
George_George28-Dec-07 23:01 
GeneralRe: Yes. Pin
JudyL_MD29-Dec-07 4:40
JudyL_MD29-Dec-07 4:40 
Volatile has nothing to do with synchronization. It simply instructs the compiler that every access to the variable must be to the "real" memory allocated to that variable - it can't use registers to store the value, it can't not perform the memory access, and so on. Synchronization typically ensures that only one thread (or process) is using a resource at a time. My use of the volatile keyword does not force only one thread at a time to touch the variable. Technically, my example is "bad" code since it does not contain any logic to synchronize access to the variable. In my example, however, I can get away with it because of the way I use the variable. There is only one thread that changes the value and only one thread that checks the value. Also, the thread logic is such that if the thread loop runs one extra time due to a thread preemption (i.e. the changing thread sneaks in and changes the value after the checking thread has loaded the memory value but before it has checked the value), then there are no ill effects.

Things like mutexes and critical sections ensure single access to a resource. What that resource is has no relationship to the synchronization mechanism that protects it. The resource could be a shared memory location or a handle to a socket or a fifo or a pipe or ... or something as simple as a counter of the number of threads. To put it simply, the synchronization mechanism controls how the resource is protected and the resource is whatever you want it to be. The only relationship between the two is that the sync controls how many threads are allowed to touch the resource simultaneously.

Judy
GeneralRe: Yes. Pin
George_George29-Dec-07 19:39
George_George29-Dec-07 19:39 
GeneralRe: Yes. Pin
JudyL_MD30-Dec-07 4:37
JudyL_MD30-Dec-07 4:37 
GeneralRe: Yes. Pin
George_George31-Dec-07 4:01
George_George31-Dec-07 4:01 
GeneralRe: Yes. Pin
JudyL_MD31-Dec-07 4:30
JudyL_MD31-Dec-07 4:30 
GeneralRe: Yes. Pin
George_George31-Dec-07 5:17
George_George31-Dec-07 5:17 
GeneralRe: Yes. Pin
George_George28-Dec-07 18:19
George_George28-Dec-07 18:19 
GeneralRe: Yes. Pin
David Crow28-Dec-07 4:58
David Crow28-Dec-07 4:58 
GeneralRe: Yes. Pin
George_George28-Dec-07 18:17
George_George28-Dec-07 18:17 
GeneralRe: MSDN volatile sample Pin
peterchen28-Dec-07 2:47
peterchen28-Dec-07 2:47 
GeneralRe: MSDN volatile sample Pin
George_George28-Dec-07 3:20
George_George28-Dec-07 3:20 
GeneralRe: MSDN volatile sample [modified] Pin
peterchen28-Dec-07 4:21
peterchen28-Dec-07 4:21 
GeneralRe: MSDN volatile sample Pin
Maxwell Chen28-Dec-07 4:31
Maxwell Chen28-Dec-07 4:31 
GeneralRe: MSDN volatile sample Pin
peterchen28-Dec-07 4:32
peterchen28-Dec-07 4:32 
GeneralRe: MSDN volatile sample Pin
Maxwell Chen28-Dec-07 4:34
Maxwell Chen28-Dec-07 4:34 
GeneralRe: MSDN volatile sample Pin
George_George28-Dec-07 18:37
George_George28-Dec-07 18:37 
GeneralRe: MSDN volatile sample Pin
peterchen29-Dec-07 10:51
peterchen29-Dec-07 10:51 
GeneralRe: MSDN volatile sample Pin
George_George29-Dec-07 19:51
George_George29-Dec-07 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.