[Interest] Executing a small function as a thread
Syam Krishnan
syamcr at gmail.com
Tue Mar 17 16:39:03 CET 2015
On 03/17/2015 08:51 PM, André Somers wrote:
> Well, in any multi-threaded programming, you basicaly have no control
> over when something runs. That's up to the OS, not you. It will usually
> be "soonish", but...
Understood.
>>> 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.
> Indeed. And that makes it quite a light solution.
OK. I'm reading on QtConcurrent now.
>>> 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().
> In fact, it is easier. Simply check the state of the future. No need for
> a QAtomicInt who's value is not self-evident. No need to really wait for
> the value/state to change either: you can attach a QFutureWatcher and
> get a notification when the future is finished. That way, you don't
> waste cycles bussy-waiting for another thread.
Well, my situation already needs me to poll objects periodically.
The following is indicative of my problem:
while(1)
{
for(all_objects)
{
ret = object[i]->execute()
if(ret == complete) all_objects.remove( object[i] );
}
sleep(1ms);
}
Now, object::execute() is supposed to get its work done by calling
do_something().
execute() should not block but do_something() is blocking. That's why I
need to call it in a separate thread.
Till do_something() completes, subsequent calls to execute() shall
return a value indicating that it's execution
is not complete, so that it will not be removed from the list of objects
(all_objects).
Thanks,
Syam
More information about the Interest
mailing list