c - Simple question on threads and variable copying (with no need of syncing) -
if want pass char* various threads via pthread_create() , want them work independently what's simplest way safely?
i notice unstable behavior if
pseudo code:
func1() { // part of loop create var.. func2(var); } func2(char* var) { // spawn thread pthread_create(.....,&func3,(void*)var); } func3(void* var) { work var // unstable behavior }
there's no need data communication, threads var , ignore rest of program doing. work on var got.
you don't show details of "create var..." part of program, christopher hunt correctly answered, if isn't declared static, go away when func1() returns. worse that, said "no need data communication" , if creating var once , accessing multiple threads having "data communication" whether mean or not (and coordination of parallel access var you).
if want each thread have own copy can mess without disturbing other threads, duplicate before each call pthread_create(). example, call strdup() allocate private copy of string new thread , pass copy pthread_create().
Comments
Post a Comment