This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Iostream deadlock...
> I hope you now will do something with the problem.
Thanks again for your bug report. This is not a bug in gcc, and
neither in glibc. Loading libpthread.so via dlopen does not work; you
need to link it statically. A corrected version of your program looks
like this:
//------------- tb.cxx ----------------
// On linux compile with: g++ -o tb tb.cxx -ldl -lpthread
#include <iostream.h>
#include <fstream.h>
#include <dlfcn.h>
int main()
{
const char *fname = "tb.cxx";
int c;
ifstream file0(fname);
//c = file0.get(); // call get() before loading pthread
// and the problem disappears
ifstream file1(fname);
if (!file1) {
cerr << "Cannot open file " << fname << endl;
return 1;
}
c = file1.get(); // remove first character <-- deadlock here
char line[256];
file1 >> ws; // eat whitespace
file1 >> line;
cout << "First token is: " << line << endl;
return 0;
}
That works fine for me.
In case you wonder why dynamic loading of libpthread does not work:
The C library provides weak symbols of the libpthread functions, which
are used in case libpthread is not loaded, and which do nothing. If
you are loading it dynamically, then it uses the weak versions in some
calls, and the truly multithreaded versions of some other
functions. That cannot work.
Regards,
Martin