1

How we can Create dynamic threads using std::thread. Actually I am accessing some raw string from a queue and have to perform some processing on that and the queue is having thousands of such messages, so i want to create a thread for each message to improve the performance. I can create the thread's using below code.

unsigned int n = std::thread::hardware_concurrency();
std::thread myThreads[n];
while(true)
{


    for (int i=0; i<n; i++){
        myThreads[i] = std::thread(&ControlQueue::processSomeStuff,this,msg_struct);
        }



//for joining
for (int i=0; i<n; i++){
    myThreads[i].join();
}
}

but the thing is if I use the above code then it will create the threads only for que.size() threads, but the queue will have some more new messages.

So is there any method for creating a thread for each new message dynamically. like server socket creates a new socket for processing the client request.

smali
  • 4,687
  • 7
  • 38
  • 60
  • 3
    Why do you think that making a thread for each of the "thousands of messages" will improve performance? Threads are *very* expensive to create. – Mankarse Jun 16 '14 at 09:17
  • 1
    for each message i have to do lot of processing searching for a pattern, converting to xml, sending to file and to another server so if i do all this serially it is taking much time for all the messages, so i am thinking of crating multiple threads which will execute parallel and save lots of execution time and i have 2 cores and that can executes 4 threads parallely . – smali Jun 16 '14 at 09:20
  • 3
    you can use a thread pool – Jerry YY Rain Jun 16 '14 at 09:23
  • @Mankarse,nogard,Jerry YY ,LosioWaty,PBrandoli . Yes it is not good to create a thread for each request but i can create a thread pool of 4 threads. will it not increase the performance. – smali Jun 16 '14 at 09:43

3 Answers3

4

This looks like a good case for a thread pool: a set of threads is pre-created and ready to execute the jobs.

When a new message is received then the app passes it to the thread pool for further processing and immediately starts waiting for the next message.

A thread pool implementation I have done some time ago may be found here https://codereview.stackexchange.com/questions/36018/thread-pool-on-c11 on the Codereview site.

Community
  • 1
  • 1
Paolo Brandoli
  • 4,681
  • 26
  • 38
3

You should really consider @Mankarse and @Jerry YY Rain comments.

But if you want to go on with your approach, I'd consider creating a receiver thread whose sole purpose would be to observe the queue in a loop (i.e. hang when there are no messages), and when a new message arrives, it should create a new worker thread and pass the message to it.

This may also simplify your synchronization, as you will have only one reader, so you don't have to worry if two threads have read the same message or not.

Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • yeah but i have check the cpu time by creating threads like above code it really increased my performance, and each seconds thousands of new message will come to queue. – smali Jun 16 '14 at 09:28
1

I would propose to profit from thread pool. This way you will have some fixed amount of threads that process your requests in parallel. The amount of threads in the thread pool could correspond to the number of cores on your machine. There is no standard thread pool in C++, but you can easily write your own (queue, thread group, mutex, condition_variable), or benefit from boost::asio::io_service. See also this for reference implementation.

Creating new thread for each request might be very expensive. Moreover you might not get better performance because of context switch overhead (when number of requests is big).

Community
  • 1
  • 1
nogard
  • 9,432
  • 6
  • 33
  • 53