Reputation: 26531
I'm writing some Win32 code like this:
myctx.somedata = 42;
CreateThread(blablabla, &my_thread_entry_func, &myctx);
Can I expect the newly created thread to see the effect of myctx.somedata = 42
as soon as it comes to life? And importantly, how could I have managed to look this up myself?
And while we're at it, how do things behave with pthreads
under Linux?
Upvotes: 2
Views: 164
Reputation: 33754
internally with thread will be associated some structure, where os store thread data. this is for any os.
what is CreateThread (or any analog function) is doing ? it first create and initialize this structure and then push it in some internal scheduler database.
then scheduler (on arbitrary core) pop thread structure from this database and begin execute it code.
obvious than after scheduler pop thread data from database - it must view all data written to thread data before push. this mean that push must have how minimum release semantic and pop acquire semantic.
because myctx.somedata = 42;
happens-before thread data is created, it of course also will be visible at point where thread data will be pop by scheduler. your user thread code will be executed after scheduler pop your thread data. so it of course will be view myctx.somedata = 42;
too.
Upvotes: 2