In reply to sriram.seshagiri:
Like I said earlier, just declaring a variable automatic is not enough to fix the problem. You need to make sure there is a separate instance for each invocation of the thread. There is only one instance of the automatic variable i shared amongst all the threats. But there is a new instance of the variable j for every loop iteration/thread.