About the global variable in multi-thread? 
Author Message
 About the global variable in multi-thread?

    Hello everybody, I develope a DLL with some global variables in it.I
want these
variables used by the code in the dll.But when I start another thread, I
find the
two thread use the same global variables.Maybe what i need to do is keep the
every
thread use its variable itself.
    Any idea would be great thanks.


Sat, 05 Jun 2004 19:08:08 GMT  
 About the global variable in multi-thread?
Two ideas:
1) Use thread-local storage: __declspec(thread)
2) Use separate class objects in which the threads execute, and put the
variables as members.


Sat, 05 Jun 2004 21:56:32 GMT  
 About the global variable in multi-thread?
Yes, that is the nature of global variables. All threads see the same variable.

Generally, avoid the use of global variables for almost any purpose. I write one or two
write-once global variables per application.

You may find out about __declspec(thread) which creates "global" variables for each
thread. Alas, this does not work in DLLs. You need to use Tls-functions

First, you need a write-once global variable that is used by all threads:
        UINT myslot;

        case DLL_PROCESS_ATTACH:
                myslot = TlsAlloc();
                if(myslot < 0)
                        return FALSE; // no more slots available
                ....
                return TRUE;

        case DLL_THREAD_DETACH:
                {
                            MyVars * vars = (MyVars *)TlsGetValue(myslot);
                 delete vars; // may be NULL, that's OK
                }
                return TRUE;
        case DLL_THREAD_ATTACH:
                {
                 MyVars * vars = new MyVars;
                 TlsSetValue(myslot, vars);
                }
                return TRUE;

Create a struct that holds are your "global" variables
        class MyVars {
                public:
                        int count;
                        ...whatever else you want...
        };

Now, whenever you need a thread-specific "global" variable, you do, instead of
        count--;

        MyVars * vars = (MyVars *)TlsGetValue(myslot);
        ASSERT(vars != NULL);
        if(vars->count > 0)
            vars->count--;  

Note that the usual optimizations apply. Also, any thread that existed before the DLL was
loaded will see vars == NULL, and if you are in a situation where threads can pre-exist,
you probably need to do something to create vars

It may surprise you that I never use this mechanism myself, except in some rare and
esoteric contexts, because I don't use global variables. The first thing you should look
at is why you are using global variables, and do some rewriting to get rid of them. And
get out of the habit of ever using them, in any context, for any purpose, except (a)
read-only tables, constants, and so on (b) write once variables which are "run-time
constants", that is, once initialized, are never modified (e.g., saving the HINSTANCE of a
DLL).
                        joe

Quote:

>    Hello everybody, I develope a DLL with some global variables in it.I
>want these
>variables used by the code in the dll.But when I start another thread, I
>find the
>two thread use the same global variables.Maybe what i need to do is keep the
>every
>thread use its variable itself.
>    Any idea would be great thanks.

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sun, 06 Jun 2004 03:19:24 GMT  
 About the global variable in multi-thread?
__declspec(thread) doesn't work in DLLs.
                        joe


Quote:
>Two ideas:
>1) Use thread-local storage: __declspec(thread)
>2) Use separate class objects in which the threads execute, and put the
>variables as members.

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sun, 06 Jun 2004 03:19:41 GMT  
 About the global variable in multi-thread?
    Thanks Joseph ,you were right, these global variable cause me a great
trouble.One of them is a imagelist which can be  accessed by several Class.
To save trouble I used global variable but result as the more trouble.


Sun, 06 Jun 2004 10:43:01 GMT  
 About the global variable in multi-thread?
An imagelist is an example of a "write once constant", and could be shared among all the
threads. Therefore, the fact that it is shared should not pose any problem. Note that you
should not be manipulating windows out of threads unless the threads own the windows, and
creating systems in which threads own windows poses its own risks.
                        joe

Quote:

>    Thanks Joseph ,you were right, these global variable cause me a great
>trouble.One of them is a imagelist which can be  accessed by several Class.
>To save trouble I used global variable but result as the more trouble.

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sun, 06 Jun 2004 15:40:32 GMT  
 About the global variable in multi-thread?
In rereading this, I forget if the mapping of imagelists is thread specific. This could
cause problems.
                                joe


Quote:
>An imagelist is an example of a "write once constant", and could be shared among all the
>threads. Therefore, the fact that it is shared should not pose any problem. Note that you
>should not be manipulating windows out of threads unless the threads own the windows, and
>creating systems in which threads own windows poses its own risks.
>                    joe


>>    Thanks Joseph ,you were right, these global variable cause me a great
>>trouble.One of them is a imagelist which can be  accessed by several Class.
>>To save trouble I used global variable but result as the more trouble.

>Joseph M. Newcomer [MVP]

>Web: http://www3.pgh.net/~newcomer
>MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Wed, 09 Jun 2004 15:35:26 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Global variable or not global variable

2. question on multi-declaration of a global variable

3. Global variable access from multiple threads.

4. global variables thread-safe

5. global variable error in commucation between threads ;)

6. Global variables and threads

7. Communication error between threads by global variable

8. global variables with threads

9. Uninitialized global data (was Re: Global variables)

10. static local variables vs global variables

11. Variables & Global Variables

12. global variables and external variables?

 

 
Powered by phpBB® Forum Software