[fpc-pascal] Re: Multi-threaded project with few locks (no Thread.waitfor). Memory consumption keeps increasing on Ubuntu 10.10 x64

Luca Olivetti luca at ventoso.org
Sat Oct 16 17:56:10 CEST 2010


Al 16/10/10 17:42, En/na Luca Olivetti ha escrit:
> Al 16/10/10 16:57, En/na Vinzent Höfler ha escrit:
>
>>
>> Well, the usual implementation of an externally called "Destroy" is
>>
>> - first a call to the Terminate method
>> - then a WaitFor()
>
> Nope, I avoid the WaitFor. I usually do a
>
> while not FFinished do
> CheckSynchronize(100);
>
> (where FFinished is set when Execute ends).
>

Btw, this is what a C++ class (cThread in vdr, simple but nice, even it 
it's C++ ;) does


cThread::~cThread()
{
   Cancel(); // just in case the derived class didn't call it
   free(description);
}

void cThread::Cancel(int WaitSeconds)
{
   running = false;
   if (active && WaitSeconds > -1) {
      if (WaitSeconds > 0) {
         for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
             if (!Active())
                return;
             cCondWait::SleepMs(10);
             }
         esyslog("ERROR: %s thread %d won't end (waited %d seconds) - 
canceling it...", description ? description : "", childThreadId, 
WaitSeconds);
         }
      pthread_cancel(childTid);
      childTid = 0;
      active = false;
      }
}

bool cThread::Active(void)
{
   if (active) {
      //
      // Single UNIX Spec v2 says:
      //
      // The pthread_kill() function is used to request
      // that a signal be delivered to the specified thread.
      //
      // As in kill(), if sig is zero, error checking is
      // performed but no signal is actually sent.
      //
      int err;
      if ((err = pthread_kill(childTid, 0)) != 0) {
         if (err != ESRCH)
            LOG_ERROR;
         childTid = 0;
         active = running = false;
         }
      else
         return true;
      }
   return false;
}


You can check the whole class here:
http://projects.vdr-developer.org/projects/vdr/repository/revisions/master/entry/thread.h

http://projects.vdr-developer.org/projects/vdr/repository/revisions/master/entry/thread.c


Bye
-- 
Luca



More information about the fpc-pascal mailing list