Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reusing threads in c++

94 views
Skip to first unread message

kushal bhattacharya

unread,
Mar 4, 2017, 1:35:11 AM3/4/17
to
Suppose i am running multiple parrallel threads in my program.So can i reuse the idle parrallel threads in order to to do some new task for a different context.( I am assuming that the previously created parrallel threads are mostly in idle state when there is no event occuring on it).
Thanks,
Kushal

Marcel Mueller

unread,
Mar 4, 2017, 2:21:51 AM3/4/17
to
On 04.03.17 07.35, kushal bhattacharya wrote:
> Suppose i am running multiple parrallel threads in my program.So can i reuse the idle parrallel threads in order to to do some new task for a different context.

This is possible. The concept is usually called ->"thread pool". Tasks
from a TODO queue are scheduled to free threads from the pool and the
thread is put back into the queue if there is nothing to do instead of
discarding it.
This concept if often implemented by web servers.

> ( I am assuming that the previously created parrallel threads are
mostly in idle state when there is no event occuring on it).

Well, "mostly" is not sufficient in any way. To get this concept working
you need /know/ whether there is something to do. An important property
is that the tasks must not block for longer e.g. at I/O operations while
the thread is still allocated from the pool.
This can be handled by using ->"asynchronous I/O". Many operating
systems provide APIs for asynchronous I/O. The concept is to release the
thread while waiting for I/O and allocate a new one from the pool as
soon as the operating system notifies that the I/O has completed.

However, in practice it depends on your platform and the number and
latency of the concurrent I/O operations whether this is a good idea.
I have worked on platforms where "start thread" was a machine
instruction which took only half a dozen clock cycles. Managing a thread
pool is by far more expensive than starting additional threads in this case.
And using asynchronous I/O is a major change in the design of the
program flow.


Marcel

kushal bhattacharya

unread,
Mar 4, 2017, 4:57:29 AM3/4/17
to
Suppose i start threads initially without initialiazing any work to do.Then with some conditions i check whether those threads are idle or not and accordingly i allocate task to those threads.Can i do that?

Melzzzzz

unread,
Mar 4, 2017, 6:06:52 AM3/4/17
to
If you know how, yes.

--
press any key to continue or any other to quit...

kushal bhattacharya

unread,
Mar 4, 2017, 6:55:37 AM3/4/17
to
Actually i cant really figure out how to start it

Melzzzzz

unread,
Mar 4, 2017, 7:02:34 AM3/4/17
to
On 2017-03-04, kushal bhattacharya <bhattachar...@gmail.com> wrote:
> Actually i cant really figure out how to start it

I recommend `C++ conurency in action` by Anthony Williams...

kushal bhattacharya

unread,
Mar 4, 2017, 7:04:34 AM3/4/17
to
ok thanks, but could u please give me a hint or so about how to look after it

Paavo Helde

unread,
Mar 4, 2017, 10:45:03 AM3/4/17
to
On 4.03.2017 8:35, kushal bhattacharya wrote:
> Suppose i am running multiple parrallel threads in my program.So can i reuse the idle parrallel threads in order to to do some new task for a different context.( I am assuming that the previously created parrallel threads are mostly in idle state when there is no event occuring on it).

Sure. The easiest way is to put tasks in a queue, then have N worker
threads read the tasks from the queue, carry them out and post the
results back to another queue.

Nowadays standard C++ provides all the means for doing this, especially
the std::condition_variable class.

kushal bhattacharya

unread,
Mar 4, 2017, 2:38:22 PM3/4/17
to
hi actually i only have the scope to access from the callback function itself which is called when any read event is called on the socket.To be clear i am using libevent library of tcp sockets so i am created threads for parsing incoming packets which is possible from the read callback function.I only have access to this read callback function as the whole setup is runnning in an infinite loop as done from the libevent library to catch these callbacks.So i am thinking about creating a subthread within the parsing thread itself for notifying for any data pushed from the parsing thread.Is this the right approach?

kushal bhattacharya

unread,
Mar 4, 2017, 2:39:49 PM3/4/17
to
Actually i am following the book concurrency of threads by anthony williams but since i havedo this task in a short period of time am not really sure about my approach

Dombo

unread,
Mar 4, 2017, 6:11:02 PM3/4/17
to
Op 04-Mar-17 om 16:44 schreef Paavo Helde:
Add to that the std::packaged_task and std::future classes, and it is
almost trivial to implement a thread pool mechanism. If you don't have
any specific requirements w.r.t. the thread the code runs on, the
simplest solution would be to use std::async.

kushal bhattacharya

unread,
Mar 4, 2017, 11:42:01 PM3/4/17
to
but how does it solve the race condition issue if i use future or async?

Dombo

unread,
Mar 5, 2017, 5:13:46 AM3/5/17
to
Op 05-Mar-17 om 5:41 schreef kushal bhattacharya:
> but how does it solve the race condition issue if i use future or async?

Without context that question is impossible to answer. If you just use
std::future to wait for a task to complete and to retrieve its return
value you won't need any further synchronization primitives. However it
won't help however if you share objects between threads.


kushal bhattacharya

unread,
Mar 5, 2017, 6:22:31 AM3/5/17
to
ok actually i have written the complete scenario about where i am about to use this in couple of posts before this you can read this.That is my whole context where i am using

kushal bhattacharya

unread,
Mar 8, 2017, 5:26:11 AM3/8/17
to
thing is i have to spawn some threads manually first which will sit idle in a different context and then in some callback other threads are created there and when value is updated on that thread it delegates work to those idle threads .How do i do that in c++

kushal bhattacharya

unread,
Mar 8, 2017, 11:59:31 PM3/8/17
to
I am thinking of storing those threds in a list and then accessing them when its really necessary to access those threads and check for whether those threads are doing any current job or not

kushal bhattacharya

unread,
Mar 9, 2017, 7:47:51 AM3/9/17
to
could anyone please point me some reference in order to acheive this?

Paavo Helde

unread,
Mar 9, 2017, 8:56:52 AM3/9/17
to
On 9.03.2017 14:47, kushal bhattacharya wrote:
> could anyone please point me some reference in order to acheive this?

http://lmgtfy.com/?q=c%2B%2B+thread+pool+library+example



w...@totalbb.net.tw

unread,
Mar 9, 2017, 9:13:33 AM3/9/17
to
On Thursday, March 9, 2017 at 8:47:51 PM UTC+8, kushal bhattacharya wrote:
> could anyone please point me some reference in order to acheive this?

Yes, it looks you just need another thread or so because codes
would be cleaner and easier to maintain, such little resource
consumption probably not something to concern about in reality.

I am not trouble myself with std:: things, can't give you reference,
and as I know, C++ has no documentation for general application users.

kushal bhattacharya

unread,
Mar 9, 2017, 12:46:20 PM3/9/17
to
hi,
this may look silly but i have searched about it and i am slightly confused whether the approach i looked is suitable for me or not so i was asking about better references here

Paavo Helde

unread,
Mar 9, 2017, 1:39:39 PM3/9/17
to
On 9.03.2017 19:46, kushal bhattacharya wrote:
> hi,
> this may look silly but i have searched about it and i am slightly confused whether the approach i looked is suitable for me or not so i was asking about better references here

You need to learn how to reply to correct messages and how to quote
previous messages. I guess by now everybody has already forgotten what
problems you are trying to solve and what approach you are trying to
take (assuming that somebody understood it in the first place).


0 new messages