Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing control to a thread immediately (immediate context switch)

Posted on 2010-11-25
5
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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