use*_*437 2 c++ solaris pthreads
我一直在面对这个警告.
我有一个主要的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
[...]
Run Code Online (Sandbox Code Playgroud)
这是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
}
[...]
Run Code Online (Sandbox Code Playgroud)
这些是发送给辅助函数的参数.没有什么相关的,我为了方便您粘贴它们:
[...]
class ThreadParameters {
public:
ThreadParameters(Car*, int);
Car* GetCar();
int GetSlot();
[...]
private:
Car* context;
int slot;
[...]
Run Code Online (Sandbox Code Playgroud)
...最后这是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));
Run Code Online (Sandbox Code Playgroud)
我使用Oracle Solaris Studio 12.3在Solaris 10中对其进行编程.构建项目时,我得到的警告对应于main()中创建线程的行:
"main.cpp",第80行:警告(不合时宜):类型为extern"C"的正式参数3 在调用pthread_create时无效*()(void)(unsigned*,const _pthread_attr*,extern"C"void*() (void),void*)正在传递void*()(void).
代码就像一个魅力,但我在编译时仍然会收到这个警告,老实说,我讨厌没有一个完全干净的编译过程.我找到了几十个使用extern"C"的解决方案,但这些解决方案都没有与我的代码结构相匹配,因此它们都没有为我工作,我想摆脱任何警告(解决它们,而不是隐藏它们) ).
你得到了警告,因为pthread_create()需要一个指向函数的指针C language linkage,但是你传递了Car::GoForward_helper它C++ language linkage.虽然在实践中这很可能会奏效,但这两者实际上是不同的; 例如,C和C++链接函数可以使用不同的调用约定.
不幸的是,类成员(甚至是静态成员)除了C++之外不能拥有语言链接.换句话说,严格按照规则的字母进行操作,不可能将静态成员函数用作期望C函数的回调.
要解决这个问题,你必须使帮助程序成为一个自由函数并为它提供C语言链接:
[...]
class Car {
public:
[...]
void *GoForward(void); //Runs in the thread
[...]
};
extern "C" void* GoForward_helper(void *); //Sends GoForward() to the thread
Run Code Online (Sandbox Code Playgroud)
像这样实现:
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();
}
Run Code Online (Sandbox Code Playgroud)
另一个选项,特别是如果函数需要类似成员的访问,您可以保留静态函数并简单地委托给它:
[...]
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 *);
Run Code Online (Sandbox Code Playgroud)
像这样实现:
extern "C" void* GoForward_helper_C(void *arg) {
return Car::GoForward_helper(arg);
}
Run Code Online (Sandbox Code Playgroud)