Skip to content

Implementation of CONFIG_MAX_PTHREAD_COUNT #15516

@SteveOss

Description

@SteveOss

Describe the bug

CONFIG_MAX_PTHREAD_COUNT appears to have been implemented as the maximum number of POSIX threads that can ever be created, rather than what might be expected i.e the maximum number of active POSIX threads. If the former is intended IMHO this is a "bad idea"
in that it breaks any dynamic creation of threads and should be documented, if the latter then is clearly a bug.

To Reproduce

In a loop call pthread_create on a function that immediately returns i.e. the thread starts then
terminates, after a few iterations the function returns EAGAIN as the maximum thread count
has been exceeded.

Expected behavior

I would expect to be able to create POSIX threads and only get an EAGAIN error if I exceed
a configured maximum of running threads. The POSIX use of EAGAIN implies this i.e. try
again later and you may be able to create a thread.

Impact

Breaks standard TCP handling program, where listener creates a new thread to handle a connection.

Environment (please complete the following information):

Am using the v1.14-branch GA release branch and testing on a frdm-k64f, although looking at the source is not platform specific.

Additional context

In lib/posix/pthread.c take a look at use of pthread_num (as a minimum should be an atomic variable or accessed via a mutex, even for existing semantics).

Fix is straight forward:

  • Create a mutex to protect pthread_num and associated stack array (posix_thread_pool)
  • Add an array to mark usage of posix_thread_pool (e.g. boolean array)
  • On thread creation, iterate usage array to find free pool element and increment pthread_num
  • On thread exit decrement pthread_num and mark pool element as free.

Probably about 20 lines of code :)

Metadata

Metadata

Assignees

Labels

area: POSIXPOSIX API LibrarybugThe issue is a bug, or the PR is fixing a bugpriority: mediumMedium impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions