CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2005
    Posts
    30

    Is using Sleep an ideal solution here?

    Hi all,

    I have made a multithreaded server that sends client the updates
    of its desktop. Server uses a seprate thread that gets updates
    and then send them to client through socket. I have used
    an infinite while loop in that thread that breaks when client disonnects
    and in each iteration of loop i get updates and sends if there is any.
    After sending one update i have used a Sleep of 50ms so that there
    should be a delay in next update send. If i dont used the sleep then
    server thread will continuously get screen updates and sends them.
    This causes my CPU usage on server near to 100%. To avoid this i have used
    sleep. Is that sleep duration is ideal or poor approach?
    Or i can use more sleep duration.

    I havent any idea how much sleep is ideal in real time applications.

  2. #2
    Join Date
    Dec 2005
    Location
    BANGALORE ,INDIA
    Posts
    19

    Red face Re: Is using Sleep an ideal solution here?

    HI ,

    What i feel is, if u use sleep and if there are any updates at that time then you will miss those updates.so,instead of sending the updates in that thread,put the updates in a message queue and create another thread which continously watches the message queue,so if there are any messages(updates) then it sends the data to the particular client.

    Regards,
    Harsh.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Is using Sleep an ideal solution here?

    If I understood correctly you are basically having an event-based approach. The server gets an event (-> send update), on this event it needs to send the data to the client.

    In this case, I would rather use an event to indicate the send thread that data should be sent. This way, you are not relying on any time that might not be accurate in regard to the updates.

    If however, the send thread is responsible for getting the update itself before sending it, then a 'Sleep()' is sufficient here...

  4. #4
    Join Date
    Oct 2005
    Posts
    30

    Re: Is using Sleep an ideal solution here?

    Thanx

    Actually i m using a System wide hooking dll(developed in VC++6.0) that sends my server (developed in C#) the coordinates of the the rectangle
    that windows is going to paint before the paint actually happens in the form
    of a message. My server gets the msg from its message queue(server main thread). Then server takes union of the rectangles arrived to make a single rectangle. The other thread that is responsible for capturing and sending the updated region checks the that single rectangle frequently to ensure whether to capture and send an update or not. If the rectangle is empty then this thread doesnt capture anything and sleeps for 50ms. After 50ms that thread again checks the rectangle set by the main thread. That process continues. It all works fine but one of my friends says that i should not use sleep if i dont use it then server CPU usage can go to 100% if there are contninuous updates, thats not feasible.

  5. #5
    Join Date
    May 2005
    Posts
    399

    Re: Is using Sleep an ideal solution here?

    Quote Originally Posted by cartographer
    It all works fine but one of my friends says that i should not use sleep if i dont use it then server CPU usage can go to 100% if there are contninuous updates, thats not feasible.
    When you say 'fine' do you mean that it meets your expectations?
    You may have to use trial-error to find an appropriate Sleep() duration that will give you acceptable CPU usage and Update rate.
    Last edited by leojose; January 3rd, 2006 at 12:35 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured