Solved

Passing control to a thread immediately (immediate context switch)

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

22 Experts available now in Live!

Get 1:1 Help Now