Reputation: 77
Here is function execute(), used for some instructions:
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();
Using this function execute(), I want to create multiple instances of threads. I think this is a possibility to declare the threads (but when I am write this code I am getting some errors - INVOKE ERROR C2672) --- MODIFIED and now working
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
My intention is to use the execute() function (threads) to execute parallel instruction instead of linear instructions - functionable parameter.
Thanks, F.
Upvotes: 2
Views: 1072
Reputation: 36488
Assuming processor::execute
is a static member function with no arguments then you are passing an extra argument to it so the std::thread
implementation can't find an overload with the correct arguments. The correct invocation is:
threads.push_back(thread(&processor::execute));
or more simply:
threads.emplace_back(&processor::execute);
If it isn't a static method then you need to pass an instance of your processor class, e.g.:
processor p;
for (int i = 0; i <= 5; i++)
{
threads.emplace_back(&processor::execute, &p);
}
Judging by printing "Synchronizing all threads"
I think you don't understand what std::thread::detach
does, it detaches the thread from the std::thread
instance so that it can continue running once the structure is destroyed. I assume you actually meant to call std::thread::join
which waits for the thread to finish executing. std::thread::detach
is rarely the correct thing to do.
Upvotes: 5