Creating a thread that lives for as long as the application.
I wrote a server using a c library, I created two threads using and have an infinite loop running in the threads method. One thread handles all socket related code, and adds incoming packets to be deserialized to a single producer single consumer queue. The other thread deserializes the packets. The codes works but it has a flaw. Creating the thread via fails and I get an exception if the system running the app has no more threads available. is it possible to tell the app use an existing thread if creating a new one fails:
9 Replies
uh, if the OS has run out of threads, there's nothing you can do
that being said, you probably want to use tasks, not an explicit thread
aren't tasks just meant for one time operations? The threads live for the life of the application.
eh, yes and no
but, socket stuff is async anyhow
so you'll be awaiting a lot
im not using c# sockets its using a c library no awaiting
oh
What exactly is the text of the exception?
There's no hard limit on the number of threads (at least in Windows). It doesn't "run out" of threads. It can run out of memory to use as stack space for threads, but that'll happen at many hundreds of thousands
each app calls new thread twice, after exactly 4 instances of my app is running the 5th will get null reference exceptions when calling thread.Start cause var thread = new Thread returns null
The OS can support thousands of threads.
Something is wrong.
hmm
let me try something
ok prior to calling thread.start my custom pool allocator was failing to allocate the memory needed
yeah my system runs out of memory after so many instances
oops