问题描述
我有一个函数可以将某些行追加到Gtk :: TreeView上,它基于documentation中的一个:
Gtk::TreeModel::Row row;
for(const auto& i : some_vector)
{
row = *(reference_to_list_store->append());
row[my_column_record.some_value] = i;
}
我的代码库中的函数与此代码类似,但是它要填充的单元格更多,并且在某些情况下向量可能非常大,在最坏的情况下,该函数需要12到14s才能运行,并使单个核变为100 %。因此,我尝试使用更多线程来实现此功能:
std::mutex mtx;
// That is need to avoid std::future's destructor from blocking the execution.
std::vector<std::future<void>> res;
for(const auto& i : some_vector)
{
res.emplace_back(std::async(std::launch::async,[&](){
Gtk::TreeModel::Row row;
{
std::lock_guard<std::mutex> lk(mtx);
row = *(reference_to_list_store->append());
}
row[my_column_record.some_value] = i;
}));
}
对我来说,这个函数看起来不错,但是有时候,当数组很大时,这个函数会创建很多线程,程序可能会崩溃。我认为问题在于我正在尝试添加非阻塞资源,但是我不知道如何解决该问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)