OK you have a mutex to protect access to you data in the threads but you do not use it when access the data in main. This is an error whenever you access the data even for read you should always lock the mutex protecting its access.
It might be said that the read of the variable is atomic (i.e. can't be interrupted and is therefore safe without the mutex), it may be but you really can't be 100% sure what assembly code will be produced from your code particularly if you code is meant to be platform independent.
As to scheduling, the shecduler is not deterministict. If you need your threads to run in a specific order then you need to do it programatically. Various ways you might do this, only start thread 2 when thread 1 has finished, use some sort on sychronisation object (a semaphore for example) to hold up thread 2 until thread 1 has finished or design and write your program so that this type of synchronisation is not required.
Finally I do not thing pthread_exit does what you think it does. It exits the current thread with a return value (see http://linux.die.net/man/3/pthread_exit) it does not exit a thread identified by its id. You may want to be calling pthread_join though.
Banfa
Practically a Master Poster
695 posts since Mar 2010
Reputation Points: 508
Solved Threads: 109
Skill Endorsements: 5