1. Is there a maximum number of threads to be created? If not, why does this program end after only 382 threads are created.
Linuxthreads defines the maximum number of threads for each process as 1024, but in fact this value is still limited by the total number of processes in the system. This is because the thread is actually a core process.
In kernel 2.4.x, a new set of computing methods for the total number of processes is adopted, so that the total number of processes is basically limited by the size of the physical memory. The calculation formula is in kernel/fork. in the fork_init () function of C:
Max_threads = mempages/(thread_size/page_size)/8
2. Will all resources be released when the thread ends (pthread_join is not called.
Generally, the following three functions are called to ensure resource release:
Void pthread_exit (void * retval)
Int pthread_join (pthread_t th, void ** thread_return)
Int pthread_detach (pthread_t th)
It is also said on the Internet that under normal circumstances, the running of each thread in the process is independent of each other, and the termination of the thread will not be notified, nor will it affect other threads, the resources occupied by the terminated thread will not be released with the termination of the thread.
Therefore, it is better to call pthread_exit or pthread_join, or set it to the detached attribute.
3. If I run multiple threads at the same time, how can I manage it?
The thread pool is a type. To simplify the process, you can also design a dedicated thread to clean up the suspended threads of pthread_join.
Summary:
I am talking about the choice principle between joinable thread and detached thread. If you need to synchronize the exit status of a thread, you must use joinable thread; otherwise, you can use detached thread to simplify the implementation and design.