Skip to content
 

Implementing AntSnes with Qt part 3: Running the emulation in worker thread

I decided to build a two threaded application.  The main thread is for the UI, and the worker thread if for the emulator. This makes all porting easier in general, since now we can have a UI thread to handle all rendering, key I/O etc, and run the emulator thread as worker thread, which can emit the new processed frames to the UI thread etc.
The Qt makes the threading extra easy:
In my application I just inherited the emulator controller class from QThread,  then I just call start() to start the thread. The QThread will guarantee that  all slots will be executed in the context of that QThread after start has been called.
Another nice trick is to use Qt::BlockingQueuedConnection as connection type when the signal must be prosessed before the emitting thread can continue.
I’m really excited that all this communication between threads was so easy. No mutexes, or semaphores were required this time  at all 🙂
Here’s some code snippets how the threading was actually implemented.

//starting the thread
void QSnesController::Start()
{
   start( QThread::LowPriority );
}
//after the start() the thread will execute in the run()
void QSnesController::run()
 {
 connect(this, SIGNAL(frameblit()), blitter, SLOT(render()),
 Qt::BlockingQueuedConnection );
 gameloop();
 disconnect(this, SIGNAL(frameblit()), blitter, SLOT(render()) );
 }
//in render frame callback function
emit( frameblit() );

9 Comments

  1. zaca11 says:

    this way you can run antsnes with sound and playable on a Nokia N95 when you finish?

  2. […] This post was mentioned on Twitter by Summeli, Robert Bellamy. Robert Bellamy said: Implementing AntSnes with Qt part 3: Running the emulation in … http://bit.ly/aG6ebZ […]

  3. PD says:

    Hello, will it work on S60v3?
    THX

  4. Devensega says:

    Sounds superb. Will there be provision for phones that have both touch and keyboard inputs? Tis nice to use both.
    Love your existing antsnes btw.

  5. Casey says:

    So, for example, the nokia X6 would be a likely candidate for the emulators since it is touchscreen? It has capacitative however, it seems t is selective in how it uses the screen.

  6. ReS says:

    I am a little confused with the situation of te emulator, this Qt part, will it work with symbian v5 still?

Leave a Reply

(required)