CKTIS / C
mtx_init — initialize a mutex
mtx_init from <threads.h> initializes a mutex with selected properties. Check its result before using the object, and finish its lifecycle with mtx_destroy.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <threads.h>
int mtx_init(mtx_t *mtx, int type);mtx_init stores a new mutex identifier in the mtx_t object pointed to by mtx. It returns thrd_success or thrd_error; after failure, do not use the object as an initialized mutex.
type must be one of four values: mtx_plain, mtx_timed, mtx_plain | mtx_recursive, mtx_timed | mtx_recursive. The first two are non-recursive; mtx_recursive permits repeated locking by the same thread, and mtx_timed supports timeouts.
Initialize before sharing with other threads. Do not reinitialize a live mutex. Call mtx_destroy after use; no thread may then be waiting for that mutex.
Support for <threads.h> is optional; __STDC_NO_THREADS__ equal to 1 indicates its absence. The repository's C23 flag does not attest production runtime availability.
Examples
#include <stdio.h>
#include <stdlib.h>
#if defined(__STDC_NO_THREADS__) && __STDC_NO_THREADS__ == 1
#error "Threads support required"
#else
#include <threads.h>
int main(void)
{
mtx_t mutex;
if (mtx_init(&mutex, mtx_plain) != thrd_success) {
fputs("mtx_init failed\n", stderr);
return EXIT_FAILURE;
}
mtx_destroy(&mutex);
puts("OK");
return EXIT_SUCCESS;
}
#endif
Result
OK
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.