[Interest] Fwd: Executing a small function as a thread

Nate Rogers bob119er at yahoo.com
Tue Mar 17 15:32:46 CET 2015


I recently discovered QtConcurrent and it works great for me!  It might work for what you are trying to do too.  Check it out...
- Asynchronous Run | Qt 4.8

|   |
|   |   |   |   |   |
| - Asynchronous Run | Qt 4.8This function is a part of the Qt Concurrent framework. The QtConcurrent::run() function runs a function in a separate thread. |
|  |
| View on doc.qt.io | Preview by Yahoo |
|  |
|   |


Nate 

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
_______________________________________________
Interest mailing list
Interest at qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150317/d4ea973b/attachment.html>


More information about the Interest mailing list