带参数的类的 pthread 成员函数
我使用本页底部的代码成功地将线程附加到类成员: http:// www.tuxtips.org/?p=5。
我不知道如何扩展代码来封装诸如 void*atom(void *inst
) 之类的方法,其中 *inst
是包含各种线程参数的结构。具体来说,Netbeans 和我都不了解 example::*f
的定义位置以及它如何在 thread_maker
范围内有效。
I was successful at attaching a thread to class member using the code on the bottom of this page: http://www.tuxtips.org/?p=5.
I can't figure out how to expand the code to encapsulate a method such as void* atom(void *inst
) where *inst
is a structure that contains various thread parameters. Specifically neither Netbeans nor I understand where the example::*f
is defined and how it can be valid in the thread_maker
scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为使用 pthread(需要 c 回调)等东西的更好解决方案是创建一个包装函数,以便您可以更轻松地使用 boost::functions 来操作。这类似于 使用 boost::bind() C 代码,它能工作吗?。
然后你可以简单地用 boost::bind 解决你的问题
包装函数看起来像这样。
虽然此方法可能比手动传递数据需要更多工作,但它的可扩展性更高,可以轻松绑定任何内容并传递它来启动线程。
编辑
最后一点:myClassInstance 和 myStructInstance 应该位于堆上。如果它们在堆栈上,它们可能会在线程启动之前被删除。
I think a better solution for using things such as pthread(which take c callbacks) is to create a wrapper function so that you can use much easier to manipulate boost::functions instead. This is similar to Using boost::bind() across C code, will it work?.
Then you could solve your problem simply with boost::bind
The wrapper function would look like this.
While this method may be more work than manually passing in data, it is much more extendable, making it easy to bind anything and pass it to start your thread.
EDIT
One last note: myClassInstance and myStructInstance should be on the heap. If they are on the stack they could get deleted before your thread starts.