12.3.56 SP_mutex_lock()

Synopsis

     #include <sicstus/sicstus.h>
     
     static SP_mutex volatile mutex = SP_MUTEX_INITIALIZER;
     
     int
     SP_mutex_lock(SP_mutex *pmx);

Locks the mutex.

Return Value

Zero on error, non-zero on success.

Examples

     static SP_mutex volatile my_mutex = SP_MUTEX_INITIALIZER;
     // only access this counter with my_mutex locked
     int volatile protected_counter = 0;
     
     // returns the new value of protected_counter
     int increment_the_counter(void)
     {
       int new_value;
     
       if(SP_mutex_lock(&my_mutex) == 0) goto error_handling;
       // No other thread can update protected_counter here
       new_value = protected_counter+1;
       protected_counter = new_value;
       if (SP_mutex_unlock(&my_mutex) == 0) goto error_handling;
       return new_value;
     
     error_handling:
       ...
     }

See Also

OS Threads.


Send feedback on this subject.