0

I have the following Python code (I thinks my question applies to every programming language):

from threading import Thread, BoundedSemaphore

# ... other things ...

mutex = BoundedSemaphore()
def some_function():
    with mutex:
        # do something with a globally available object

Thread(target=some_function).start()
# do something else after the thread invokation

Many threads starting some_function are invoked but only one of such threads is executing due to the mutex.

My question is: can I reach a point where my operating system cannot handle such a number of thread even if there is only one thread executing at a time? I already searched for an answer (like this thread but this talks about measuring) but no one mentioned nothing about interrupted threads.

Thanks in advance for your time and your answers. :)

Mega-X
  • 67
  • 5
  • There are always limits on resources. As a rough guide, 5000 blocked threads has no human-detectable performance impact on my Windows box. The broswer, Office, IDE works just the same as if the threads did not exist. – Martin James Apr 04 '20 at 08:03
  • so my question changes: what kind of resources are used by a blocked thread? (do I have to modify the original question in order to reflect the new question?) – Mega-X Apr 04 '20 at 18:30
  • Depends on how blocked and for how long:) In your case of a semaphore, a blocked thread is likely an extra TCB pointer in the waiting queue of the semaphore. If the thread has been stuck for a while, it's possible that it's code and stack have been completely paged out, so the other resource used is virtual memory, ie real disk space in the swap file and virtual space against process. The TCB, and other such, may be in non-swap RAM, but those details are OS dependent. – Martin James Apr 05 '20 at 04:25

1 Answers1

0

Ok I figured out that I can achieve the same thing with two threads: one thread has a queue of jobs to execute and everytime it extracts one (if any) and the other adds jobs to the queue when needed and does other things as well. In this way I do not have any concern about multithreading "shenanigans" (of course that queue can become quite big but that is another problem).

Thanks everybody. :)

Mega-X
  • 67
  • 5