Solved

Passing control to a thread immediately (immediate context switch)

Posted on 2010-11-25
5
347 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

14 Experts available now in Live!

Get 1:1 Help Now