سؤال

I try to implement something that prevent a thread from being interrupted by other running threads (inside of a single process), using the winapi.

My first ID was to put this thread to a higher priority, regarding documentation (from http://msdn.microsoft.com/en-us/library/windows/desktop/ms685100%28v=vs.85%29.aspx) it should prevents other from being executed (of course if my thread does not fall to any WAIT state).

But, and I can't really get why, other thread are still being scheduled after this "trick".

I can't get any other idea than using SuspendThread() on all other threads...

Is there some cleaner way to do so? Or have you got any advice regarding my issue?

هل كانت مفيدة؟

المحلول

Threads are scheduled OS wide and not only for one process. You can change the prio to 31. This will be the highest possible... but if other threads also runs with prio 31, your thread will also interrupted... Also all hardware-events (IRQs) will have higher prio than 31...

If you just want to have an "Process-wide" solution, I suggest to use Fibers. With fibers, you can do your own scheduling and also "prevent" other "Fibers" to "interrupt" your task. Of course, it might still be "interrupted" by an other thread. But if you run your thread with prio 31, this should be no big problem....

With this scenario, we run very high responsive (almost) realtime systems with Profibus connections... Also SQL server is using the fiber mode for optimized and very high performance mode.

Alternatively you can modify (restrict) the process affinity mask for all other processes (SetProcessAffinityMask)! To modify all processes you need to start your process as SYSTEM (for example via a service).

If you just care about your own process you can modify (restrict) the thread affinity of all other threads (SetThreadAffinityMask) in your own process!

Be aware, that it makes no sense to restrict your "important" thread to a specific CPU! This will only restrict your thread. But you must restrict all other threads/processes in order to get your "important" thread the best performance!

Also be aware that you never should consider using SuspendThread! This might lead to dead-locks... see also: http://blog.kalmbachnet.de/?postid=6 / http://blog.kalmbachnet.de/?postid=16

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top