[Interest] Executing a small function as a thread
André Somers
andre at familiesomers.nl
Tue Mar 17 15:33:18 CET 2015
Syam Krishnan schreef op 17-3-2015 om 15:18:
> 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.
>
>
Might I suggest a third method? Use QtConcurrent::run(yourFunction)
instead.
André
More information about the Interest
mailing list