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":
<cpp>
<c>
HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
</cpp>
</c>
To lock the mutex:
To lock the mutex:
<cpp>
<c>
WaitForSingleObject(hMutex, INFINITE);
WaitForSingleObject(hMutex, INFINITE);
</cpp>
</c>
To unlock the mutex
To unlock the mutex
<cpp>
<c>
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
</cpp>
</c>
When the program is finished with the mutex:
When the program is finished with the mutex:
<cpp>
<c>
CloseHandle(hMutex);
CloseHandle(hMutex);
</cpp>
</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}}