Re: KITTEN BOF at IETF 60?

Love <[email protected]> Tue, 23 Mar 2004 21:36:30 +0100
Newsgroups gmane.ietf.cat
Message-ID <[email protected]>
Ken Raeburn <[email protected]> writes:

>> The problems that I have encountered with multithreading on various
>> platforms are:
>>
>>  - on most Unix/Posix platforms single-threaded objects/shared libs
>>    can *NOT* be used/linked with multi-threaded objects/shared libs
>
> Huh.  I'd gotten the impression that code compiled for a multithreaded
> environment, but not doing thread stuff itself, was generally safe to
> use in a single-threaded executable (with code compiled for
> single-threaded execution).

It depends how the shared object is linked. A threads shared object/module
must be loaded by a program that used pthread. Its not a requirement that
that libc (malloc/free/syslog etc) can handle any other situation.

"but not doing thread stuff itself" includes NOT calling libc.

Love

$ cat o.c
#include <pthread.h>
#include <stdlib.h>
int foo(void) { char *p = malloc(1); free(p); }
$ cat a.c
#include <stdio.h>
#include <dlfcn.h>
#include <err.h>
int main(int argc, char **argv)
{
        void *p;
        void (*f)(void);
        p = dlopen("./o.o", 0);
        if (p == NULL) errx(1, "dlopen %s", dlerror());
        f = dlsym(p, "foo");
        if (f == NULL) errx(1, "dlsym foo %s", dlerror());
        (*f)();
        return 0;
}
$ gcc -shared -o o.o o.c
$ gcc -g -o a a.c 
$ ./a
$ gcc -g -o a a.c -pthread
$ ./a
$ gcc -shared -o o.o o.c -pthread
$ gcc -g -o a a.c -pthread
$ ./a
$ gcc -g -o a a.c  
$ ./a
Abort trap (core dumped)
$ gdb -q a a.core
Core was generated by `a'.
Program terminated with signal 6, Aborted.
Reading symbols from /usr/libexec/ld.elf_so...done.
Loaded symbols for /usr/libexec/ld.elf_so
Reading symbols from /usr/lib/libc.so.12...done.
Loaded symbols for /usr/lib/libc.so.12
Reading symbols from ./o.o...done.
Loaded symbols for ./o.o
Reading symbols from /usr/lib/libpthread.so.0...done.
Loaded symbols for /usr/lib/libpthread.so.0
#0  0x4807896b in kill () from /usr/lib/libc.so.12
(gdb) bt
#0  0x4807896b in kill () from /usr/lib/libc.so.12
#1  0x4807aa2f in __libc_mutex_unlock () from /usr/lib/libc.so.12
#2  0x480eafeb in malloc () from /usr/lib/libc.so.12
#3  0x4810b6d8 in foo () from ./o.o
#4  0x080488de in main (argc=1, argv=0xbfbff678) at a.c:13
#5  0x08048602 in ___start ()
signature.asc (application/pgp-signature, 477 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (NetBSD)

iQEVAwUAQGCf0XW+NPVfDpmCAQKzugf+IVdkZrCFr81hIcVqit6pJ4FTTRWvNlHi
xC3kPcYYzt8tt5pBXFT9mrJr8rS6Ce6TsUl516TQPcJhtoKvbaOXbApbueiP4AI7
WJRmHFkTLCjTepHvDqR+XoRfVuEloHukCALYk8HWlDZIatvR83W7ARIzE9VRgMoF
/vYi8thVrNA90Nb5Nu41qbHeOkiSm24jbWRlyhi5n9N8ifZxeMzdyzc7If6aWqC2
AsH3aJ14nn8gy0n8vw9i1NScfhhy7AMlYQjkKsBNrcWHG3FCrpcpZXr8Giv+lwsn
QCFOpSSE8TQ9GiM3+YaWwgRzxsokwjQ5NjccTJLUyJ0SQu43NFjA4w==
=NGCM
-----END PGP SIGNATURE-----