[Interest] Executing a small function as a thread

Sze Howe Koh szehowe.koh at gmail.com
Tue Mar 17 16:15:06 CET 2015


Hi,

On 17 March 2015 at 23:06, Syam Krishnan <syamcr at gmail.com> wrote:
> On 03/17/2015 08:03 PM, André Somers wrote:
>> Might I suggest a third method? Use QtConcurrent::run(yourFunction)
>> instead.
>
> The documentation on QtConcurrent::run() says
> "Note that the function may not run immediately; the function will only
> be run when a thread is available."

These threads are part of the global thread pool. The documentation
means, *if you are currently using all available threads in the pool*,
then QtConcurrent::run() will wait until one of those threads finish
before running. However, since you only want one instance running at a
time, then it's not a problem -- your function will run immediately.


> That is not acceptable to me. I am looking for the simplest solution
> that has the least overhead.

QtConcurrent::run() is the simplest solution available.

QtConcurrent uses QThreadPool internally. One of the reasons
QThreadPool was created was to reduce the overhead of starting new
threads.


> And I should have mentioned that only one instance of do_something()
> must be executing at any time.
> I am using QAtomicInt to control calls to this function. So when a
> second event is raised, it will check if
> the thread is already executing. If it is, it will wait till the thread
> execution is complete.

Yes, that is compatible with QtConcurrent::run().


> And I'm using Qt 4.8 on Linux.
>
> Syam

Regards,
Sze-Howe



More information about the Interest mailing list