0

While implementing ManualResetEvent something surprised me,

As far as I understand mre.Set() command signals and let other processes to execute.

mre.WaitOne(); Holds on the current line and waits for a signal. Beside this if we use it with timeout mre.WaitOne(100ms);

BUT! Lets suppose that StartCommunicate is a thread's job.

If I use waitHandle.Set(); my process uses ~%25 or for another project ~%1 CPU resource.

But if I use waitHandle.WaitOne(100); (the timeout value is symbolic. It (try)waits for a signal for 100ms).

The process starts using ~%0 CPU resource with waitone(timeout) What does it mean ? ThereIsAJobToExecute is Socket.HasData for me. So does it mean that hitting much to SerialPort.BytesToRead or Socket.Available makes our CPU usage higher ?

Does it any side effect for me holding the thread for 100 ms for every hit? Supposing that a socket program or a rs232 connection baud rate is very low comparatively new generation PCs.

So using mre.WaitOne(1); seems more preferable to me. What do you think about it ? I'm doing some experiments with some memory and performance profilers but I'm not sure if I'm doing optimum solution for various kind of client machines or not...

Longing for your comments.

Thanks in advance!

    ManualResetEvent waitHandle = new ManualResetEvent(false);
    public void StartCommunicate()
    {
        while (true)
        {
            if (ThereIsAJobToExecute)
            {
                Execute the job here!
            }
            else {
                //waitHandle.Set();
                waitHandle.WaitOne(1);
            }                              
        }

    }

EDIT: For Socket programming it is available to work ASYN so we can easily do it by the below code and we don't need polling.

But RS232 COMM port programming I need it. Or not ?

 do
 {
      socket.BeginReceiveASYN(....ReceiveCallBack,...,socket)
      mre.WaitOne();
      mre.Reset();
 }while(true)

     void ReceiveCallBack(IResult rst)
     {
     //get the socket and do my job here!
      mre.Set();
     }
Alberto
  • 15,626
  • 9
  • 43
  • 56
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • 1
    `Set()` and `WaitOne` are rarely interchangable. It's unclear to me why you think they are in this case. – Damien_The_Unbeliever Mar 19 '14 at 15:55
  • Usually, whatever generates the 'JobToExecute' does the signaling. It's somewhat of the main point of inter-thread comms - no polling for work required. – Martin James Mar 19 '14 at 16:07
  • @MartinJames I'm gonna edit my question to make it more clear. – Davut Gürbüz Mar 19 '14 at 16:09
  • @Damien_The_Unbeliever you are right at this point their jobs are different . I just noticed that by WaitOne I could lighten the burden of a thread to utilize CPU more efficient. I supposed com port polling not needs too much CPU. I'm trying drawing a real time chart by getting 1000 bytes from a com port, processing the sequential bytes... That is the real job . I agree, it sounds weird but I'm trying all the ways to achieve this. – Davut Gürbüz Mar 19 '14 at 16:23
  • It is not fair comparing apples and oranges. `Set` and `WaitOne` are orthogonal. and.. btw your async socket code is wasting a thread to wait, you don't get any scalability from async this way it is better to use synchronous sockets than making a thread to wait till you receive data. – Sriram Sakthivel Mar 19 '14 at 16:25
  • But for synchronous I must wait `if (SerialPort.BytesToRead>0)` in an infinite loop. This is the problem. Am I wrong ? Is there a way of notifying for new available data on a serial port? If so it is ok. – Davut Gürbüz Mar 19 '14 at 16:30
  • Why are you using callback-based IO and waiting for the callback to have completed? Just use the synchronous version. With serial ports, just read which will block until data arrives. – usr Mar 19 '14 at 16:33
  • OK! Thank you all. I think I figured out . SerialPort has an event `DataReceived` I'll use it. No need polling for this then. – Davut Gürbüz Mar 19 '14 at 16:36
  • @usr Serial port does not support asyn one already. I just wrote it how it is OK! for Socket program. Thank you. – Davut Gürbüz Mar 19 '14 at 16:37

2 Answers2

1

WaitOne puts the thread in a suspended state, which does not cost CPU resources. The signal from the ManualResetEvent later awakens the thread.

Haney
  • 32,775
  • 8
  • 59
  • 68
0

It's not 100% clear to me what you're using the ManualResetEvent for. However...

Doing something like waitHandle.WaitOne(1) is pretty much pointless, as you're sleeping for such a small amount of time that you're effectivly busy waiting on that thread, and consuming CPU resources that are not doing anything.

If you're looking to tell your thread that it should wake up and process data then try something like this:

while(true)
{
  waitHandle.Wait();
  waitHandle.Reset();

  while(ThereIsAJobToExecute)
  {
    // Process the jobs
  }
}

This will put your thread to sleep when there is nothing to do, and it won't waste any resources. Now you can signal it with waitHandle.Set() when there is work to do.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • I think your code won't work if waitHandle's state is false(if you meant waitone by wait). I supposed by Waiting a period I just did a timer's sleep job. I did it for avoiding polling's CPU overuse and I did it just when there isn't a job. If there is a SYNC job it finishes the job and polling again. But there is no job lets wait a little and ask again.It is something like "don't ask me the same question very often". It takes more time than NOP. Did you read comments before answering ? – Davut Gürbüz Mar 19 '14 at 17:07
  • If the event isn't signalled then the thread will sleep until it becomes signalled. If there's no job for it to execute what would you like it to do instead of sleeping? Your question isn't very clear about what you're trying to do, so I've taken a best guess. – Sean Mar 19 '14 at 17:15
  • Suppose that there is a polling job. You have no notification for a change. Then you ask it in a continuous loop. But determining if there is a job or not [this becomes another job] takes time and no need to ask for this change very often. BTW you asking rare you get some CPU resources free for the sake of -no execution of IsThereAJobMethod-. I asked the question to understand what really goes on. I think David Haney is right. – Davut Gürbüz Mar 19 '14 at 17:22
  • I just didn't know SerialPort's DataReceived event to use as a notification. Now my question is not valid for socket programming or serial port programming. I thought that the single way were polling for it but not. Sorry for misunderstanding. – Davut Gürbüz Mar 19 '14 at 17:27