Use class variables between threads
I've always assumed it was safe to use variables between threads, but is it also safe to use class instances between threads?
__declspec(align(4)) volatile bool g_bHoldGameThread;
This is how I seen a demo app compiling its variables to be used between class' and I am unfamiliar with volatile and __declspec, I put that into my app and I get all kinds of errors.
I have no idea about __declspec but volatile tells the compiler that the variable can change (by another thread) without notice.
without volatile a loop that checks a value that is suppost to change in another thread my be complied into an infinite loop.
without volatile a loop that checks a value that is suppost to change in another thread my be complied into an infinite loop.
Quote:...but is it also safe to use class instances between threads?
It depends on your class, its expected behaviour and design. And it's not inherently safe to share variables between threads either. It all depends :)
Look at this:
struct A{ int some_variable = 0; void f() { if (some_variable == 0) { some_variable++; } }};
If two threads access a shared instance of A and call f at the same time, some_variable can accidentally be incremented twice. So, in the case above you'll need some form of synchronization. Basically, the only scenario where you don't need synchronization is when there are only read operations. As soon as you start adding some behaviour to the class, as in the case above, you'll probably need to serialize access. In your example, using a global variable to pause the demo/game seems a bit silly. Why not let all objects that can be paused have pause-methods?
Regards Mats
Quote:
I have no idea about __declspec
__declspec(align(4)) tells the compiler that you want the memory here to be allocated aligned to 4 bytes. This is the simple answer, there are many complexities to memory alignment that I won't get into, but thats what that means.
hth
moe.ron
I know all about Semaphores, Mutex... in Win32 more like CRITICAL_SECTION, makes a nice implimentation of a POSIX like condition variable.
I also understand memory aligment, you can change padding and what not with some pragma directives #pack (compiler specific probably), you can also use what is posted... So why would you need to specify that? especially when most compilers default to a 4 byte padding?
Volitile this I understand, I will use.
Anyway, so __declspec I've never seen, I know __stdcall cdecl___......the other one eludes me I'm to lazy to look, busy working... Anyway, a little explanation would be nice.
I also understand memory aligment, you can change padding and what not with some pragma directives #pack (compiler specific probably), you can also use what is posted... So why would you need to specify that? especially when most compilers default to a 4 byte padding?
Volitile this I understand, I will use.
Anyway, so __declspec I've never seen, I know __stdcall cdecl___......the other one eludes me I'm to lazy to look, busy working... Anyway, a little explanation would be nice.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement