Re: Query whether a thread is running



On Thu, Apr 30, 2009 at 7:07 PM, andras toth <grossespinne gmail com> wrote:
> On Thu, Apr 30, 2009 at 6:20 PM, klaus triendl <klaus triendl eu> wrote:
>> andras toth schrieb:
>>>
>>> Hello!
>>>
>>> Is it possible to query somehow whether a Glib::Thread * instance is
>>> running?
>>> Does the joinable() method do this or does it just return the flag
>>> that was given to the Thread::create() ?
>>> Of course I could just have a separate bool flag for each thread which
>>> I update manually, but I was wondering if there is some better
>>> solution.
>>
>>
>> Hi Andreas,
>>
>> for what exactly do you need this?
>>
>> The easiest way to know the thread's lifetime is to know when you have
>> created the thread and then joining it.
>>
>> Otherwise there is no other way than to maintain your own flag in the
>> executing slot (you would still need to join() a joinable thread, though).
>> joinable() only tells you whether you've created the thread previously with
>> joinable=true.
>> join() blocks until the thread finishes and it cleans up any resources -
>> i.e. the thread object gets destroyed and the pointer to it is no longer
>> valid.
>>
>> Klaus
>>

Hi Klaus,

Thanks for clarifying it. I need to know whether the thread is already
running, because I use threads to fetch content from a server when the
user clicks on a button. And if the button is clicked again whilst the
previously started thread is not finished yet, I want to return
without doing anything. On the other hand, if no fetching thread is
actually running, I start a new one. And I would prefer not to put a
join on the actually running thread, before starting a new one,
because then the user interface would hang in case of a second
request.

Andras


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]