Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Thread Local Storage

Moderators: larsivi kris

Posted: 11/18/07 11:41:45 Modified: 11/18/07 11:53:02

Hi, Instead of using the WINAPI TLSxxx functions like TlsAlloc?() I would prefer to use the ThreadLocal? class. Is anybody willing to offer me a small example ?
Many thanks in advance, Bjoern

Author Message

Posted: 11/18/07 12:55:39

Have a look at the sources in lib/common/tango/core/Thread.d

In general...

  1. create a storage with uint key = createLocal and store the key to access the storage.
  2. use setLocal(key, value), void* p = getLocal(key) to store and retrieve a pointer
  3. use deleteLocal(key) to delete the storage

Posted: 11/18/07 13:27:32 -- Modified: 11/18/07 13:39:40 by
BLS

Thanks Frank, yep you are talking about the tango.core.Thread Thread class. I meant the LocalThread? class. http://www.dsource.org/projects/tango/docs/current/tango.core.Thread.html

(at the bottom)

I thought I could use this class ; (But probabely I am completely wrong.)

alias ThreadLocal!(null) tls;
.....

Beside the data I have to deal with :

	// Used for Thread Local Storage (TLS)
	struct TLSData
	{
		CWnd rCWnd;		// ref to CWnd object for Window creation
		HHOOK  hCBTHook;	// CBT hook for Window creation
		CMenubar rMenubar;	// ref to CMenubar object
		HHOOK  hMenuHook;	// MSG hook for CMenubar
................... etc
	};

Posted: 11/18/07 13:49:26

Yes you can use this. The templates argument must be a type, this is why your null argument will not work.

Something like this shall do it...

alias ThreadLocal!(TLSData) MyData;

MyData data = new MyData( getSomeInitialTLSData() );

// retrieve
TLSData d = data.val();
// store
data.val( getSomeTLSData() );

Is it this, you are looking for?

Posted: 11/18/07 13:56:24 -- Modified: 11/18/07 13:57:43 by
BLS

Thanks Frank ! Perfect. Just have to save the MyData?.val() results in dyn. array . Bjoern