C++ cout crashes during concurrent usage with boost’s threads under embedded Linux

Last week I was testing my C++ concurrent priority queue for HTTP requests with std::cout printing on the console output for possible race conditions. I was running my unit tests for some potential problems, when I found out that there was a crash of a unit test under embedded Linux. The same code was tested under Windows 7 and I didn’t see any crash of all unit tests. I was very curious to find out the reason for this crash. Under suspicion was problem with memory allocation or something similar as usual, but I hadn’t any assertions for that. On the next morning I spent two hours experimenting with isolation of the problem code. It appears that std::cout is crashing when it’s used by threads printing on the Linux console. Test application given below is a simple example for the crash with two boost threads which are printing numbers using std::cout. Maybe it’s trivial case why this example is crashing but sometimes similar problems like this can lose more than a week in spaghetti code, when you don’t  expect crash from this kind. The used embedded Linux for testing is BusyBox and the code is executed by processor with MIPS architecture.

 C++ |  copy code |? 
01
int main(int argc, char* argv[] )
02
{
03
 boost::thread    thOne(coutOne);
04
    boost::thread    thTwo(coutTwo);
05
 
06
    thOne.join();
07
    thTwo.join();
08
 
09
    return 0;
10
}
11
 
12
void coutOne()
13
{
14
   for(int i = 0; i < 10000; i++)
15
   {
16
     std::cout << " coutOne  : " << i << std::endl;
17
   }
18
}
19
 
20
void coutTwo()
21
{
22
   for(int i = 0; i < 10000; i++)
23
   {
24
       std::cout << " coutOne : " << i << std::endl;
25
   }
26
}

Posted in C++ | Tagged , , , , , , , | 2 Comments

Thought of the day by Martin Fowler

You should use iterative development only on projects you want to succeed.

Martin Fowler

Posted in Thoughts | Tagged | Leave a comment

C++ Heap Corruption Problem

In the last six months I am consulting a company in C++. Today my colleague and I find out a very tricky heap corruption. Below is presented a C++ snapshot with the heap corruption. Can you see the problem ?

 C++ |  copy code |? 
01
string Core::ntos(unsigned int n){
02
 string ts= "";
03
 if( !n )
04
     return ts= "0";
05
 char i= 0;
06
 string tmpstr= "";
07
 char s;
08
 while( n ){
09
     tmpstr[i++] = (char)(n %10 +48);
10
     n/= 10;
11
 }
12
 for(; i-- ;)
13
     ts.push_back(tmpstr[i]);
14
 return ts;
15
}

I don’t think it’s easy to see the corruption, if you are interested write me to post the answer. ;-)

Posted in Experience | Tagged , | 6 Comments

Thought Of The Day By Napoleon Hill

“Education comes from within; you get it by struggle and effort and thought.”
— Napoleon Hill

Posted in Thoughts | Tagged | Leave a comment

TDD Bag Code Kata

  • Practice of Bag code kata. For any references please see the link below http://algs4.cs.princeton.edu/lectures/13StacksAndQueues.pdf
Posted in TDD | Tagged , , , | Leave a comment

You can’t be a warrior, if you have read the manual for the sword!

Recently, I quit my job, in order to write my Ph.D. thesis. Now I have more time for practicing TDD. Don’t ask me why I didn’t have time to do it before. ;-) The result of TDD code katas practicing is realized habits for every day usage. In most cases the work out habits are independent from the environment and programming language. The new “mental habits” give you a new way of thinking on a small steps which is very useful in every day programming activities. I strongly recommend you to practice some kind of code kata regularly just to work out a new “mental habit” which will save you in the next chaotic project.
Believe me, you don’t have time to become software developer(warrior) for a day!

Posted in Experience, TDD | Tagged , | Leave a comment

TDD Queue Code Kata

Posted in Experience, Java Experience, Software Katas, TDD | Tagged , | Leave a comment

Testing is developers’ job not testers’

When I started working as a developer, I didn’t work with a tester. So I keep thinking that quality and testing of the code is developer’s job, and Gojko Adzic presentation confirms my understanding of software development process on some way. Take a look the Gojko Adzic presentation with his QA analysis about Greece crisis. ;-)

Posted in Experience, Observations | Leave a comment

Thought of the day by Unknown

Never confuse activity with progress.

Posted in Project Management, Thoughts | Leave a comment

Thought of the day by John von Neumann

There’s no sense being precise about something when you don’t even know what you’re talking about.
John von Neumann

Posted in Thoughts | Leave a comment