0

that is the blocking code, how would I convert it to non blocking async ? I'm trying to do async communication between a client and a server. here is my blocking sync code, how would I do it async ?

bool S3W::CImplServerData::WaitForCompletion(unsigned int timeout)
{


    unsigned int t1;
    while (true)
    {
        BinaryMessageBuffer currBuff;
        if (m_Queue.try_pop(currBuff))
        {
            ProcessBuffer(currBuff);
             t1 = clock();
        }
        else
        {
            unsigned int  t2 = clock();

            if ((t2 - t1) > timeout)
            {
                return false;
            }
            else
            {
                Sleep(1);
            }
        }
    }

    return true;
}
andre
  • 731
  • 2
  • 13
  • 27
  • How do you do the "communication"? Are you using a specific framework? Some platform-specific functions? Please elaborate! And please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Mar 06 '17 at 08:33
  • I'm using OGR Api. I will edit my post – andre Mar 06 '17 at 08:43

1 Answers1

0

Move the while loop outside of the function itself:

bool S3W::CImplServerData::WaitForCompletion()
{
    BinaryMessageBuffer currBuff;
    if (m_Queue.try_pop(currBuff))
    {
        ProcessBuffer(currBuff);
        // do any processing needed here
    }

    // return values to tell the rest of the program what to do
}

your main loop gets the while loop

while (true)
{
    bool outcome = S3W::CImplServerData::WaitForCompletion()

    // outcome tells the main program whether any communications
    // were received. handle any returned values here

    // handle stuff you do while waiting, e.g. check for input and update 
    // the graphics
}
Jason Lang
  • 1,079
  • 9
  • 17
  • is this async communication ? so its not that much code should be rewritten ? – andre Mar 06 '17 at 08:51
  • BTW you can go for multi-threaded but that's overkill for something like this. You already have try_pop which is a non-blocking function to check for input, so you can make use of that to get around the blocking problem. This is how you do it in a single-threaded app. – Jason Lang Mar 06 '17 at 08:53
  • should the while(true){WaitForCompletion()} be called in its own thread and another thread for pushing the data into a queue ? – andre Mar 06 '17 at 09:10
  • I really wouldn't recommend using a thread for this. In your program you need a main loop anyway. In the main loop you do all the "housekeeping". Check for keyboard+mouse input, check for incoming messages, update graphics, update logic code etc. Basically design all of them as non-blocking code. If you use threads for this then it's just going to be a huge PITA for you. – Jason Lang Mar 06 '17 at 09:21
  • BTW you only want to think about threads later on when you're doing stuff with heavy-duty processing. Then you'd have one thread that does keyboard/mouse/messages etc, and separate threads for doing the number-crunching. – Jason Lang Mar 06 '17 at 09:22
  • so its like a producer consumer problem. producer produce the queue buffers and consumer will wait for WaitForCompletion if it succeeds _ – andre Mar 06 '17 at 09:28
  • You don't need to "wait". That's why the while loop is outside the function. Any *program* needs a main loop to even function. What's actually happening is the program is going about it's normal business, but periodically (each time through the loop) checks whether incoming messages exist, with try_pop, and processes then when they are there. – Jason Lang Mar 06 '17 at 09:32