C++ 有没有办法用多线程函数附加Gtk::TreeView-Gtk::ListStore?

C++ 有没有办法用多线程函数附加Gtk::TreeView-Gtk::ListStore?,c++,gtk,gtk3,gtkmm,gtkmm3,C++,Gtk,Gtk3,Gtkmm,Gtkmm3,我有一个函数,它将一些行附加到Gtk::TreeView,它基于: 我的代码库中的函数与此类似,但它有更多的单元格要填充,并且在某些情况下向量可能非常大,在最坏的情况下,此函数需要12~14秒才能运行,并使单个核心运行100%。因此,我尝试使用更多线程来实现此函数: std::mutex mtx; // That is need to avoid std::future's destructor from blocking the execution. std::vector<std::

我有一个函数,它将一些行附加到Gtk::TreeView,它基于:

我的代码库中的函数与此类似,但它有更多的单元格要填充,并且在某些情况下向量可能非常大,在最坏的情况下,此函数需要12~14秒才能运行,并使单个核心运行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;
    })); 
}
std::mutex-mtx;
//这是为了避免std::future的析构函数阻塞执行。
std::向量res;
for(const auto&i:some_向量)
{
res.emplace_back(std::async(std::launch::async,[&](){
Gtk::TreeModel::Row-Row;
{
标准:锁紧装置lk(mtx);
行=*(参考列表存储->附加());
}
行[my_column_record.some_value]=i;
})); 
}

对我来说,这个函数看起来不错,但有时,当数组很大时,这个函数会创建很多线程,程序可能会崩溃。我认为问题在于我试图附加一个未阻塞的资源,但我不知道如何修复它。

14s进程有多少行和多少列?看起来您试图从其他线程附加到TreeView,这是UB。看一看
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;
    })); 
}