p.magalhaes
p.magalhaes

Reputation: 8354

Threads - Sharing variables

I have a system that is multi-threaded. I want to create a object in a thread, and every object that runs in this thread can view this object.

Example,

When i use GetCurrentThreadID i always get same id, if i call it from the same thread.

I want to call, for example, getSharedObject and always see the same object if i call it from the same object. So I need to write this object in a memory location that any object inside the same thread can see this object.

Is there anyway to do that, using the Windows API? Or I have to do it by myself?

thanks!

Upvotes: 1

Views: 95

Answers (1)

rodrigo
rodrigo

Reputation: 98328

If the variable where you save the object pointer is global, then any code in your thread can access it. And any code from any other thread can, too, for that matter.

If you want that each thread sees a different object, then you want Thread Local Storage.

See the win32 functions TlsAlloc, TlsSetValue, TlsGetValue and TlsFree.

See also __declspec( thread ) here.

Upvotes: 2

Related Questions