You have a stream already created at all times or create a stream if needed

I am working with a custom mode driver for small USB devices. My usb read loop should be very responsive and the operations performed should be very small (not necessarily atomic). So is the interrupt service routine in the kernel-mode driver. In one processing, I need to create a stream and pass some parameters to that stream inside this read loop.

So I need to know the exact upper limit of this operation. It shouldn't take more than 200ms or something.

The next alternative is to initialize the thread during device initialization (probe time) and then a sleeping thread, waiting until I pass it from the read stream. But in this case, the flow always works and it will be expensive.

What's the best option? My platform is Linux and they said that thread creation is very short on linux. I need to decide which is best. Keep the stream alive at all times, or create a stream if needed.

+3


source to share


2 answers


Modern machines have hundreds, sometimes thousands, of threads created and constantly "ready". "Ready" doesn't mean "Actually works."

Thus, there is no problem with starting another thread when initializing the device and keeping it in the "Ready" state in most cases, and sometimes it has to do some work in a rare time.



The trick to get this to work smoothly is to make sure the thread is waiting for an event. When a thread is waiting for a block, it consumes zero or near zero CPU.

Starting a new thread every time you need to do something can be quite costly. A new thread usually has to allocate memory, and this can be a tedious operation, especially on a low memory system where allocating memory can cause swaps.

+2


source


Just create a thread once and block it on some semaphores or mutecs until you report it. Thus, it will not "always work" and it will not be "expensive". This way, you do not need to handle cases such as: "What if the thread did not start when I needed some processing" or "What if the system was busy and the thread was started slowly"? ..



Small thing: if the thread doesn't do a lot, I would initialize it with a smaller stack size.

+2


source







All Articles