将普通数据传递给 pthread void *
pthread 函数采用 void * 参数。如何发送普通结构而不是指针?
我想向一个 pthread 函数发送一个非指针结构。
另外我想发送一个指向 void * 函数的指针,这是如何完成的?可以将任何指针发送到 void * 函数吗?
The pthread functions take a void * argument. How can a plain struct, not a pointer be sent in?
I want to send in a non pointer struct to one pthread function.
Also I want to send a pointer to the void * function, how is this done? can any pointer be sent in to the void * function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可能;你必须发送一个指针。然而,
void *
可以指向任何东西。如果您的结构变量名为foo
,您可以简单地将其作为(void *) &foo
传递,并且在函数内部,您可以将其转换回例如 < code>struct Foo 与struct Foo * fooPtr = (struct Foo *) param;
或struct Foo foo = *((struct Foo *) param);
。编辑:正如 @forsvarir 在评论中提到的,
foo
不能是局部变量(除非调用函数等待线程完成)。请参阅@Gavin Lock 的帖子。Not possible; you have to send a pointer. However, a
void *
can point to anything. If your struct variable is calledfoo
, you can simply pass it as(void *) &foo
, and inside the function, you can cast it back into e.g. astruct Foo
withstruct Foo * fooPtr = (struct Foo *) param;
orstruct Foo foo = *((struct Foo *) param);
.Edit: As @forsvarir mentioned in the comment,
foo
must not be a local variable (unless the invoking function waits for the thread to complete). See @Gavin Lock's post.根据您的评论,您需要做这样的事情...
在您的主代码中:
在您的看门狗线程中:
在您的导航线程中:
您不能只是这样做:
因为
myStruct
将会被清理。 ..当PassSomeStuff
返回时。获取地址(获取指向它的指针)不会复制对象。注意:
Based on your comments you need to do something like this...
In your main code:
In your watchdog thread:
In your navigation thread:
You can't just do:
Because
myStruct
will get cleaned up... whenPassSomeStuff
returns. Taking the address (getting a pointer to it), doesn't copy the object.Note:
正如已经提到的,您必须传递一个指针。将 void* 视为无类型指针,因此您必须将其转换回线程函数内的正确类型。 (参见 Aasmund 的回答)
正如 forsvarir 提到的,您必须确保指向的结构在线程使用它之前不会被破坏 - 最安全的方法是在堆上新建结构并将其地址和所有权传递给线程函数。
我所说的“传递所有权”的意思是,传递结构体消息的函数不得删除它,而线程函数必须在使用完该结构体后删除该结构体。
As already mentioned, you have to pass a pointer. Think of void* as an untyped pointer, so you have to cast it back to the correct type inside your thread function. (see Aasmund's answer)
As forsvarir mentions, you HAVE to make sure that the pointed-to struct is not destroyed before the thread uses it - safest way to do this is to new the struct on the heap and pass its address and ownership to the thread function.
What I mean by "passing ownership" is that the function that news the struct must not delete it, and the thread function must delete the struct once it is done with it.
这不是一个完整的答案,而是其他人提供的关于确保新线程获取结构时结构仍然存在的警告的替代解决方案。当然,您可以使用
malloc
来获取它,并让新线程负责释放
它。在许多方面,这似乎是最简单和最便宜的方法(不需要同步),但同步实际上隐藏在malloc
和free
内部,并且可能会稍微昂贵,特别是因为当释放内存的线程与分配内存的线程不同时,大多数面向线程的分配器(例如 ptmalloc 和 tcmalloc)会产生额外的成本。您可以使用的另一种方法是将 pthread 屏障放入 init 结构中,并等待它:
并让线程启动函数在复制结构后也调用
pthread_barrier_wait(arg->barrier);
到它自己的自动存储。This isn't a complete answer, but rather an alternative solution to the warnings others have offered about making sure the structure still exists when the new thread gets it. Certainly you can use
malloc
to obtain it, and give the new thread the responsibility forfree
ing it. In many ways this seems like the simplest and cheapest way (no synchronization required), but the synchronization is actually hidden insidemalloc
andfree
, and could be mildly expensive, especially since most thread-oriented allocators (ptmalloc and tcmalloc for example) incur additional costs when the thread freeing the memory is not the same as the thread that allocated it.An different approach you can use is to put a pthread barrier inside your init structure, and wait on it:
And have the thread start function also call
pthread_barrier_wait(arg->barrier);
after copying the struct to its own automatic storage.