Click to See Complete Forum and Search --> : Stdout from multiple threads.


Czy
August 2nd, 2007, 05:49 AM
Hello,

I've been stumped on this problem at work for a week. It is my first exposure to multithreaded C++ programming, and I am completely frustrated at how difficult it is to do something so simple.

My task is to write a command-line test app for an API i have written (on a linux platform, RH7). The library beneath the API is a public communications library for VoIP SIP (Sofia Sip). The API spawns the communications stack in a posix thread, while the test app should read console commands and interact with the network events.

The main thread uses a GLib main loop which currently simply uses g_print() (which is like printf in GLib) to continually print a message.

The problem is that within the communications library there are a number of console outputs, and once this thread has been spawned, the subsequent printfs in the main thread will not show.

Note that this problem also exists if I don't use GLib, ie. in an infinite while loop etc.

I'm not sure what other information I can provide, so please ask! Any help would be greatly appreciated!

Thanks in advance,
Dan

JVene
August 2nd, 2007, 04:17 PM
Whenever I've run tests on object libraries in console builds with multiple threads, printf's never gave me a problem (VC from 4.x to date, linux RH9, GCC 3 something forward).

Something must be happening that redirects standard output, corrupts the environment so that's non-functional, etc.

That is, if you simply created, say, 4 threads, and run a loop in each one to print values (maybe the loop integer), you'll get intermingled output from all 4 threads.

If you find that kind of thing isn't working if constructed in your application project, trying doing it in a new console app and prove it to yourself.

That will prove there's something amiss in your project's runtime environment (or perhaps your compiler/runtime).

Are you sure you're choosing appropriate multi-threaded options in your makefile/build?

Czy
August 2nd, 2007, 07:15 PM
Hello there,

I tried an experiment console app that infinitely loops and prints a message on both the main and pthread. Like the original app, the output only comes from one thread (there is no intermingling).

My colleagues have previously mentioned that threads in a loop can not be pre-empted in linux. One thing I tried that was able to achieve intermingling messages was to put a sleep(1); in the while loops of both the main and pthread. However, this is not an option in the original app, since I do not know the impact of calling sleep(); on the communications stack.

I also tried moving the experiment app to a machine running Ubuntu 6, but the result was the same.

For the experiment app, I simply compile with:

gcc TestMultiPrint.cpp -lpthread

I have also tried using the -D_REENTRANT flag while compiling as follows:

gcc -D_REENTRANT -g -c TestMultiPrint.cpp -o TestMultiPrint.o
gcc -D_REENTRANT -g -o TestMultiPrint TestMultiPrint.cpp.o -lpthread

It also has not solved the problem, although this time, the thread loops a number of times (and prints), before the main loop takes over and prints infinitely (the thread no longer prints). I don't know if the flag did anything though, because when i removed it and compiled as I did prior to the reentrant flag, the same output occured.

Are there other environment options that I am not aware of?

One thing to note is that when i call g_thread_supported(); in GLib, it returns false. I'm not sure if this is relevant.. I think it is to do with the GLib compilation, and not the runtime environment.

Thanks,
Danny

exterminator
August 22nd, 2007, 01:31 PM
Can you show the test code you wrote?