0

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.

6
  • 2
    You need to add more info on the structure of your program. Do all the threads cooperate on doing one thing, or do they all have a separate purpose? If separate, which tasks will be done by which thread when the 4 tasks should be done by 2 or 3 threads? Commented Jan 20, 2013 at 12:58
  • What about thread jobs. i mean if 2 cores then 3 thread perform tasks and if 4 cores then ?? jobs will increase or not ?? Commented Jan 20, 2013 at 13:04
  • State your problem clearly, possibly with some example. Commented Jan 20, 2013 at 13:31
  • It is not clear to me what the question is here? What exactly are you trying to solve? Commented Jan 20, 2013 at 14:20
  • 1
    My advice is to not worry about. Just make 4 threads all the time. You will not be able to measure any difference. Now if you might run on a machine with 16 cores, then it could matter. Commented Jan 20, 2013 at 16:24

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

-1. That is not the question. He knows how to detect number of cores, and the question is not about knowing number of concurrent threads supported by the implementation.
@Nawaz You're right, I just saw that. Reading the question carefully, I can't figure out what is being asked.
Even I can't figure out, that is why I left a comment on the question.
1

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.

1 Comment

Thank you Jerry , It is more convenient to have a only one thread,I will do as per your suggestion.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.