gpt4 book ai didi

c++ - 为什么不在 pop 之后运行任务并返回 true?

转载 作者:行者123 更新时间:2023-11-28 08:15:24 25 4
gpt4 key购买 nike

我运行以下来自书籍 c++ concurrency in action 的示例,并通过其提交传递函数 a、b 和 c,它打印 pop 并确定返回 true,但为什么不打印“开始任务”似乎没有运行 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));
}

最佳答案

修复所有编译错误后,在 Linux 上使用 GCC 4.6.1 编译并运行,产生以下输出:

begin
push 0push 1push 2push 3push 4push 5submit start
submit finish
poptask start
a
task end
poptask start
b
task end
poptask start
c
task end

在那之后,它会一直处于无限循环中,因为 done 永远不会变成 true

看起来实际上打印了“任务开始”。

关于c++ - 为什么不在 pop 之后运行任务并返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com