- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我一直面临这个警告。
我有一个创建多个线程的主 C++ 程序。这些线程运行一个特定类中的函数。为了做到这一点,这个类有一个静态函数,它返回我用来在这些线程中运行的真实函数。我将粘贴我的代码的相关部分,以便您更好地理解它。
这些是 Car.h 的相关部分,其函数在线程中运行的类的头文件:
[...]
class Car {
public:
[...]
void *GoForward(void); //Runs in the thread
static void* GoForward_helper(void *); //Sends the previous one to the thread
[...]
这是 Car.cpp 中作为参数传递给 pthread_create 的函数,它返回真正在线程中运行的函数。这是我发现在线程中运行不同类的函数的唯一方法:
[...]
void *Car::GoForward_helper(void *context) {
//Just getting parameters, not big deal
ThreadParameters* parameters = (ThreadParameters*) context;
int newSlot = parameters->GetSlot();
parameters->GetCar()->setSlot(newSlot);
//Returns the function I want to run in the thread
return parameters->GetCar()->GoForward();
}
[...]
void* Car::GoForward(void) {
//Tasks which actually run in the thread
}
[...]
这些是发送到辅助函数的参数。没有什么相关的,为了您的方便,我粘贴它们:
[...]
class ThreadParameters {
public:
ThreadParameters(Car*, int);
Car* GetCar();
int GetSlot();
[...]
private:
Car* context;
int slot;
[...]
...最后这是创建线程的 main() 中的代码:
[...]
vector<Car *> cars; //Pointers to objects whose functions I want to run in threads
int threadsLaunched = 0;
pthread_t threads[numberOfThreads];
vector<int> freeSlots; //The number of threads is limited
[...]
int slot = freeSlots.at(0);
threadCode = pthread_create(&threads[slot], NULL,
&Car::GoForward_helper,
new ThreadParameters(cars.at(threadsLaunched), slot));
我正在使用 Oracle Solaris Studio 12.3 在 Solaris 10 中对其进行编程。构建项目时,我收到与 main() 中创建线程的行相对应的警告:
"main.cpp", line 80: Warning (Anachronism): Formal argument 3 of type extern "C" void*()(void) in call to pthread_create(unsigned*, const _pthread_attr*, extern "C" void*()(void), void*) is being passed void*()(void).
代码运行起来非常棒,但我在编译时仍然收到此警告,老实说,我讨厌没有一个完全干净的编译过程。我已经找到了许多使用 extern "C"的解决方案,但是这些解决方案都不符合我的代码结构,所以它们都不适合我,我想摆脱任何警告(解决它们,而不是隐藏它们).
最佳答案
你收到警告是因为 pthread_create()
需要一个指向函数的指针 C language linkage
,但你传递了它 Car::GoForward_helper
具有 C++ 语言链接
。虽然在实践中这很可能会奏效,但这两者实际上是不同的;例如,C 和 C++ 链接函数可能使用不同的调用约定。
不幸的是,类成员(即使是静态成员)不能有 C++ 以外的语言链接。换句话说,严格遵守规则,不可能在需要 C 函数的地方使用静态成员函数作为回调。
要解决这个问题,必须让helper成为一个free function,并赋予它C语言的linkage:
[...]
class Car {
public:
[...]
void *GoForward(void); //Runs in the thread
[...]
};
extern "C" void* GoForward_helper(void *); //Sends GoForward() to the thread
这样实现:
extern "C" void *GoForward_helper(void *context) {
//Just getting parameters, not big deal
ThreadParameters* parameters = (ThreadParameters*) context;
int newSlot = parameters->GetSlot();
parameters->GetCar()->setSlot(newSlot);
//Returns the function I want to run in the thread
return parameters->GetCar()->GoForward();
}
一个额外的选择,特别是如果函数需要类似成员的访问,你可以保留静态的并简单地委托(delegate)给它:
[...]
class Car {
public:
[...]
void *GoForward(void); //Runs in the thread
static void* GoForward_helper(void *); //Sends the previous one to the thread
[...]
};
extern "C" void* GoForward_helper_C(void *);
这样实现:
extern "C" void* GoForward_helper_C(void *arg) {
return Car::GoForward_helper(arg);
}
关于C++:在类中使用静态函数从 main 创建 pthread 时,Solaris Studio 中出现警告(时代错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422857/
我正在将一些 pthreads 代码添加到我使用 autotools 构建的 Linux 应用程序中。我收到关于未在 libpthreads 中链接的错误。所以我想在 autotools 中指定 pt
libpthread 库位于 Linux 系统的哪个目录中? 最佳答案 有多种方法可以找出这一点。 只需输入 find / -name 'libpthread.so' -print找到名为 libpt
pthread 属性对象是否需要在使用它们的对象的生命周期内存在,或者在使用它们后立即销毁它们是否安全?例如: // Create the mutex attributes. pthread_mute
到目前为止我读过的所有文档似乎都表明我的 vxWorks (6.8) 版本中存在 posix 线程支持,但是一个简单的测试应用程序无法按预期执行。来源如下: tTest.h #include cla
我试图找到指定 pthreads 标准的文档。我见过各种指向 IEEE 1003.1c-1995 的链接(即 Wikipedia 或 OpenGroup )。然而,当我在 IEEE 标准站点上搜索此文
我试图找到指定 pthreads 标准的文档。我见过各种指向 IEEE 1003.1c-1995 的链接(即 Wikipedia 或 OpenGroup )。然而,当我在 IEEE 标准站点上搜索此文
我在 MSVC 2010 上运行一个 pthread,我已经包含 pthreadVC2 .lib & .dll。来自以下网站 http://sourceware.org/pthreads-win32/
我的问题是: 如何在不更改其他 pthread 中的当前目录的情况下更改 pthread 中的当前目录,我找到了一个使用 openat() 函数的解决方案,但我没有找到任何解释它如何工作的示例。 使用
是否可以通过任何方式更改进程可以创建的 pthread 数量限制? 目前在我的 linux 系统上我可以创建大约 380 个线程,但我想增加它,只要内存可用。 最佳答案 减少用户的堆栈大小' ulim
问候。我正在尝试创建一个 autoconf 配置脚本,该脚本自动检查要使用的 pthread 选项,并且理想情况下,在使用 gcc 编译时指定 -pthread。 我希望 AX_PTHREAD 能够工
如何知道 pthread 是否死亡? 有办法检查 pthread 状态吗? 最佳答案 if(pthread_kill(the_thread, 0) == 0) { /* still runni
我正在从一个由互斥锁控制的固定大小的全局池中分配我的 pthread 线程特定数据。 (有问题的代码不允许动态分配内存;它允许使用的所有内存都由调用者作为单个缓冲区提供。pthreads 可能会分配内
在阅读了一些 MPI 规范后,我了解到,当使用 MPI_THREAD_SERIALIZED 进行初始化时,程序必须确保发生在不同线程中的 MPI_Send/Recv 调用不能重叠。换句话说,您需要一个
我尝试根据 this guide 安装 pthread win32 . 我将 pthreadVC2.dll 文件添加到 C:\Windows 并将 pthreadVC2.lib 文件添加到 C:\Pr
我有一个 pthreads 程序。我必须使用 Linux 中的 gcc -pthread(-pthreads 是无法识别的选项)和 Sun 中的 gcc -pthreads(-pthread 是无法识
我有一个包含文件名列表的文件,我想在其中搜索一个词并替换它我稍微修改了代码只是为了在这里只显示相关部分问题是如果我在该列表中只有一个文件,它不会用多线程处理它,因为线程只有在我有多个文件时才工作所以我
我正在编写一个 SMT 程序,并且正在尝试解决一个有趣的问题。 我需要所有函数一起退出,但是有些线程卡在障碍物上,即使我不希望它们这样做。 我的问题是:当我删除障碍时会发生什么?卡在屏障处的线程会释放
我阅读了有关 pthread 及其相关 API 的所有内容,以创建、锁定和同步不同的线程。但我经常发现线程池、消费者/生产者等词提示。我理解这些是 pthread 实现的模型。 任何人都可以让我知道
我在 man pthread_join 中读到,多个 pthread 不能加入一个已经加入的 pthread。还有另一种方法可以达到相同的结果吗?多个 pthread 挂起自己,直到某个特定的 pth
我知道 OpenMP 实际上只是一组编译成 pthread 的宏。有没有办法在编译的其余部分发生之前查看 pthread 代码?我正在使用 GCC 进行编译。 最佳答案 首先,OpenMP 不是一组简
我是一名优秀的程序员,十分优秀!