Solved

Passing control to a thread immediately (immediate context switch)

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

763 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