C++:在类中使用静态函数从 main 创建 pthread 时,Solaris Studio 中出现警告(时代错误)

标签 c++ pthreads solaris

我一直面临这个警告。

我有一个创建多个线程的主 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/

相关文章:

c++ - 如何正确格式化 C++ 注释?

C pthread : Multiple Threads but only ONE thread is used

python - 使用 curses 支持编译 Python

linux - Unix 上的应用程序控制脚本

linux - 在 Linux 中寻找 kstat 等价物

c++ - 有什么方法可以告诉链接器 "respect"__attribute__((__used__))

c++ - 定义新的中缀运算符

c - 在 C 中使用 mutex 和 barrier 进行线程同步

c - 向等待锁的线程发出信号,表明该锁已变得无关紧要

c++ - 在 OpenGL 中使用 vbo 绘制圆柱体