简体   繁体   English

请帮我使用std :: thread

[英]Please, Help me to use std::thread

I have a big query for SQLite and I want to make the query in another thread. 我对SQLite的查询很大,我想在另一个线程中进行查询。 Furthermore, I want to update a ProgressBar in another thread. 此外,我想在另一个线程中更新ProgressBar。 Suffice it to say, I don't understand how to do this, but think that my main thread won't continue to run if I create std::thread t(func); t.join(); 可以这么说,我不知道该怎么做,但是认为如果我创建std::thread t(func); t.join();我的主线程将不会继续运行std::thread t(func); t.join(); std::thread t(func); t.join(); .

I could instead send a single SQL query per frame update, but that would perform more slowly. 我可以改为每帧更新发送一个SQL查询,但这会执行得更慢。

I have the following code: 我有以下代码:

//....pseudo code

int main()
{
    Game g;
    g->run();
    return 0;
}

//....code

class Game
{
private:
    ProgressBar *m_bar;  //have methods increase|decrease & draw for rendering
    SQLClass    *m_sql;

public:
    void handleEvents();
    void update()
    {
        if(/*when happened something*/)
        {
            m_sql->insertVeryBigQuery(); //20-30 seconds for writing
        }
    }
    void render()
    {
        m_bar->draw();
    }

    void run()
    {
        while(1)
        {
            handleEvents();
            update();
            render();
        }
    }
}

join() means you want to wait for the end of the thread right there. join()意味着您要在那里等待线程结束。 That's not what you want. 那不是你想要的。 You want it to run in parallel. 您希望它并行运行。 Drop the join and find a way to signal your main loop when the thread is done. 删除连接并找到在线程完成后发信号通知主循环的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM