я запускаю следующий пример из книги c ++, параллелизм в действии, и передаю функции a, b и c с ее отправкой, она печатает pop и обязательно возвращает true, но почему бы не вывести «start task»
похоже не запускается задача ()
#include <thread>
#include <atomic>
#include <vector>
#include <queue>
class join_threads
{
std::vector<std::thread>& threads;
public:
explicit join_threads(std::vector<std::thread>& threads_):threads(threads_)
{}
~join_threads()
{
for(unsigned long i=0;i<threads.size();++i)
{
if(threads[i].joinable())
threads[i].join();
}
}
};
template<typename T>
class thread_safe_queue
{
private:
mutable std::mutex mut;
std::queue<T> data_queue;
std::condition_variable data_cond;
public:
thread_safe_queue(){}
thread_safe_queue(thread_safe_queue const& other)
{
std::lock_guard<std::mutex> lk(other.mut);
data_queue=other.data_queue;
}
void push(T new_value)
{
std::lock_guard<std::mutex> lk(mut);
data_queue.push(new_value);
data_cond.notify_one();
}
void wait_and_pop(T& value)
{
std::unique_lock<std::mutex> lk(mut);
//data_cond.wait(lk,[this]{return !data_queue.empty();});
data_cond.wait(lk);
value=data_queue.front();
data_queue.pop();
}
std::shared_ptr<T> wait_and_pop()
{
std::unique_lock<std::mutex> lk(mut);
//data_cond.wait(lk,[this]{return !data_queue.empty();});
data_cond.wait(lk);
std::shared_ptr<T> res(new T(data_queue.front()));
data_queue.pop();
return res;
}
bool try_pop(T& value)
{
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return false;
value=data_queue.front();
data_queue.pop();
printf("pop");
return true;
}
std::shared_ptr<T> try_pop()
{
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return std::shared_ptr<T>();
std::shared_ptr<T> res(new T(data_queue.front()));
data_queue.pop();
return res;
}
bool empty() const
{
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
};
class thread_pool
{
std::atomic_bool done;
thread_safe_queue<std::function<void()> > work_queue;
std::vector<std::thread> threads;
join_threads joiner;
void worker_thread()
{
while(!done)
{
//printf("workerthread");
std::function<void()> task;
if(work_queue.try_pop(task))
{
printf("task start\n");
task();
printf("task end\n");
}
else
{
std::this_thread::yield();
}
}
}
public:
thread_pool() : joiner(threads),done(false)
{
unsigned const thread_count=std::thread::hardware_concurrency();
try
{
for(unsigned i=0;i<6;++i)
{
printf("push %d",i);
threads.push_back(std::thread(&thread_pool::worker_thread,this));
}
}
catch(std::bad_alloc)
{
done=true;
throw;
}
}
~thread_pool()
{
done=true;
}
template<typename FunctionType>
void submit(FunctionType f)
{
work_queue.push(std::function<void()>(f));
}
};
void a()
{
//while(true)
{
printf("a\n");
}
}
void b()
{
//while(true)
{
printf("b\n");
}
}
void c()
{
//while(true)
{
printf("c\n");
}
}
int main()
{
printf("begin\n");
thread_pool* pool = new thread_pool(); //start thread pool
printf("submit start\n");
pool->submit((*a)); // pass function to queue
pool->submit((*b));
pool->submit((*c));
printf("submit finish\n");
while(pool->done == false)
std::this_thread::sleep(std::milliseconds(1));
}