0

I have a global variable declared in a common header file that I want multiple threads to have access too. However if main.cpp changes the global variable before the thread is created or after it created, the change isn't shown in any of the spawn threads. In this case it won't show the value 5 in run_thread1. I thought static variables were shared by all threads by default.

Why am I getting this behavior and how do I fix it so that I have a global variable shared among threads?

main.cpp

#include <iostream>
#include <pthread.h>
#include "../include/common.hpp"
#include "../include/thread1.hpp"

int main() {
pthread_t thread1;

g_var = 5;

std::cout << "main: g_var = " << g_var << std::endl;

int rc = pthread_create(&thread1,
                        NULL,
                        run_thread1,
                        (void *) 0);
return 0;
}

thread1.hpp

#ifndef THREAD1
#define THREAD1

#include <iostream>
#include "common.hpp"

void run_thread1(void* p);

#endif

thread1.cpp

#include "../include/thread1.hpp"

void run_thread1(void* p) {
    std::cout << "Thread1: g_var = " << g_var << std::endl;

    return;
}

common.hpp

#ifndef COMMON
#define COMMON

static int g_var;

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
project(pthreads)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fpermissive")

set(SRC ${CMAKE_CURRENT_SOURCE_DIR}/src)
file(GLOB SRCS ${SRC}/*.cpp)

set_source_files_properties(${SRCS} PROPERTIES LANGUAGE CXX )

include_directories("include/")

add_executable(pthreads ${SRCS})

output:

main: g_var = 5

Thread1: g_var = 0

Community
  • 1
  • 1
randomname
  • 265
  • 1
  • 9
  • 20
  • Change to `static std::atomic g_var;` On mobile so will let someone else give you the full explanation. – Richard Critten Oct 14 '16 at 06:40
  • 1
    I suspect that you compile each `.cpp` separately; I am wondering whether this implies that you have one static `g_var` per compilation unit? A solution is to define your variable as `extern` in your header and define `g_var` in a single `.cpp` file. I cannot verify my claim right now though... – piwi Oct 14 '16 at 06:43
  • @RichardCritten This problem has nothing to do with concurrency; I bet you can simply call `run_thread` in the same thread and still have the same problem. – piwi Oct 14 '16 at 06:45
  • `#include` is a simple mechanism - it literally just includes the text of the indicated file at that point. Every time you include "common.hpp", the definition `static int g_var;` is added to that translation unit. It is exactly the same as if you'd written that manually in each file instead of using a header. Now think about how many `g_var`s there are. – molbdnilo Oct 14 '16 at 06:47
  • @molbdnilo I think your right. I created another thread in main.cpp and it had the correct value of 5. How would I go about creating a variable that isn't created on a per file basis? I want to have a single variable for the whole process. – randomname Oct 14 '16 at 06:55
  • @randomname Don't make it `static`. (There are lots of question on SO regarding how to define global variables in C++.) But why not have one non-global that you pass as a thread parameter instead? – molbdnilo Oct 14 '16 at 07:07

0 Answers0