• Post Reply Bookmark Topic Watch Topic
  • New Topic

ANother basic one: How to fork best to offload the Main Thread?

 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Another basic question:

The situation can perhaps be best explained with the typical example:
Suppose you have a server waiting for calls from clients in a main thread:



What I wondered was: this sets the main Thread in a busy waiting, while it (I can imagine) has so many other things to do.
So what I did, is this:

- I created a


This (I understood) creates just ONE thread, forked from the main Thread.
It is in this Thread that I use a where I offer one Thread from the pool for every client that presents itself.

Is this OK? Or is there a much better way of doing this?

Thanks for advice,
-Wolf
 
Henry Wong
author
Sheriff
Posts: 22525
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this OK? Or is there a much better way of doing this?


First of all, let's not use the word "fork". It has a different meaning in the OS, and using threads is not forking.


As for your question... it is your application, it is your decision what is best. The server socket will return a data socket, you need to decide whether a new thread is best or an executor pool is better. If the clients can timeout, maybe a new thread is best.

And BTW, if you are going to create lots of new threads, I don't know if I would recommend creating lots of new single thread executors -- it may be simplier to just create the threads.

Henry
 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,
You may have misunderstood the question:
The question was, whether it was better to have the main Thread listening, or to create a separate Thread that has just this responsibility. (the main or separate Thread would then assign a Thread from the pool at every time a client presents itself)
My fear, or better reason for asking is: if you don't create this "master listener Thread", don't you keep the main Thread to busy polling then?
-Wolf
 
Henry Wong
author
Sheriff
Posts: 22525
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accepting on the server socket isn't polling -- so the answer to your question is... if your main thread has to do something else, in addition to accepting on the server socket, then yes, you will need to have another thread.

Henry
 
Wolf Benz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - Thanks Henry.
This is what I wondered about (good/bad pattern).

-Wolf
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!