QtConcurre​nt programmin​g need help

  • Banned

    I am working on an existing application where threads are submitted concurrently. he is submitting 8 threads together and then he is waiting for all 8 threads to be finished to submit the next 8 threads. In this way after every 8 threads he is waiting and a lot of time is being taken for waiting. Can you me some suggestions how can I skip this waiting after submitting 8 threads to improve speed. Total number of cpu is 8 so he is submitting 8 threads at a time.

    Submitting threads like this


    int nCpus = Environment::Get_No_Of_Cpus(); // ==8

    for(z=0; z<21; z++)


    for (int cpuID=0; cpuID<nCpus; cpuID++)


    Th = QtConcurrent::run(this, zLayerFunction,z,...,cpuID);



    //then he after submitting 8 threads, he is waiting for all 8 threads to be finished to submit the next 8 threads.

    for(int th=0; th<Threads.size(); th++ )








    How can i improve the speed so that it doesn't wait. How can I make it continuous. Any suggestions would be highly appreciated. Thanks Sujan

  • You could have something like this:

    listOfThreads threads; //This is a list of all threads that should be run. We will pull out one after the other
    fillListofThreads(); //Here we fill the above list with threads to run
    listOfThreads runningThreads; //This list will hold all threads currently running
    for(int i = 0; i < 8; i++)
    runningThreads.push(threads.pop()); //We take the first 8 threads and put them into the running list...
    foreach(Thread thread, runningThreads)
    thread.start(); //and start them
    while(listOfThreads is not empty and runningThreads is not empty){ // we continue to loop till we have ran all threads
    foreach(Thread thread, runningThreads){
    if(thread.isFinished(){ //is one of the running threads finished?
    runningThreads.remove(thread); //then remove it...
    if(listOfThreads is not empty){ //..and if we still have threads...
    Thread t = listOfThreads.pop(); //..take the next one...
    t.start(); //...start it...
    runningThreads.push(t); //..and put it into the list for running threads

    This should always have 8 threads running.
    (It's only pseudo code, so don't try to compile it directly :))


  • Banned

    Thanks JanDal I'll try this out.

  • You also asked this question on the interest@qt-project.org mailinglist, right? What in the answers there wasn't enough to warrant a post here as well?

  • Banned

    I asked both the places at the same time.

  • Banned

    how can I keep track of the cpuID on which the thread is running.

    I want to submit for z=0 to z =31 total 32 threads in sequence. They should be submitted and run in 8 cpus one by one in sequence. Please help.

  • You cannot. At least, Qt won't help you with that.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.