[Interest] Executing a small function as a thread

Syam Krishnan syamcr at gmail.com
Tue Mar 17 15:18:32 CET 2015


Hi

I have a function that I need to execute as in a thread in response to 
some event. The event is not a signal.
My function do_something() typically takes less than 20 milliseconds to 
finish.

Method-1
----------
1. I derive from QThread and override run() to:

void run()
{
    do_something();

    //copy results
}

I will create my thread object during initialisation and when the time 
comes,
I will just call thread->start().

Method-2
-----------
2. I derive from QThread and override the run() function to do something 
like this:

void run()
{
    my_mutex.lock();
    wait_condition.wait(&my_mutex);
    my_mutex.unlock();

    do_something();

    //copy results

}

During initialisation, I create the thread object and call 
thread->start() and it waits for the QWaitCondition.
When needed, I call wait_condition->wakeAll().


Which solution is better?
Method-1 is simpler for my use case as I find QWaitCondition too clumsy 
to use. But I am worried about possible
overheads of starting the thread new every time.

Please remember that I don't need signals and slots for this.

Any help is highly appreciated.


Thanks and regards,

Syam



More information about the Interest mailing list