C++ 中的 QNX 面向对象线程

发布于 2024-07-11 12:53:42 字数 318 浏览 5 评论 0原文

我想使用 C++ 和线程在 QNX 中创建一个并行的面向对象系统。 我该怎么做呢?

我尝试过:

pthread_t our_thread_id;
pthread_create(&our_thread_id, NULL, &functionA ,NULL);

函数 A 是指向函数的指针:

void *functionA()
{ //do something
}

但是该函数仅适用于 C 而不适用于 C++。 我怎样才能让它在 C++ 中工作?

I want to create a parallel object oriented system in QNX using c++ and threads. How do I do this?

I tried:

pthread_t our_thread_id;
pthread_create(&our_thread_id, NULL, &functionA ,NULL);

with function A being a pointer to a function:

void *functionA()
{ //do something
}

However this function only works in C and not C++. How can I make it work in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

满天都是小星星 2024-07-18 12:53:42

请参阅“如何传递指针-信号处理程序的成员函数、X 事件回调、启动线程/任务的系统调用等?”

简而言之,您传递一个静态函数(“trampoline”)作为函数指针。 您将“this”作为用户定义的参数传递。 然后,静态函数将调用弹回到真实对象。

例如:

class Thread {
public:
    int Create()
    {
        return pthread_create(&m_id, NULL, start_routine_trampoline, this);
    }

protected:
    virtual void *start_routine() = 0;

private:
    static void *start_routine_trampoline(void *p)
    {
        Thread *pThis = (Thread *)p;
        return pThis->start_routine();
    }
};

并且您需要确保 C++ 函数具有与 pthread_create 所期望的相同的调用约定。

See "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc?".

In short, you pass a static function (the "trampoline") as the function pointer. You pass "this" as the user-defined parameter. The static function then bounces the call back to the real object.

For example:

class Thread {
public:
    int Create()
    {
        return pthread_create(&m_id, NULL, start_routine_trampoline, this);
    }

protected:
    virtual void *start_routine() = 0;

private:
    static void *start_routine_trampoline(void *p)
    {
        Thread *pThis = (Thread *)p;
        return pThis->start_routine();
    }
};

And you need to ensure that the C++ function has the same calling convention as expected by pthread_create.

鱼忆七猫命九 2024-07-18 12:53:42

您的 functionA 不是指向函数的指针,它是返回 void* 的函数。 该函数还应该采用 void* 作为参数。 该参数用于传递指向线程中所需数据的指针。

如果你替换

void* functionA() {

void functionA(void* threadData) {

我希望它可以在 C 和 C++ 中工作。

Your functionA is not a pointer to a function, it's a function returning a void*. The function is also expected to take a void* as argument. This argument is used to pass a pointer to the data needed in the thread.

If you replace

void* functionA() {

with

void functionA(void* threadData) {

I'd expect it to work in both C and C++.

渡你暖光 2024-07-18 12:53:42

我认为您需要将 functionA 声明为 extern "C" (并为其提供正确的签名,请参阅 QNX 上的 pthread 文档)。 该函数将 this 实例作为用户参数:

extern "C"
{
   void* DoSomethingInAThread(void* pGenericThis)
   {
     YourClass* pThis = reinterpret_cast<YourClass*>(pGenericThis);
     pThis->DoSomethingInAThread();
   }
}

int main()
{
  YourClass* pSomeInstance = new YourClass();
  pthread_t our_thread_id;
  pthread_create(&our_thread_id, NULL, &DoSomethingInAThread, pSomeInstance);
}

I think you need to declare functionA as being extern "C" (and give it the correct signature, see the documentation for pthreads on QNX). This function takes as user parameter the this intance:

extern "C"
{
   void* DoSomethingInAThread(void* pGenericThis)
   {
     YourClass* pThis = reinterpret_cast<YourClass*>(pGenericThis);
     pThis->DoSomethingInAThread();
   }
}

int main()
{
  YourClass* pSomeInstance = new YourClass();
  pthread_t our_thread_id;
  pthread_create(&our_thread_id, NULL, &DoSomethingInAThread, pSomeInstance);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文