Mutex: Difference between revisions
Content deleted Content added
Some explanation of Ada implementation |
→{{header|C}}: posix mutex |
||
Line 68: | Line 68: | ||
It is also possible to implement mutex as a monitor task. |
It is also possible to implement mutex as a monitor task. |
||
=={{header|C}}== |
=={{header|C}}== |
||
===Win32=== |
|||
{{works with|Win32}} |
{{works with|Win32}} |
||
To create a mutex operating system "object": |
To create a mutex operating system "object": |
||
< |
<c> |
||
HANDLE hMutex = CreateMutex(NULL, FALSE, NULL); |
HANDLE hMutex = CreateMutex(NULL, FALSE, NULL); |
||
</ |
</c> |
||
To lock the mutex: |
To lock the mutex: |
||
< |
<c> |
||
WaitForSingleObject(hMutex, INFINITE); |
WaitForSingleObject(hMutex, INFINITE); |
||
</ |
</c> |
||
To unlock the mutex |
To unlock the mutex |
||
< |
<c> |
||
ReleaseMutex(hMutex); |
ReleaseMutex(hMutex); |
||
</ |
</c> |
||
When the program is finished with the mutex: |
When the program is finished with the mutex: |
||
< |
<c> |
||
CloseHandle(hMutex); |
CloseHandle(hMutex); |
||
</ |
</c> |
||
===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++}}== |
=={{header|C++}}== |
||
{{works with|Win32}} |
{{works with|Win32}} |