简体   繁体   English

可以通过线程修改pthread_create参数吗?

[英]can pthread_create argument be modified by thread?

I can pass struct into pthread_create as thread argument. 我可以将struct作为线程参数传递到pthread_create中。

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? 如果是这样,我是否需要将该结构强制转换回线程中的空白?

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. 线程函数获取一个void *参数。 It will cast that into a struct whatever * ; 它将struct whatever *转换为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. 只要确保如果有任何其他线程可以修改结构,那么您就可以正确地协调更改。

Yes it is possible, just don't touch the data until the thread is done. 是的,有可能,只是在线程完成之前不要触摸数据。 At least not without protection. 至少不是没有保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM