When you modify any kind of data from two or more different threads, then you have to protect the data with a lock. You can only modify the data when the current thread has successfully acquired the lock, then you have to release the lock after performing the write on the data. This way only one thread can write the data at a time. In your situation this disaster can happen:
1. Your counter is zero.
2. Both thread 1 and 2 read out this zero value at the same time to some register in the processor.
3. Thread 1 increases the value to one, and thread 2 decreses it to minus one.
4. Both threads write back their own value and one of them will win (random result, either -1 or 1).
With a lock you can achieve that while one of the threads is reading/modifying/writing the int, the other thread is blocking on the acquire method of the lock.
For simple integral values you can use gcc atomic builtins that will work well in your situation because they increment/decrement the integer values with locking:
http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html[
^]
I also recommend you to get some lessons on multithreading because with your current knowledge you will produce hell buggy multithreaded code and threading bugs are often hard to reproduce/find. Some threading bugs occur only once a month or so but then they can deadlock your program!