You can use mutex to achieve this effect,
mutex.lock();
atomic
{
int temp = a[i];
a[i] =a[j];
a[j] = temp;
}
mutex.unlock();
If there are multiple threads you can use POSIX style Read/Write locks as follows,
pthread_rwlock_rdlock(rw_lock_ptr);
atomic
{
int temp = a[i];
a[i] =a[j];
a[j] = temp;
}
pthread_rwlock_unlock(rw_lock_ptr);