bug-gnustep
[Top][All Lists]
Advanced

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

NSThread


From: Enrico Sersale
Subject: NSThread
Date: Wed, 03 Nov 2004 14:07:18 +0200


After this change in -base

2004-09-27 Richard Frith-Macdonald <rfm@gnu.org>

        * Source/NSThread.m: Refrain from creating thread dictionary in an
        inactive thread ... we don't want to create new objects in a thread
        which is in the process of exiting.  Also, improve documentation.

that is, in GSDictionaryForThread() the line:

  if (dict == nil)

has become:

  if (dict == nil && ((NSThread_ivars *)t)->_active == YES)

I'm encountering problems with threads that doesn't exit anymore.
I'm using the following code in many places, where a long task, as a file 
operation, is executed in an other thread. In all the places the code is 
something like this:

In the main thread I detach a new thread:

  ...

  ports = [NSArray arrayWithObjects: port[1], port[0], nil];

  conn = [[NSConnection alloc] initWithReceivePort: port[0] sendPort: port[1]];
  [conn setRootObject: self];
  [conn setDelegate: self];

  [nc addObserver: self
         selector: @selector(connectionDidDie:)
             name: NSConnectionDidDieNotification
           object: conn];

  NS_DURING
    {
      [NSThread detachNewThreadSelector: @selector(setPorts:)
                                       toTarget: [Updater class]
                                     withObject: ports];
  ...

- setPorts: creates a new instance of "Updater" and release it because it is
retained in the main thread:

  ...

  conn = [NSConnection connectionWithReceivePort: (NSPort *)port[0]
                                        sendPort: (NSPort *)port[1]];

  updater = [[self alloc] init];
  [updater setFileop: thePorts];
  [(id)[conn rootProxy] registerUpdater: updater];
  // "updater" is retained in "-registerUpdater:"

  RELEASE (updater);

  [[NSRunLoop currentRunLoop] run];
  RELEASE (pool);

  ...

After doing its work "updater" notices the object in the main thread that has a 
method like this:

  ...

  [nc removeObserver: self
                    name: NSConnectionDidDieNotification
              object: conn];
  [updater exitThread];
  DESTROY (updater);
  // we retained it in "-registerUpdater:"

  DESTROY (conn);

  ...

In -exitThread method there is only a line:
[NSThread exit];

but the thread doesn't exit and the cpu usage of the process grows to 99%.

Examples of this code are in Operation, Inspector, etc...
If you grep '[NSThread exit]' you will find all the places.






















reply via email to

[Prev in Thread] Current Thread [Next in Thread]