views:

25

answers:

2

In pthreads, you can associate a destructor function with each per-thread storage slot. When a thread dies, if the slot is non-0, the destructor is called.

In a Win32 DLL, the DLLMain function, called at thread exit, can do the same thing.

What can I do in code that lives in a purely static library?

+1  A: 

Windows support Thread Local Storage (TLS) in a DLL. It can be very practical if you want have some memory blocks per thread with the unique value (unique per thread). Inside of any other function from the DLL you can get the value which correspond to the current thread in very easy way. It is very useful in some scenarios. Look at here for more details.

I dun't use pthreads myself, but I suppose that per-thread storage slot introduced to make the work with TLS more comfortable.

UPDATED: From your comment I see that you misunderstand my answer. So I explain it in other words.

First of all you should correct the name pthread_keycreate from the question title to pthread_key_create.

The equivalient of pthread_ functions in Win32 are following:

I not recommended you to use the construct __declspec(thread), which is more compiler specific.

The example Using Thread Local Storage shows how to use thread local storage (TLS) without DLLs, but I personally like and use TLS only in DLL.

The destructor parameter of the pthread_key_create function has no analog in Win32, but I don't see here any problem. All C/C++ compilers support __try {/**/} __finally {/**/} construct of the Structured Exception Handling so you can use it in the body of your thread function and implement in the way any Cleaning up Resources exacly like you can do this in the main thread.

I find pity that you not included in your question an example which shows how you typically use destructor of the pthread_key_create function. I find that examples can clear much things without a lot of words. So if my answer do not help you we can better explain all in examples: you write an example and probably short comment what it should do and I could write the same code using Win32 API only in C or C++.

Oleg
The point of my question is the management of this in code that does NOT live in a DLL. The __declspec thing has nasty limitations.
bmargulies