Home » U++ Library support » U++ MT-multithreading and servers » Use same variable in different threads
Re: Use same variable in different threads [message #30133 is a reply to message #30132] |
Fri, 10 December 2010 14:08 |
gprentice
Messages: 260 Registered: November 2005 Location: New Zealand
|
Experienced Member |
|
|
No expert here either but when you're sharing data between threads, I think you need both volatile and synchronization.
A mutex ensures that the compiler/linker can't optimise code across the mutex entry, that the cache is flushed and that only one thread can be executing the code the mutex protects (i.e. it's synchronized).
http://msdn.microsoft.com/en-us/library/ms686355(v=VS.85).aspx
volatile ensures that the compiler/linker can't optimise your code and use a cached value. With Microsoft, optimisation can occur at link time. On some platforms you can get away without volatile if you call a global function that the compiler can't see - the compiler has to assume that global function might modify the variable you're sharing so is forced to re-read the variable from memory, but that is't safe with Microsoft.
There's also thread local storage - see thread__
Regarding atomic - on Win32, 32 bits are atomic and on Win64, 64 bits are atomic. On Win32, the atomixXXX functions use the Interlocked... functions that allow you to read/write without being interrupted by another thread etc, and also provide a memory barrier.
Hence I think you need
volatile mySharedData data;
volatile Atomic threadnum;
Anyway, I don't think that using volatile would be wrong, even if it's not always necessary.
Graeme
[Updated on: Fri, 10 December 2010 14:09] 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 07:58:05 CEST 2024
Total time taken to generate the page: 0.02192 seconds
|