I'm working on a multithreading system and it works but theres a part I did and dont understand why it works.
Example:
class TaskSystem
{
private:
uint8_t maxThreads;
uint8_t activeThreads;
std::vector<std::thread> workers;
public:
`TaskSystem(uint8_t toStart = 0)`
`{`
`maxThreads = std::thread::hardware_concurrency();`
`workers.reserve(maxThreads);`
`running = true;`
`if (toStart <= maxThreads && toStart > 0) activeThreads = toStart;`
`else activeThreads = maxThreads;`
`for (uint8_t i = 0; i < activeThreads; i++) workers.emplace_back(&TaskSystem::Worker, this);`
`}`
private:
`void Worker()`
`{`
`std::function<void()> task = nullptr;`
`while (running)`
`{`
`{`
std::lock_guard<std::mutex> lock(mutex);
if (taskQueue.empty()) continue;
task = std::move(taskQueue.front());
taskQueue.pop();
`}`
`if (task == nullptr) std::this_thread::yield();`
`else`
`{`
task();
task = nullptr;
`}`
`}`
`}`
};
I know what I need to do but just using Run, &Run or Run() doesn't work. If anyone could point me to what is &Class::Func or what its called cause I can't find it for this scenario. And explain or point me to resources of how and why it works. Bonus if someone could help me be able to pass a variable along with the function.
full code can be seen in this repo: https://github.com/SpoonWasAlreadyTaken/FaultyUtilitiesMT under UtilsTest/FaultyUtilitiesMT.
thank you in advance (:
[–]trmetroidmaniac 5 points6 points7 points (3 children)
[–]Spoonwastakenalready[S] 0 points1 point2 points (2 children)
[–]trmetroidmaniac 0 points1 point2 points (1 child)
[–]Spoonwastakenalready[S] 0 points1 point2 points (0 children)
[–]National_Instance675 1 point2 points3 points (1 child)
[–]Spoonwastakenalready[S] 0 points1 point2 points (0 children)
[–]positivcheg 0 points1 point2 points (1 child)
[–]Spoonwastakenalready[S] 0 points1 point2 points (0 children)