How to run 21 threads concurrently


  • Banned

    I am not able to run my threads concurrently please help.
    How can I run 21 threads concurrently and get the job done by 8 cpus what I have in my machine.

    I am doing like this
    [code]

    std::vector<QFuture<void> > Threads;
    QFuture<void> Th;

    for(int z=0; z<21; z++)
    {
    Th = QtConcurrent::run(this,&UnrstParser,z);
    Thraeds.push_back(Th);
    }
    for(int th=0; th<Threads.size(); th++)
    {
    if(Threads[th].isRunning())
    Threads[th].waitForFinished();
    Threads[th].cancel();
    }
    Threads.clear();
    [/code]

    With this first few threads are probably running but not all the threads. As it's supposed to generate some files by each thread which is not being generated. Please help.



  • A QFuture is not a thread. QtConcurrent uses a thread pool which on your box is very likely to create 8 threads, one per core. Each future is passed to one of the threads in the pool as soon as it is done processing a previous future. I can't see any real interest in running 21 threads in parallel (as they will end up being interleaved by the kernel anyway). If each of your operations in the futures is supposed to last, maybe you should rethink the problem and make each future call more atomic: only process one iteration of its inner loop for example...



  • Recent CPU architectures are wider than a single thread can saturate, especially the case of intel processors that support hyper threading. On such a processor running 2 threads on a single physical core can still yield very good performance boost, roughly 1.5x speedup on average. An improvement you won't be getting by scheduling the work to a single thread.

    On the other hand, QtConcurrent might very well create a thread for every logical core of the CPU. In this case there is no performance gain to be missed.

    Also I doubt he has "8 cpus" in his system, more likely 8 cores, and even more likely 8 logical cores. Systems that actually support 8 CPUs are not only rare but insanely expensive.



  • Note that there have already been many (detailed) "answers to this question":http://lists.qt-project.org/pipermail/interest/2012-October/004244.html in the Qt mailinglist.



  • Well of course modern CPU architectures are multicore, and Qt Concurrent is perfectly fit to handle that. The question is what problem would require 21 concurrent threads ? Running so many threads even with hyperthreading on a 8 logical cores machine will behave poorly in my opinion due to context switching and heavier scheduling burden on the kernel.


Log in to reply
 

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