带参数的类的 pthread 成员函数

发布于 2024-12-21 01:56:21 字数 308 浏览 3 评论 0原文

我使用本页底部的代码成功地将线程附加到类成员: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-12-28 01:56:21

我认为使用 pthread(需要 c 回调)等东西的更好解决方案是创建一个包装函数,以便您可以更轻松地使用 boost::functions 来操作。这类似于 使用 boost::bind() C 代码,它能工作吗?

然后你可以简单地用 boost::bind 解决你的问题

class myClass
{
    void atom(myStruct *data); // void return type to keep it similar to other code
    // You could change it to a void* return type, but then you would need to change the boost::function declarations
};

boost::function<void(void)> function = boost::bind(&myClass::atom,&myClassInstance,&myStructInstance); //bind your function
boost::function<void(void)>* functionCopy = new boost::function<void(void)> (function); //create a copy on the heap

pthread_t id;
pthread_create(&id,NULL,&functionWrapper,functionCopy);

包装函数看起来像这样。

void functionWrapper(void* data)
{

   boost::function<void(void)> *func = (boost::function<void(void)>* ) (data);
   (*func)();
   delete(func);
}

虽然此方法可能比手动传递数据需要更多工作,但它的可扩展性更高,可以轻松绑定任何内容并传递它来启动线程。

编辑

最后一点: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

class myClass
{
    void atom(myStruct *data); // void return type to keep it similar to other code
    // You could change it to a void* return type, but then you would need to change the boost::function declarations
};

boost::function<void(void)> function = boost::bind(&myClass::atom,&myClassInstance,&myStructInstance); //bind your function
boost::function<void(void)>* functionCopy = new boost::function<void(void)> (function); //create a copy on the heap

pthread_t id;
pthread_create(&id,NULL,&functionWrapper,functionCopy);

The wrapper function would look like this.

void functionWrapper(void* data)
{

   boost::function<void(void)> *func = (boost::function<void(void)>* ) (data);
   (*func)();
   delete(func);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文