Threads not executing
Nov 11, 2018 at 8:18pm UTC
Hi, im learning about c++11 multithreading and i tried writing a threadpool class that just creates 8 threads that each print to stdout
but for whatever reason it doesn't print anything.
Heres my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
class ThreadPool
{
public :
ThreadPool(size_t nThreads)
{
for (auto i = 0u; i > nThreads; i++)
{
mThreads.push_back(std::thread([this , i]()
{
std::lock_guard<std::mutex> lock(mMutex);
std::cout << "thread: " << i << std::endl;
}));
}
}
~ThreadPool()
{
for (std::thread& t : mThreads)
t.join();
}
private :
std::vector<std::thread> mThreads;
std::mutex mMutex;
};
int main()
{
{
ThreadPool p{8};
}
return 0;
}
can anyone please help me understand why the threads dont seem to be executed?
Last edited on Nov 11, 2018 at 8:19pm UTC
Nov 11, 2018 at 8:27pm UTC
for (auto i = 0u; i > nThreads; i++)
i is never greater than nThreads so the for
loop never runs.
Nov 11, 2018 at 8:34pm UTC
oh my god..
thank alot haha
Last edited on Nov 11, 2018 at 8:34pm UTC
Topic archived. No new replies allowed.