Thread: Extremely odd cout/pthread bug!

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    73

    Extremely odd cout/pthread bug!

    Hey everyone, I found a really, really weird bug! I'm making a multithreaded application, and for some reason, the program just quits halfway through, always in different spots!

    And get this, it only starts working when I remove the cout in join()!

    Code:
    #include <pthread.h>
    #include <cassert>
    
    #include <iostream>
    using namespace std;
    
    class Thread {
    protected:
    	static void *pthreadMain(void *args) {
    		return ((Thread *)args)->main();
    	}
    
    	void *main() { return NULL; }
    
    	pthread_t thread;
    
    public:
    	void jumpIntoNewThreadAndStart() {
    		int error = pthread_create(&thread, NULL, pthreadMain, this);
    		assert(error == 0);
    	}
    
    	void join() {
    		std::cout << __FILE__ << ":" << __LINE__ << std::endl;
    		void *returnValue = NULL;
    		int error = pthread_join(thread, &returnValue);
    		assert(error == 0);
    	}
    };
    
    int main() {
    	for (int i = 0; i < 100; i++) {
    		std::cout << "trial " << i << std::endl;
    		Thread thread;
    		thread.jumpIntoNewThreadAndStart();
    		thread.join();
    	}
    
    	std::cout << "done!" << std::endl;
    }
    As you can see, it does 100 trials. On occasion it gets through all of them, but most of the time, it dies before it gets halfway through them. My latest run of the program only made it to 13:

    Code:
    trial 0
    ..\TestWFMO.cpp:25
    trial 1
    ..\TestWFMO.cpp:25
    trial 2
    ..\TestWFMO.cpp:25
    trial 3
    ..\TestWFMO.cpp:25
    trial 4
    ..\TestWFMO.cpp:25
    trial 5
    ..\TestWFMO.cpp:25
    trial 6
    ..\TestWFMO.cpp:25
    trial 7
    ..\TestWFMO.cpp:25
    trial 8
    ..\TestWFMO.cpp:25
    trial 9
    ..\TestWFMO.cpp:25
    trial 10
    ..\TestWFMO.cpp:25
    trial 11
    ..\TestWFMO.cpp:25
    trial 12
    ..\TestWFMO.cpp:25
    trial 13
    ..\TestWFMO.cpp:25
    It's odd that when I comment out the std::cout in join(), it always makes it all the way through.

    Definitely the weirdest bug I've ever seen. This thing has had me banging my head against the wall for days.

    Anyone have any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    std::cout is a global object. If multiple threads can access it concurrently, those accesses need to be synchronised. For example, using a mutex or critical section.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    73
    Yeah, I was thinking about that, but in this case, it's only the main thread accessing cout...

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Show how you compiled your sample.

    grumpy is correct, though std::cout is only used by the main thread in this example.

    gg

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    73
    My eclipse output shows this for the compile:

    Code:
    **** Rebuild of configuration Debug for project TestWFMO ****
    
    **** Internal Builder is used for build               ****
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -o TestWFMO.o ..\TestWFMO.cpp
    g++ -o TestWFMO.exe TestWFMO.o -lpthread
    Build complete for project TestWFMO
    Time consumed: 1594  ms.
    By the way, thanks for taking a look at this, I am seriously confused about this bug.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    When you say it "dies" what does that mean?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I would try compiling and linking with "-pthread" (no 'l' after the dash). This tells gcc to include any necessary compiler and linker flags for the platform.

    gg

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    73
    I added the -pthread, and this is the new compile output:

    Code:
    **** Build of configuration Debug for project TestWFMO ****
    
    **** Internal Builder is used for build               ****
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -pthread -o TestWFMO.o ..\TestWFMO.cpp
    g++: unrecognized option '-pthread'
    g++ -pthread -o TestWFMO.exe TestWFMO.o -lpthread
    g++: unrecognized option '-pthread'
    Build complete for project TestWFMO
    Time consumed: 1700  ms.
    When I say it dies, the program just hangs. No more code is executed, and CPU usage is 0. When I hit the stop button in eclipse while it's doing this, it says "terminate failed". The only way I can kill it is via the task manager. In my bigger project, when it hangs, its never predictable where it will hang, it hangs in someplace different every time. Very weird stuff.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What is your tool-chain/environment? Versions?
    MinGW [+MSYS], Cygwin, or cross-compile from some *nix?

    What would we need to compile your sample and reproduce the issue?

    gg

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    73
    I'm on a windows 7 machine using eclipse indigo CDT, using MinGW.

    According to windows batch file to determine MinGW version info my versions are this:
    Code:
    MINGWBASEDIR=C:\MinGW
    gcc version 4.5.2 (GCC)
    gcc version 4.5.2 (GCC)
    GNU gdb (GDB) 7.3
    GNU ld (GNU Binutils) 2.21.53.20110804
    GNU windres (GNU Binutils) 2.21.53.20110804
    GNU dlltool (GNU Binutils) 2.21.53.20110804
    GNU Make 3.82
    #define __MINGW32_VERSION           3.18
    #define __W32API_VERSION 3.17
    It looks like I have msys 1.0.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What happens if you put a millisecond delay in the threads' main?

  12. #12

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    73
    I tried putting in a Sleep(1) and it didn't change anything (it got to 30 out of 100). Then I tried Sleep(1000) and it got to 70 out of 100. Sleep(300) got to 18 out of 1000... dang, for a bit there I thought the duration of the sleep affected it. I guess not.

    I just tried updating my MinGW, the new versions are this:
    Code:
    MINGWBASEDIR=C:\MinGW
    gcc version 4.6.1 (GCC)
    gcc version 4.6.1 (GCC)
    GNU gdb (GDB) 7.3
    GNU ld (GNU Binutils) 2.21.53.20110804
    GNU windres (GNU Binutils) 2.21.53.20110804
    GNU dlltool (GNU Binutils) 2.21.53.20110804
    GNU Make 3.82
    #define __MINGW32_VERSION           3.20
    #define __W32API_VERSION 3.17
    Press any key to continue . . .
    But unfortunately the code still does the same thing.

    If I can't figure this out, what should I do? Should I file a bug report with someone?

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Can you run it in a debugger and break the process when it hangs? You should at least be able to determine which call it's hanging inside (presumably the join, but who knows)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    73
    Is that what happens when I hit the pause button in eclipse's debugger? I've tried that, but it acts as if the button does nothing... I can't figure out where its happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help on this extremely harsh and tricky c++
    By kingkobra in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 01:00 PM
  2. extremely confused
    By hockey1 in forum C Programming
    Replies: 9
    Last Post: 04-24-2009, 11:36 AM
  3. something extremely wierd
    By nerdykid in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2007, 10:14 AM
  4. extremely simple help
    By C++idiot in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2007, 10:39 AM
  5. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:34 PM