R
R
ramntry2012-08-05 20:04:15
linux
ramntry, 2012-08-05 20:04:15

How to get rid of boost thread: failed in pthread_create: Resource temporarily unavailable exception?

I am learning multi-threaded programming and programming of network applications using the Boost.Thread and Boost.Asio libraries. I
wrote a small server and a testing client for it such that the
server creates a separate thread to process the next client connection. I ran into the following problem: after about five hundred successful
thread creation cycles for a connected client and it terminates at the end of processing the request, the server
can no longer create threads: an exception is thrown
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost:: exception_detail::error_info_injector
<boost::thread_resource_error> >
what(): boost thread: failed in pthread_create: Resource temporarily unavailable
At the same time, with the current values ​​of the constants, the server cannot create more than 10
threads at the same time, and the user on behalf of which the server and client are launched
has no restrictions on the number of processes or descriptors . Neither threads nor memory "flow" during operation -
everything is correctly completed and freed, sockets are correctly closed.
For a more complete description of the problem and source code (as well as statically linked binaries for linux x86-32), see
How to overcome this? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DurRandir, 2012-08-05
@DurRandir

Because you only join when the server finishes its work, for all running threads at once. Those. you started N threads, M of them finished their work, but you didn't clean up the resources after them.
A thread that has exited but remains unjoined counts against
[_POSIX_THREAD_THREADS_MAX].
© man pthread_join

I
ixSci, 2012-08-06
@ixSci

Although I think that you are doing everything wrong, and that this is not the way to go at all. You can use the at_thread_exit() function. That will allow you to detail the thread at the end of it. How to do this is up to you.
In general, it is better to make a thread_pool and send requests to it, and not fool yourself.

R
ramntry, 2012-08-06
@ramntry

Well thank you. Can you tell me where to look how to do it right? Unfortunately, only abstract simple examples can be found in the literature. Server organization combinations (processes / threads, they are the same - one per client or by the number of cores, thread pool, select / pol, epoll, they are the same - by hand or using asio, poco, etc. etc.) a lot of crazy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question