Game Development Reference
In-Depth Information
Sleep(1000);
}
}
};
Our response to an event is implemented in the
TestCall
class:
class TestCall: public iAsyncCapsule
{
public:
virtual void Invoke() { printf("Test\n"); }
};
The
main()
function starts both threads and waits ininitely (you can press
Ctrl
+
Break
to
stop it):
int main()
{
(Responder = new ResponseThread())->Start();
(new RequestThread())->Start();
while (true) {}
return 0;
}
You should see this output:
Test
Test
Test
…
The
printf()
function might not be thread-safe, but our queue ensures the calls to it do not
interfere with each other.
Working with the network asynchronously
Networking is essentially a set of unpredictable and asynchronous operations. Let's do it
asynchronously in a separate thread to prevent stalls on the UI thread, which may result in
ANR behavior on Android.
Getting ready
Here, we need all that we have implemented in the previous recipes of this chapter: smart
pointers, worker threads, libcurl downloader, and asynchronous events queue.