global variable at dynamic library

This is a discussion on global variable at dynamic library within the C++ Programming forums, part of the General Programming Boards category; Hello! I have a couple of functions that MUST share a single variable. These functions will be compiled into a ...

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    global variable at dynamic library

    Hello! I have a couple of functions that MUST share a single variable. These functions will be compiled into a dynamic library (.so) and this library will be accessed by multiple threads. Is this method thread-safe, or I must use mutexes to prevent corruption of the global variable?
    Code:
    // foo.h
    void foo1();
    void foo2();
    Code:
    // foo.cpp
    static int foo_flag = 0;
    void foo1() {
        // uses foo_flag
    }
    
    void foo2() {
        // uses foo_flag
    }
    Thanks any help!

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Hi, I have made a test myself and would like to share the results:
    Code:
    // test.cpp
    #include <pthread.h>
    #include <stdio.h>
    
    #include "my_lib.h"
    
    int main()
    {
    		pthread_t thread1, thread2;
    
    		printf("Creating thread 1\n");
    		pthread_create(&thread1, NULL, foo1, NULL);
    
    		printf("Creating thread 2\n");
    		pthread_create(&thread1, NULL, foo2, NULL);
    
    		printf("Waiting for key press\n");
    		getchar();
    }
    Code:
    // my_lib.h
    #include <stdio.h>
    #include <unistd.h>
    
    void* foo1(void*);
    void* foo2(void*);
    Code:
    // my_lib.cpp
    #include "my_lib.h"
    
    static int flag = 0;
    
    void* foo1(void*)
    {
    		while (true) {
    				printf("foo1: Flag is %d\n", flag);
    				sleep(1);
    		}
    		return NULL;
    }
    
    void* foo2(void*)
    {
    		while (true) {
    				printf("foo2: Flag is %d\n", flag);
    				sleep(1);
    				flag++;
    		}
    		return NULL;		
    }
    Both threads printed the incremented flag, i.e., the statement flag++ of foo2 of the second thread affected foo1 of the first thread.
    I will try to solve this problem, thanks.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Be aware that any global variable accessed by multiple threads WILL need some mechanism to ensure that it's consistently used.

    If you read flag in one place, and update it in another, you will have a race-condition between the two points as to the value being read.

    It is PROBABLY ok to set the flag to zero and one, but incrementing it in one place and decrementing in another is quite possibly a failure scenario, as you may find that the decremented value written back is the one before the increment or after the increment, no way to know for sure.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using extern to have a global variable
    By steve1_rm in forum C Programming
    Replies: 3
    Last Post: 01-29-2009, 05:44 AM
  2. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 03:25 AM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  4. Adding a directory to a dynamic library path
    By vivharv in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2007, 07:09 AM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21