user1002288
user1002288

Reputation: 5050

How to monitor each thread behavior of a multithread (pthread) C++ program on Linux ?

I am running a multithread (pthread) C++ program on Linux (redhat).

I want to follow the behavior of the running program but the print-out from threads interleave with each other. It is hard to find out the behavior of each thread.

I want to use DDD (a gdb GUI) to analyze the behavior of the program but it needs "motif", when I install "motif", which has an error:

cc -c -O -I../../include -I../../imports/x11/include/X11 ./ccimake imake.c imake.c:162:21: error: Xosdefs.h: No such file or directory

I also used helgrind and drd tools but there are a lot of plain text print-out, which makes it confusing the behavior of each thread.

Would you please recommend better open-source tools that can help me do debug and analyze/monitor behavior of multiple pthreads clearly and easily ?

Is it possible to show each thread in a distinct terminal xterm window w/o mixing up all threads together ?

thanks

Upvotes: 1

Views: 2353

Answers (2)

swang
swang

Reputation: 5249

The standard print functions like printf are not thread safe. If you are logging to stdout or a single file, you need to treat your logging functions as critical section and protect them using mutex. Otherwise your log could be interleaved or out of order.

Alternatively you can let each thread create their own log file, write thread name/id and timestamp to each file, and "tail" those log files in separate terminals.

Upvotes: 1

Mika Fischer
Mika Fischer

Reputation: 4268

  • Be sure to use printf for printing, then at least the lines will be printed intact
  • Make sure each thread uses a prefix, like [MainThread] Log message...
  • Direct output to a file: your_program > logfile.txt
  • For each thread, do: tail -f logfile.txt | grep <thread_prefix>

Upvotes: 0

Related Questions