« first day (2337 days earlier)      last day (740 days later) » 

03:52
@milleniumbug All of these.
 
10 hours later…
14:08
Given the following in C code:

// some .c source file

static int global_env = 0;

static void change_global_env() {
global_env = 1;
}

static void thread_task() {
if (gloval_env == 1) printf("good");
else printf("bad");
}

int main() {
change_global_env();
for(int i < 10) spawn_threads_with_thread_task();
}

Will everything print "good"?
In other words, does all the threads see the updated static global varible?
Because I do something similar and visual studio debugger gives me garbage numbers for the static var in the threads task but im not sure if this is just a fault in the debugger o
Because static global vars are shared between threads, no?
They are not on the thread private stack
nwp
nwp
14:44
They are shared and they will all print "good".
If you call spawn_threads_with_thread_task() before change_global_env() then it's UB.
Maybe debugger's watch isnt reading this correctly.
Inside thread stack sometimes I get garbage for gloval_env watch
nwp
nwp
global_env is not defined in a header, right?
Nope
This is so weird.
If i'm inside a func, a single step that is totaly unrelated to global_var changes its value in watch window
 
2 hours later…
16:33
@Eminem are you calling the task from multiple threads? If so that's undefined behavior in both C and C++
You'd need to use atomic operations to make that not UB.
@nwp so from a language perspective AFAIK it's still UB even if it's just reads. Because AFAIK the memory model assumes that if it's not const and accessed from multiple threads it could be written at any time.
nwp
nwp
Nah, reads from multiple threads are allowed.
I hate to do this... but show me in the standard because AFAIK if it's not const and not atomic multiple access even reads is UB. Because the value may not be correct on the threads
nwp
nwp
It's difficult to prove a negative, but this is what makes data races UB.

« first day (2337 days earlier)      last day (740 days later) »