arrays - Why isn't my string being passed properly to this thread-invoked function? -
i working on multithreaded application in client program generates request threads send strings data server program, answers sending strings back. unfortunately, having trouble passing data client's main function function invoked each request thread. in main, request threads going (some previous variable declarations not shown):
for(int j = 0; j < persons.size(); j++){ //requests deal imaginary people thread_args* dat = new thread_args; strcpy(dat->str, persons[j].c_str()); cout << "***i copied " << dat->str << " dat->str!!!"<<endl; //prints expected pthread_create(&request_thread[j],null,&deposit_requests, &dat); }
specifically, structure using pass things threads looks this:
typedef struct str_thdata{ char str[100]; } thread_args;
now, in cout within main ("***i copied..."), , good. correct value indeed copied in. when actual function invoked thread, though, string lost:
void* deposit_requests(void* str_arg){ for(int = 0; < req_per_person; i++){ thread_args* data = (thread_args*)str_arg; cout << "data->str " << data->str << endl; //empty string!!! ... } }
my idea maybe going out of scope? unless being blind here, don't see happen. or maybe need additional casting somewhere? tried few variations within second cout (e.g., using ((thread_args*)data)->str) ), didn't seem work. happening character array?
that &dat pass in pthread_create address of pointer thread_args struct, not address of thread_args struct.
pass dat without & pthread_create, , code deposit_requests should run fine.
pthread_create(&request_thread[j],null,&deposit_requests, dat);
Comments
Post a Comment