samedi 24 novembre 2018

Use QThreadPool inside a Task

I have QThreadPool which run some tasks. I need also create new tasks in other tasks. To do that i think implement something like following:

class Task : public QRunnable
{
    QThreadPool *pool;
public:
    Task (QThreadPool *p) : pool(p){}

    void run()
    {
        static std::atomic<int> counter = ATOMIC_FLAG_INIT;
        qDebug() << QThread::currentThreadId();

        int c;
        if ((c = counter.load(std::memory_order_acquire)) < 10) {
            pool->start(new Task(pool));
            counter.store(c + 1, std::memory_order_release);
        }

    }
};

int main(int argc, char *argv[])
{
    //...

    QThreadPool threadPool;
    threadPool.start(new Task(&threadPool));

    //...
}

But I'm not sure that this is the right approach.

Aucun commentaire:

Enregistrer un commentaire