Thread Creation Overhead vs Synchronization overhead
Hello,
I need to know the relative overheads of a new thread creation every time a new request is serviced vs having a dedicated thread waiting for a request, which gets woken using the pthread_cond_signal / pthread_cond_wait calls.
Note:
- The applicaiton is a server.
- The thread in question is a light-weight thread used only for implementing timeout.
- The main thread is the one which executes the core logic
The timeout is implemented in this way:
- on a new request, the main thread spawns a new threads which waits for a specificed number of seconds[ sleep or an equivalent ] , say x, which is the application timeout. If sleep is not interrupted, and x seconds have elapesed, sets a global denoting that timeout has happened.
- The main thread [ core logic ] checks for this state at safe points, if timeout is detected, aborts the logic and gracefully exits after terminating the timer threads[ pthread_cancel].
The questions that I have are:
- As every request creates a new thread, what is the overhead ?
The alternative option is to have one long running thread, that wakes up on a new request [ main thread does pthread_cond_signal ] and the long running threds blocks on a wait.
I want to know which of them is a better option ? I read in one of the posts that for a rpc kind of applicaiton, one thread per request may be better. Any Comments ?
Thanks.