Home » U++ Library support » U++ MT-multithreading and servers » Use same variable in different threads
Re: Use same variable in different threads [message #30122 is a reply to message #30121] |
Fri, 10 December 2010 00:26 |
Didier
Messages: 680 Registered: November 2008 Location: France
|
Contributor |
|
|
Hi Koldo,
I would add one thing to Honza's explanation: 'Memory bariers'
Actually this is not a thing that is used directly by people but it is necessary to understand all that is going on.
Volatile allows you to access a shared variable not protected by mutex and you will be sure it always has the wright value (the cache on the processor might be different for each thread, so in extreme cases you can get de synchronised values of one variable from time to time ..... ==> bugs impossible to find).
Of course if the variable is larger than the processor bus ... the R/W process will not be atomic so you need a mutex !
Memory barriers are points where the cpu cache is flushed so all the synchronisation problems just disappear
In mutexes, such barriers are used to avoid such problems, and therefore when using a mutex lock/unlock procedure, you protect the resource (and it's memory) but you also flush the cash at each mutex lock/unlock.
So .... when using a mutex there is no need to use volatile it might even slow down you're app.
Finally, to answer you're question:
* if the variable (and it's context !) is just an int or smaller it can be accessed atomically ==> use this method, it's the fastest one (volatile will work in most cases but to be sure use the dedicated functions)
* otherwise you have to use the good old mutex, no may out !
- mutex.lock()
- do you're thing ..R..W....etc
- mutex.unlock()
NB: the volatile/synchronisation problems mostly has effects on programs compiled in O3.
Hope this helps you !
[Updated on: Fri, 10 December 2010 00:29] Report message to a moderator
|
|
|
|
|
Use same variable in different threads
By: koldo on Thu, 09 December 2010 18:26
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: koldo on Thu, 09 December 2010 22:04
|
|
|
Re: Use same variable in different threads
By: Didier on Fri, 10 December 2010 00:26
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: koldo on Fri, 10 December 2010 10:01
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: koldo on Fri, 10 December 2010 13:19
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Fri, 10 December 2010 18:50
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Sat, 11 December 2010 23:13
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: mirek on Sun, 12 December 2010 09:43
|
|
|
Re: Use same variable in different threads
By: koldo on Sun, 12 December 2010 23:16
|
|
|
Re: Use same variable in different threads
By: tojocky on Mon, 13 December 2010 11:35
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Mon, 13 December 2010 22:48
|
|
|
Re: Use same variable in different threads
By: koldo on Tue, 14 December 2010 09:02
|
|
|
Re: Use same variable in different threads
By: mirek on Sat, 25 December 2010 20:45
|
|
|
Re: Use same variable in different threads
By: tojocky on Sun, 02 January 2011 15:35
|
|
|
Re: Use same variable in different threads
By: mirek on Sun, 02 January 2011 18:38
|
|
|
Re: Use same variable in different threads
By: koldo on Sun, 02 January 2011 19:06
|
|
|
Re: Use same variable in different threads
By: mirek on Sun, 02 January 2011 22:56
|
|
|
Re: Use same variable in different threads
By: koldo on Mon, 03 January 2011 09:17
|
|
|
Re: Use same variable in different threads
By: koldo on Sun, 02 January 2011 01:12
|
|
|
Re: Use same variable in different threads
|
|
|
Re: Use same variable in different threads
By: Didier on Sun, 02 January 2011 12:18
|
|
|
Re: Use same variable in different threads
|
Goto Forum:
Current Time: Thu May 09 09:28:30 CEST 2024
Total time taken to generate the page: 0.02617 seconds
|