c - Creating a thread -
i know can create thread within program createthread()
, need waitforsingleobject()
finish before returning original program.
my question is, original program thread well? say, have variable a=3
in program, create thread. second thread, can access variable a
?
when windows o/s starts process create first thread process.
so, "original program" wrong name. initial item executing progra m thread.
when variable scope allows access second thread can access it. if want pass access specific variable can use appropriate parameter createthread
pass pointer it. beware: when execution of original thread goes out of scope of variable pointer gets invalid. recommend consider redesign.
edit requested, here example variable going out of scope. wrong implementation:
dword thethread(void*p) { int *pcounter = (int*)p; // pcounter } handle makeitrun() { int counter; return createthread(0, 0, &thethread, &counter, 0, 0); } // scope of counter ends here int main() { handle h; h = makeitrun(); waitforsingleobject(h, infinite); }
Comments
Post a Comment