Solved

Passing control to a thread immediately (immediate context switch)

Posted on 2010-11-25
5
350 Views
Last Modified: 2012-05-10
Hi,

I have been a multi-threading developer for years, but I don't know how to accomplish the following.  Someone suggested "fibers" but I get enough of that in my diet already LOL.

In my main form, I have created a separate thread that runs all the time, spinning and waiting (with sleep(1) in the loop to throttle it a bit) for a global flag to be set before it performs its action.  Once I set that flag to True to signal the tread to start it's processing, I notice it takes about 3-5 milliseconds for the thread to start doing it's thing.  I need to eliminate this delay.

Using the process object, I have elevated my priority to high and when I create that thread, its priority is set to high as well to help assign horsepower.

This is probably the natural delay between the time the main form gives up it's quantum and the OS thread scheduler gets around to scheduling time for that target thread.  I need to eliminate this delay since it's a real time need.

How do I effectively transfer control to that running thread right away without the delay mentioned above. (as if it were a called subroutine).

0
Comment
  • 3
  • 2
5 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 34214701
Is the threaded loop doing anything other than checking the flag before sleeping?

If so, you can make it HALT completely by using a ManualResetEvent and WaitOne:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx

Simply "set" the ManualResetEvent() and the thread will continue...
0
 

Assisted Solution

by:RobertFromSecretWeapons
RobertFromSecretWeapons earned 0 total points
ID: 34218629
Hi IdleMind,

Your suggestion is interesting as I have not used this particular function before.  However, still may not actually improve the condition I am trying to tune for (other than perhaps eliminating the thread.sleep in the while loop replacing it with your suggestion.  However I will try it and see if it makes any difference.

In a nutshell, in my main form, in a certain event handler, I will set SomethingToDo to true and then on the next line, perform Thread.Sleep(0) to immediately give up the quantum in the main form.  This at least hopefully minimizes any transition time to the target thread waiting to do something (below).  Despite this, I still see around 3ms time delay before the target thread receives it's quantum.

Private Sub ThreadTask()

While(ThreadEnabled)
   If(SomethingToDo) Then
      DoSomeThings()
   Else
     Thread.Sleep(1)
   EndIf
End While

Are you still thinking that your suggestion is the way to go...
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34219811
It may not be any faster...just more efficient with respect to a polling loop.

Not sure you can get rid of a mere 3 millisecond delay...how exactly are you measuring this SUPER short delay?
0
 

Author Comment

by:RobertFromSecretWeapons
ID: 34233096
Hi Idle_Mind,

To answer your question about super short delay, I have a logging class that timestamps down to 1ms.  I use now() as the basis of time stamping the log entries.  This class leaves the bulk of the log entry processing to a low priority processor thread.  The line is time stamped in real time, and then fed into a queue whereby the other end of the queue gets read by a low priority thread, where the log entries are text formatted and written to a file.

Yes, I suspect there is a resolution issue at times using now, but in certain test environments, I seem to get reasonable resolution.

However, my main question for this post is not about this issue, it's about another issue unrelated to this.  Thanks for the help.  While I learned something new, this suggestion did not help me with the root issue.  I may have to open a ticket with MS and work it through them.  I am working on a similar issue that seems to point me in the direction of NGENing my exe to get rid of JIT issues slowing done one time only code execution branches.
0
 

Author Closing Comment

by:RobertFromSecretWeapons
ID: 34272792
I will award 250 points as I learned something new but it did not resolve my root issue.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now