SwitchToThread

Can SwitchToThread function be used to yield to other processes in the system? I have a dll that does processor-intensive work (the CPU slaves at 100% for several minutes at a time). I can afford a little delay at the cost of CPU utilization. Any idea whether this would work? Any other ideas?
arvind_csAsked:
Who is Participating?
 
seredaConnect With a Mentor Commented:
Yes, you can use this function.
The main execution entities in NT (i believe you're using NT) are threads. Processes are groups of threads with certain shared attributes (like process address space), but in terms of kernel time scheduler all threads are the same with cumulative priority taken from thread itself and its process.

So if you yield to the other threads it will let them execute.

The other thing to consider is that really time-critical threads have already been given higher priority and
THEY will have processor time when they need (or a little later ;).

So if you want constrain your application of CPU resource, that isn't enough.
Consider putting sleep call for a few milliseconds in the main cycle if you have one, or similar means.
0
 
nietodCommented:
I think your best bet is to use SetThreadPriorityBoost() and/or  SetThreadPriority().  so that your thread gets less CPU time when other threads are in need of it.  The best thing about this sort of approach is that the thread still gets lots of CPU time when it is available.
0
All Courses

From novice to tech pro — start learning today.