C++ 中的 QNX 面向对象线程
我想使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅“如何传递指针-信号处理程序的成员函数、X 事件回调、启动线程/任务的系统调用等?”。
简而言之,您传递一个静态函数(“trampoline”)作为函数指针。 您将“this”作为用户定义的参数传递。 然后,静态函数将调用弹回到真实对象。
例如:
并且您需要确保 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:
And you need to ensure that the C++ function has the same calling convention as expected by pthread_create.
您的
functionA
不是指向函数的指针,它是返回void*
的函数。 该函数还应该采用 void* 作为参数。 该参数用于传递指向线程中所需数据的指针。如果你替换
为
我希望它可以在 C 和 C++ 中工作。
Your
functionA
is not a pointer to a function, it's a function returning avoid*
. 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
with
I'd expect it to work in both C and C++.
我认为您需要将
functionA
声明为extern "C"
(并为其提供正确的签名,请参阅 QNX 上的 pthread 文档)。 该函数将this
实例作为用户参数:I think you need to declare
functionA
as beingextern "C"
(and give it the correct signature, see the documentation for pthreads on QNX). This function takes as user parameter thethis
intance: