这是函数execute(),用于一些指令:
void execute() {
while (run) { //thread is running
if (time % 3 == 0) { // execute instructions when clock is 3
Instruct Instr;
uint16_t src1 = 0;
uint16_t src2 = 0;
int target_cycle = time;
while (target_cycle > time) {
std::this_thread::sleep_for(thread_sleep);
}
while (hpp_DE_EX.size() != 0) {
Instr = hpp_DE_EX.front();
hpp_DE_EX.pop();
uint16_t instr = Instr.header;
ptrLog->PrintData(get, instr);
src2 = instr & 0x1F;
src1 = (instr >> 0x5) & 0x1F;
uint16_t opcode = (instr >> 0xA) & 0x3F;
....
}
//For running this thread:
auto exThread = std::thread(&processor::execute, this);
exThread.detach();
使用此函数execute(),我想创建多个线程实例。我认为这是一种声明线程的可能性(但是当我写这段代码时我得到了一些错误 - INVOKE ERROR C2672)---修改后现在正在工作
std::vector<std::thread> threads;
for (int i = 0; i <= 5; i++) // need 5 instances
threads.push_back(thread(&processor::execute, this));
cout << "Synchronizing all threads...\n";
for (auto& th : threads) th.join(); // Running code
我的目的是使用execute()函数(线程)来执行并行指令而不是线性指令 - 可操作的参数。
谢谢,F。
假设processor::execute
是一个没有参数的静态成员函数,那么你将向它传递一个额外的参数,因此std::thread
实现无法找到具有正确参数的重载。正确的调用是:
threads.push_back(thread(&processor::execute));
或更简单地说:
threads.emplace_back(&processor::execute);
如果它不是静态方法,则需要传递处理器类的实例,例如:
processor p;
for (int i = 0; i <= 5; i++)
{
threads.emplace_back(&processor::execute, &p);
}
通过打印"Synchronizing all threads"
来判断我认为你不明白std::thread::detach
做了什么,它将线程与std::thread
实例分离,以便一旦结构被破坏它就可以继续运行。我假设你实际上打算调用等待线程完成执行的std::thread::join
。 std::thread::detach
很少是正确的事情。