Jump to content

Mutex: Difference between revisions

→‎{{header|C}}: posix mutex
(Some explanation of Ada implementation)
(→‎{{header|C}}: posix mutex)
Line 68:
It is also possible to implement mutex as a monitor task.
=={{header|C}}==
 
===Win32===
{{works with|Win32}}
To create a mutex operating system "object":
<cppc>
HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
</cppc>
To lock the mutex:
<cppc>
WaitForSingleObject(hMutex, INFINITE);
</cppc>
To unlock the mutex
<cppc>
ReleaseMutex(hMutex);
</cppc>
When the program is finished with the mutex:
<cppc>
CloseHandle(hMutex);
</cppc>
 
===POSIX===
{{works with|POSIX}}
 
Creating a mutex:
 
<c>#include <pthread.h>
 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;</c>
 
Locking:
 
<c>pthread_mutex_lock(&mutex);</c>
 
Unlocking:
 
<c>pthread_mutex_unlock(&mutex);</c>
 
=={{header|C++}}==
{{works with|Win32}}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.