0

In C++11, I create threads with std::thread. And I have some global variables:

const int max_size = 78;
const int cat[5] = {1, 20, 3, 40, 5};

Now, if my threads read those variables, is there any chance of unidentified behavior?

Luka
  • 1,761
  • 2
  • 19
  • 30
  • 3
    You should be fine in this case, although you probably meant ["undefined" or "unspecified"](http://stackoverflow.com/questions/2397984) behavior. – Daniel Frey Jan 09 '14 at 21:44
  • I am not writing to const variables. – Luka Jan 09 '14 at 22:12
  • C++ defines a data race as simultaneous access to the same memory location by two or more threads, at least one of which is a modification. – Casey Jan 09 '14 at 22:15

1 Answers1

4

As long as those variables are never written to (yes it is possible to write to const variables in C++ via pointer manipulation, but for what reason?) then no.

If you are worried about undefined or unspecified behavior (with respect to thread safety), you could always use a mutex.

An OK example:

// Globals
const int max_size = 78;
const int cat[5] = {1, 20, 3, 40, 5};

void call_from_thread() {
    std::cout << "Max Size: " << max_size << std::endl;
    std::cout << "cat[0]: " << cat[0] << std::endl;
    //..
    std::cout << "cat[4]: " << cat[4] << std::endl;
}

int main() {
    //Launch some threads
    std::thread thread1(call_from_thread);
    std::thread thread2(call_from_thread);

    //Join the threads with the main thread
    thread1.join();
    thread2.join();

    //..

    *((int*)&cat[3])=15; // Since threads are joined, this is OK as far as the write is concerned
    return 0;
}

In the above example, writing to a const variable can cause undefined behavior. So it is still a REALLY BAD idea.

A really really bad example:

int main() {
    //Launch some threads
    std::thread thread1(call_from_thread);
    std::thread thread2(call_from_thread);

    *((int*)&cat[3])=15; // BAD idea for thread safety as well as what is discussed above.

    //Join the threads with the main thread
    thread1.join();
    thread2.join();

    //..

    return 0;
}
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
  • If I remove the const, edit the variables on the main and start threads that only read the variables, I am still OK? – Luka Jan 09 '14 at 21:45
  • 1
    As long as the "start" threads are launched from the main thread and no subsequent writes happen after they are launched then yes you are OK. – Chris Dargis Jan 09 '14 at 21:46
  • If I remove the const from cat array and start 5 threads passing to each cat[i], do I need mutex? Those threads will write to cat[i]. Also, mutex gives a performance hit, right? – Luka Jan 09 '14 at 21:53
  • @Luka As long as those threads **do not write** to the *location* of `cat[i]`, you're fine. If you're passing `cat[i]` by value, that's also fine. – Keeler Jan 09 '14 at 21:55
  • If I start 5 threads and I want them to return a value each, is this possible without mutex? – Luka Jan 09 '14 at 21:57
  • As long as they are **just returning (reading) the value and not writing**, then no you do not need a mutex. If you are writing, then you absolutely need a mutex. – Chris Dargis Jan 09 '14 at 22:01
  • The C++ memory model is extremely generous with instruction reordering and stramge architctures: are you folks certain? – Yakk - Adam Nevraumont Jan 09 '14 at 22:12
  • 3
    `// This is OK` - no it isn't, modifying objects declared to be `const` has undefined behavior. It's not a data race, but it's far from "OK." – Casey Jan 09 '14 at 22:13
  • @Casey: I suppose I wasn't specific enough with `// (but not good programming)`. I meant in terms of the write. – Chris Dargis Jan 09 '14 at 22:15
  • @ChrisDargis Correct, you are not specific enough. "But not good programming" is not a clear indication of undefined behavior, especially when preceded by "This is OK." – Casey Jan 09 '14 at 22:19
  • It's not sufficient to say "this is not good programming". It is _worse_ than a data race. You shall simply not do it. – Lightness Races in Orbit Jan 10 '14 at 01:49
  • If you inject even trace amounts of stupid into a program, it will be come ill. – Martin James Jan 10 '14 at 02:07