Using top to see what happens, find it hung in umtxn status.
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
41277 kasicass 1 45 0 15012K 2688K umtxn 0 0:00 0.00% lua
Once more, I try it in my debian box, everything is ok. Strange huh?
Then I found thing goes wrong when libzmq.so call pthread_create().
Why
Now I create a simple program to show why.
---------------- mydll.c -------------------
#include <pthread.h>
#include <stdio.h>
void *threadfunc(void *_)
{
return (void *)0;
}
void myfoo()
{
pthread_t pid;
puts("111");
pthread_create(&pid, NULL, threadfunc, NULL);
puts("222");
}
--------------- main.c --------------------
#include <dlfcn.h>
typedef void (*myfunc)(void);
int main()
{
void *dll;
myfunc f;
dll = dlopen("./mydll.so", RTLD_NOW);
f = dlsym(dll, "myfoo");
f();
dlclose(dll);
return 0;
}
------------------ Makefile ------------------
all:
gcc -o mydll.o -Wall -fPIC -c mydll.c
gcc -o mydll.so -shared mydll.o -pthread
gcc -Wall main.c # gcc -Wall main.c -pthread
------------------------------------------------
$ ./a.out
Segmentation fault (core dumped)
$ ldd a.out # without -pthread
a.out:
Start End Type Open Ref GrpRef Name
0000000010000000 0000000010044000 exe 1 0 0 a.out
0000000055078000 00000000551d8000 rlib 0 1 0 /usr/lib/libc.so.53.1
0000000056d40000 0000000056d40000 rtld 0 1 0 /usr/libexec/ld.so
$ ldd a.out # with -pthread
a.out:
Start End Type Open Ref GrpRef Name
0000000010000000 0000000010044000 exe 1 0 0 a.out
0000000057a80000 0000000057ae8000 rlib 0 1 0 /usr/lib/libpthread.so.12.0
0000000059798000 00000000598f8000 rlib 0 1 0 /usr/lib/libc.so.53.1
0000000051710000 0000000051710000 rtld 0 1 0 /usr/libexec/ld.so
------------------------------------------------
Aha, that's it. The program without -pthread crashes in OpenBSD, FreeBSD and debian.
As lua is designed without multi-thread function, it's been linked without -pthread.
Debian's lua is linked without -pthread, but I don't know why lua-zmq runs correctly.
I think it's not safe either.
Solution
Download lua source and rebuild it with -pthread
Modify lua-5.1.4/src/Makefile: LIBS= -lm $(MYLIBS) -pthread
ps. lua-zmq need be linked with -lstdc++ explicitly in my OpenBSD/loongson. OpenBSD sucks?
评论