I am writing a C UNIX program that is threaded and shares one variable across all threads. What would be the easiest way to acquire a lock on this variable? I can’t find any small libraries just for locking in UNIX.
Any suggestions how to do this?
Thanks, Boda Cydo.
There’s pthread_mutex_lock, if you’re already using pthreads.
Quick example, where
counteris the shared variable andmutexis a mutex variable of typepthread_mutex_t: