[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Hang with pthreads and iostreams under Linux
From: |
Dan McGuirk |
Subject: |
Hang with pthreads and iostreams under Linux |
Date: |
07 Apr 2001 01:38:09 -0700 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Canyonlands) |
I'm having a problem where I write to an ostream (say, cout) from one
thread, then later try to write to it from a second thread, and the
second thread hangs. A test case is below.
If I watch what's happening in the debugger, I can see that during the
output in main(), __flockfile() is called twice in a row without a
corresponding __funlockfile(), which seems to be the source of the
problem.
Is this a bug in g++/libstdc++, or am I doing something wrong?
Actually it looks like the problem is in endl. I get the following
calls to __flockfile() and __funlockfile() in main: (And sure enough,
if I get rid of the endl's and just use \n, the problem goes away.)
#0 __flockfile (stream=0x40073cc0) at lockfile.c:32
#1 0x40057201 in ostream::operator<< () from /usr/lib/libstdc++-libc6.2-2.so.3
#0 __funlockfile (stream=0x40073cc0) at lockfile.c:47
#1 0x40057313 in ostream::operator<< () from /usr/lib/libstdc++-libc6.2-2.so.3
#0 __flockfile (stream=0x40073cc0) at lockfile.c:32
#1 0x4005789d in endl () from /usr/lib/libstdc++-libc6.2-2.so.3
#2 0x4006cc6f in ostream::operator<< () from /usr/lib/libstdc++-libc6.2-2.so.3
#0 __flockfile (stream=0x40073cc0) at lockfile.c:32
#1 0x400ef32d in _IO_putc (c=10, fp=0x40073cc0) at putc.c:37
#2 0x400578b8 in endl () from /usr/lib/libstdc++-libc6.2-2.so.3
#3 0x4006cc6f in ostream::operator<< () from /usr/lib/libstdc++-libc6.2-2.so.3
#0 __funlockfile (stream=0x40073cc0) at lockfile.c:47
#1 0x400ef311 in _IO_putc (c=10, fp=0x40073cc0) at putc.c:37
#2 0x400578b8 in endl () from /usr/lib/libstdc++-libc6.2-2.so.3
#3 0x4006cc6f in ostream::operator<< () from /usr/lib/libstdc++-libc6.2-2.so.3
Thanks for any help.
gcc version:
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.0)
versions of libraries I'm linking with:
libpthread.so.0 => /lib/libpthread.so.0 (0x40024000)
libstdc++-libc6.2-2.so.3 => /usr/lib/libstdc++-libc6.2-2.so.3
(0x4003a000)
libc.so.6 => /lib/libc.so.6 (0x4007d000)
libm.so.6 => /lib/libm.so.6 (0x4019c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
stack trace of hung thread:
(gdb) where
#0 0x400a9952 in __sigsuspend (set=0xbf7ff98c) at
../sysdeps/unix/sysv/linux/sigsuspend.c:45
#1 0x4002da99 in __pthread_wait_for_restart_signal (self=0xbf7ffc00) at
pthread.c:898
#2 0x4002f587 in __pthread_lock (lock=0x40073ca8, self=0xbf7ffc00) at
restart.h:34
#3 0x4002be39 in __pthread_mutex_lock (mutex=0x40073c98) at mutex.c:108
#4 0x4002eb1f in __flockfile (stream=0x40073cc0) at lockfile.c:32
#5 0x40057201 in ostream::operator<< () from /usr/lib/libstdc++-libc6.2-2.so.3
#6 0x0804876c in thread_main (ignore=0x0) at test4.cpp:8
#7 0x4002ad93 in pthread_start_thread_event (arg=0xbf7ffc00) at manager.c:274
test code:
#include <iostream.h>
#include <pthread.h>
void *thread_main(void *ignore)
{
cout << "in thread_main." << endl;
cout.flush();
return NULL;
}
int main()
{
pthread_t thread;
cout << "in main." << endl;
cout.flush();
pthread_create(&thread, NULL, thread_main, NULL);
pthread_join(thread, NULL);
return 0;
}
--
Dan McGuirk
address@hidden
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Hang with pthreads and iostreams under Linux,
Dan McGuirk <=