help-gnustep
[Top][All Lists]
Advanced

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

Re: Unable to detach thread


From: Nicola Pero
Subject: Re: Unable to detach thread
Date: Sat, 16 Nov 2002 01:32:10 +0000 (GMT)

> > Well that's even weirder, since the Objective-C runtime (libobjc) 
> > needs to be compiled to use threads also, but if it was, why didn't 
> > you get a link error when -lpthreads was not included?

That was true of old GCCs ... newer GCCs are *much much smarter* (than
they used to be) in terms of threading. :-)

The libobjc shipped with gcc-3 is supposed to be able to automatically
switch into single-thread mode if not linked against -lpthread, and to
switch into posix-thread mode if linked against -lpthread.

To explain it better, try the following example code with a new gcc -

test.m
======

#include <objc/thr.h>
#include <objc/Object.h>

@interface Thread : Object
- (void) detach: (id)arg;
@end

@implementation Thread
- (void) detach: (id)arg
{
  printf ("In separate thread\n");
}
@end


int main (void)
{
  objc_thread_t thread;
  
  thread = objc_thread_detach (@selector(detach:), [Thread new], nil);
  
  if (thread == NULL)
    {
      printf ("No thread :-(\n");
    }
  else
    {
      printf ("Thread! :-)\n");
    }

  return 0;
}

==

I tried on my gcc-3.1, and here is the result:

nicola@didone:~$ gcc test.m -lobjc 
nicola@didone:~$ ./a.out 
No thread :-(
nicola@didone:~$ gcc test.m -lobjc -lpthread
nicola@didone:~$ ./a.out 
Thread! :-)
In separate thread
nicola@didone:

As you see, no matter if you specify -lpthread or not, it always compiles,
and runs.  But if you don't pass -lpthread on the command line, no threads
are available (but it still compiles, and runs single-threaded).  If you
pass -lpthread on the command line, threads are available.

This nice trick is implemented by using weak symbols.


> I'm not sure to completely understand what you're saying... I have 
> installed gcc-3 precompiled on my Debian, so I guess libobjc came 
> precompiled also. The gnustep-base package seemed to be broken (core 
> dump when executing GNUstep apps), so I recompiled it manually.
> 
> At first, without specifying the --with-thread-lib="-L/usr/lib 
> -lpthread" gnustep-{make, base} weren't compiled with threads. Then 
> using the --with-thread-lib configure option everything went fine. So I 
> guess this is just a problem of gnustep-base configure not finding the 
> pthread library ?

Yes - from what you describe, it looks like a problem in the configure
step in gnustep-make likely, which has not found libpthread.

I suppose what made the difference is when you added -L/usr/lib ... but
I'm not sure why (frankly) but my gcc (debian gnu linux) automatically
finds libraries in /usr/lib, there's no need to add -L/usr/lib, I'm not
sure why yours doesn't (that is, I can compile a file with "gcc
config_thread.m -lobjc -lpthread" and it works - it works on all gnu/linux
machines I ever tried).

Maybe you could try adding /usr/lib to ld.so.conf ?  Or check your linker
or compiler options or configuration ?

Let us know if you find more info on the problem.





reply via email to

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