简体   繁体   中英

writing multithread program for variable number of cores

I am writing a program where there is a scope for 4 threads. I am using VC++ 6.0 & don't want to use any libraries only VC++. (By rule) optimal number of threads should be decided based on number of cores.

How to write code that creates different number of threads based on different number of cores ???

i want to follow following table

Cores | Threads
---------------
   1  |  2 
   2  |  3
   3+ |  4

There is 1 GUI & 3 worker threads. All worker threads use a circular buffer. I am thinking to implement 3 worker threads as follows.

  1. Read from File 2. Process the file 3. Create a new file after processing.

All these 3 steps are done sequentially for each single input file.

There will be a large number of files for processing (1000+)

I know how to detect cores. There can be if then else way also. But it seems it is going to be a difficult task to manage the code following 'if then else' way.

Is there any standard way to manage code under this situation.

You could use std::hardware_concurrency :

#include <iostream>
#include <thread>

int main() {

  std::cout << "Number of available cores on this system (hint): " 
            << std::thread::hardware_concurrency() << "\n";

}

Then launch the appropriate number of std::threads .

For the moment, let's ignore the GUI thread, since's it's always going to be the same.

For the worker threads, you're basically creating the same number of threads as cores, up to three, so you want something like:

HANDLE worker_threads[3];
int num_threads = min(num_cores, 3);

for (int i=0; i<num_threads; i++)
    worker_threads[i] = CreateThread(...);

Now, as to how to use those threads: the short answer is that I would not do it as you've described in your question. It makes little sense to take three steps that need to be executed in sequence, and break them up across threads. In fact, I'd avoid dedicating a thread to a particular type of task at all.

Instead, I'd keep the threads generic, so each thread can execute any of the three tasks. Use a queue of tasks to be carried out. The threads simply retrieve tasks from the queue and execute them. If that task results in another task to be carried out, that task gets put back in the queue with the rest.

Since you're doing this in C++, one obvious way to implement that is to create a base class for all tasks:

class Task {
public:
    virtual int operator()() = 0;
};

And then have your three different task types derive from that. Then you'll probably want to create a small wrapper class that covers up the fact that you're dealing with pointers to keep the code simple and straightforward.

As the question is not very specific, it is hard to guess what you need.

In general you could create workers which do a certain job and notify you (via callback or something else) about the result.

You then just start a new worker each time you need something to be done. There could be a maximum number of workers depending on the number of cores. It could be possible to somehow store the tasks you want to do in a queue and give them to the next free worker .

If you could somehow describe what you want to do (perhaps as a simplified example) it could be possible to help you better. Perhaps it is possible to show you how you could fit the worker-design to your purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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