bug report: std::cout and posix threads

BlakWolf blakwolf@tiscalinet.it
Thu Jan 4 22:14:00 GMT 2001


bug report for gcc/g++ generated binary

Problem: calls to std::cout from different threads hang up the program

gcc/g++ version: gcc version 2.96 20000731 (Red Hat Linux 7.0)

OS: linux Mandrake 7.0 without modifications

Machine: AMD Athlon 700, 512MB

sample code:

-------------begin----------------


#include <iostream>

extern "C" {
#include <pthread.h>
};

void* cout_func(void*);

int main(int argc, char *argv[]){
    std::cout << "Hello from main thread" << std::endl;

    pthread_t thread;
    pthread_create(&thread, 0, cout_func, 0);
    pthread_join(thread, 0);
}

void* cout_func(void* Arg){
    // I know, I need a mutex to avoid manglings, but this isn't the point.
    std::cout << "Hello from the child thread" << std::endl;
    pthread_exit(0);
}

----------------end---------------

compilation command:
   g++ thread.cc -o thread -lpthread


top output:

  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
  901 walter     0   0   636  636   520 S     0,0  0,1   0:00 thread
  902 walter     0   0   636  636   520 S     0,0  0,1   0:00 thread
  903 walter     0   0   636  636   520 S     0,0  0,1   0:00 thread

The problem rose when I updated my distribution,
older compilers still work (e.g. gcc 2.95.x)
I don't know if this is a "undefined behaviour" in the c++ standard,
it seems strange to me that older versions handle this "correctly".
I hope you could tell me if it's a bug in my code, a c++ UB, or whatever,
or if it's due to the c++ stdlib.
In the latter case, who should I contact?

Regards,
Walter Chiricozzi




More information about the Gcc-bugs mailing list