Thread: Suggestions for a simple thread dispatch

  1. #1
    Guest
    Guest

    Suggestions for a simple thread dispatch

    I'm looking for a simple way of sending a divided workload to a fixed (maximum) number of threads.

    Here is how I do it when the workload is simply divided by the number of threads:
    Code:
    std::vector<std::thread> threads;
    for(uint i = 0; i < numThreads; ++i) {
        threads.push_back(std::thread(
            &MyClass::myFunction, this, myData)
        );
    }
    for(auto& t : threads) { t.join(); }
    If the workload was so large, that I wanted to divide it into more pieces than I have hardware threads, it's probably not so good to do the following, right?
    Code:
    for(uint i = 0; i < numPieces; ++i) {
        threads.push_back(...);
    }
    Is there a minimalist way of dispatching the workload (over time) without using more than numThreads at once? I tried to think of ways to join finished threads using mutexes, atomics, queues, but the code always becomes bloated rather quickly. Maybe someone here has experience with threads and can point to an elegant solution.

    Thanks!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You might want to use std::async .
    std::async - cppreference.com

    If both the std::launch::async and std::launch::deferred flags are set in policy, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.
    This means your numThreads is upto the implementation unfortunately.
    Not sure if you can handle it in a more granular way.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The "easy" way is to use a work queue with functors (just use std::function<void()>). Push back functors along with any data it needs to execute. Then each thread locks, pops, unlocks and executes functor. It scales pretty well with few threads.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static type dispatch with structs inside classes
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2013, 12:36 PM
  2. multiple dispatch
    By yosef_yaniv in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2008, 12:17 PM
  3. simple thread signaling question
    By bwcal1999 in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2004, 09:28 AM
  4. Suggestions thread
    By ygfperson in forum Contests Board
    Replies: 17
    Last Post: 12-28-2002, 09:28 PM
  5. Replies: 5
    Last Post: 12-05-2001, 07:37 AM