2
votes

I can pass struct into pthread_create as thread argument.

But is it possible to modify this struct within the thread so that when the thread terminates, the main program can access this struct to obtain modified data? If so, do I need to cast the struct back to void in the thread?

2

2 Answers

3
votes

Yes - assuming the thread function is passed the structure by pointer, you can modify the structure it points at.

No - there is no need to cast the struct back to void in the thread; indeed, I'm not quite sure what you're thinking of, but there's no need for it. The thread function gets a void * argument. It will cast that into a struct whatever *; it can then reference that.

Just make sure that if any other threads could modify the structure, then you are coordinating the changes properly.

1
votes

Yes it is possible, just don't touch the data until the thread is done. At least not without protection.