Qt-UI
QThread and QMutex
← Threads and Mutexes | ● | Scoped Lock →
With Qt we create a thread by subclassing from QThread. Calling the start() method on a QThread object will execute the run() method in a new concurrent thread. The wait() method waits until the thread is finished by returning from run() (same as join with POSIX threads).
TomKhaGai with QThread:
#include <QtCore/QThread>
#include <QtCore/QMutex>
class TomKhaGai: public QThread
{
public:
void run()
{
wok.lock();
put_ingredients();
cook_and_stir();
clean_wok();
wok.unlock();
}
~TomKhaGai()
{
wait();
}
protected:
static QMutex wok;
};
#include <QtCore/QMutex>
class TomKhaGai: public QThread
{
public:
void run()
{
wok.lock();
put_ingredients();
cook_and_stir();
clean_wok();
wok.unlock();
}
~TomKhaGai()
{
wait();
}
protected:
static QMutex wok;
};
Now let’s have two persons cooking two soups:
TomKhaGai soup1, soup2;
soup1.start();
soup2.start();
soup1.start();
soup2.start();
← Threads and Mutexes | ● | Scoped Lock →