我正在使用VS2019
想在DLL中调用线程。与分离同时运行两个可执行文件
当我运行普通的c ++程序时,以下线程工作了
我得到错误
错误C3867'myClass :: runexeone':非标准语法;使用“&”创建指向成员myGateway C:\ Users \ user \ Downloads \ Demo \ myGateway \ myplugin.cpp的指针
插件头
#include <windows.h>
#include <iostream>
#include <thread>
#define MYPLUGIN_EXPORT __declspec(dllexport)
extern "C"
{
MYPLUGIN_EXPORT void WINAPI OnStart();
}
插件.cpp
#include "plugin.h"
using namespace std;
class myClass
{
public:
myClass()
{
}
~myClass()
{
}
void onStart()
{
std::thread(runexeone).detach();
std::thread(runexetwo).detach();
}
void runexeone()
{
int exerunpne = system("cmd /C \"%MY_ROOT%\\bin\\Mytest.exe\" -ORBEndpoint iiop://localhost:12345 -d");
}
void runexetwo()
{
int exeruntwo = system("cmd /C \"%MY_ROOT%\\bin\\Mytest_2.exe\" -ORBEndpoint iiop://localhost:12345 -d");
}
};
myClass& getmyclass()
{
static myClass myclass;
return myclass;
}
MYPLUGIN_EXPORT void WINAPI OnStart()
{
getmyClass().onStart();
}
正如MSalters所述,你为std :: thread的函子提供了错误的数据类型。如果你不能将方法设为静态(实际上至少可以针对代码的当前状态使用该方法,但是请不要在此处声明),则可以这样做。
void onStart()
{
std::thread(std::bind(&myClass::runexeone, this)).detach();
}
但是要注意对象/对象的寿命/存在!
我会用的TBH
[this]() { runexeone(); }
。我发现lambda提供了更好的错误消息。但是严格来说,甚至没有必要,std::thread::thread
它将为您完成绑定。IEstd::thread(&myClass::runexeone, this).detach();
是的,但至少对于以前的编译器而言,有时需要对使用lambda的参数转发进行详细的说明。std :: bind的std :: placeholder在这里是一个不错的样板还原器:)感谢有关std :: thread自身功能的语法糖!