C global static - common to threads?

In C, declaring a static variable in the global scope makes it global. Is this global variable distributed between threads or is it distributed across threads?

Update: If they are shared between threads, then what is an easy way to make globals in an existing library unique to a thread / not shared?

Update2: Basically, I need to use the existing C-library with globals in a thread-safe manner.

+6
c ++ c multithreading static global
source share
3 answers

It is displayed for the entire process, that is, for all threads. Of course, this is in practice. You could not theoretically say that streams have nothing to do with the C standard (at least until c99, which is the standard that was valid when this question was asked).

But all the thread libraries I have ever used would have global variables available to all threads.


Update 1:

Many thread libraries (pthreads for one) will allow you to create streaming data, a means to create and use thread-specific data without passing through a function.

So, for example, a function to return pseudo-random numbers may require that each thread have an independent seed. Therefore, every time he calls this, he creates or attaches to a certain thread a block containing this seed (using some kind of key).

This allows functions to maintain the same signature as non-streaming (which is important if they are ISO C functions, for example), since another solution involves adding a thread-specific pointer to the function call itself.

Another possibility is to have an array of globals from which each thread gets one, for example:

int fDone[10]; int idx; : : : for (i = 0; i < 10; i++) { idx = i; startThread (function, i); while (idx >= 0) yield(); } void function () { int myIdx = idx; idx = -1; while (1) { : : : } } 

This will let the flow function know which global variable in the array belongs to it.

There are other methods, no doubt, but without knowing the target environment, there is not much to discuss them.


Update 2:

The easiest way to use an unsafe library in a streaming environment is to provide wrapper calls with mutex protection.

For example, let's say your library has a function other than streaming doThis() . What you do is a shell for it:

 void myDoThis (a, b) { static mutex_t serialize; mutex_claim (&serialize); doThis (a, b); mutex_release (&serialize); } 

What will happen is that only one thread at a time will be able to request the mutex (and therefore, call a function other than the stream). Others will be blocked until the current one returns.

+16
source share

The C / C ++ standard does not support streams. Thus, all variables are distributed between threads. Thread support implemented in the C / C ++ runtime library, which is not part of the standard. Runtime is specific to each C / C ++ implementation. If you want to write portable C ++ code, you can use the increase interprocess library .

To declare a local thread variable in Microsoft Visual Studio, you can use the Microsoft __declspec( thread ) keyword.

+1
source share

As mentioned in @Pax, static variables are visible to all threads. There is no C ++ data construct associated with a particular thread.

However, on Windows, you can use the TlsAlloc API to distribute the index for stream-dependent data and put that index in a static variable.Each stream has its own slot, which you can access using this index and TlsGetValue and TlsSetValue. For more information, see Using Local Stream Storage on MSDN.

Refresh . It is not possible to make global tables in an existing library stream-specific. Any solutions will require that you also modify the code to know that the data has thread similarity.

+1
source share

All Articles