View A Backtrace For All Threads With GDB

Debugging multi-threaded programs can be really tricky. GDB however, will always do its best to rescue you from whatever horrible bug you’re currently looking at. Today I wanted to show you a really nice command for viewing all the threads in your program.

This particular command isย especially helpful if you ever need to diagnose a deadlock, because it will give you a complete overview of the entire system and allow you to see which locks are currently being requested.

I’m going to use the C++11 threads program from my thread tutorial to illustrate, with one minor modification – I’ve added a sleep command in the thread subroutine. The only reason I’ve done this is because it keeps my threads alive for long enough to show them all to you in the debugger. On a commercial multi-threaded system, your threads will be doing something most of the time, so my sleep command is essentially simulating my threads going off and doing important jobs ๐Ÿ˜‰

Read more

C++11 Auto Keyword

This is just a short post to illustrate the use of the auto keyword. It’s something really simple from the latest standard that you can use in your programs and it will save you typing time ๐Ÿ™‚ .

So, the auto keyword has always been around, but it was pretty much redundant. ‘auto’ was the default storage duration specifierย of any variable that you declared, even if you didn’t state it as such. That means that it would be allocated space at the start of the enclosing block of code, and deallocated space at the end.

To put it into context, if you didn’t use auto, you’d use extern or static. It’s a way to tell the compiler how long you want the variable available.

Read more

A C++11 Threads Tutorial

Last week we created a simple program using the pthreads library. This week I’ve “translated” that program into the C++11 threads version, so you can see how it compares.

I say compares, but on linux, using GCC, the C++11 thread library is basically a wrapper for pthreads anyway, so although you are using native C++ commands, they are effectively calling the pthread commands that we saw last week.

So, the first thing to remember, when you are using the new C++11 thread library, is that you have to add a couple of flags to your build in order for it to run properly.

Read more