I needed to get some thread help on home windows with quite simple headers for a library that I am engaged on however it needed to work on each Home windows and Linux. Since C11 threads usually are not broadly supported, this was an important alternative to be taught extra in regards to the Home windows API and POSIX threads. I made a decision to put up this right here as a result of I am not very skilled with threads and mutexes from each Home windows and POSIX and I might prefer it to be reviewed. Some issues are omitted as a result of they’re a part of the aforementioned library.
flags.h
/**
* cmc_flags
*
* Defines frequent error codes utilized by the whole library.
*/
static struct
{
int OK; // No errors
int THREAD; // Generic error relating to threads
int MUTEX; // Generic error relating to mutexes
} cmc_flags = { 0, /* Omitted */ 8, 9 };
mutex.h
/**
* A quite simple, header-only and minimalistic cross-platform mutex
*
* Varieties
* - cmc_mutex
*
* Capabilities
* - cmc_mtx_init
* - cmc_mtx_destroy
* - cmc_mtx_lock
* - cmc_mtx_unlock
* - cmc_mtx_trylock
*/
#embrace <stdbool.h>
#embrace "flags.h"
#if outlined(_WIN32)
#outline CMC_MUTEX_WINDOWS
#elif outlined(__unix__)
#outline CMC_MUTEX_UNIX
#else
#error "Unknown platform for CMC Mutex"
#endif
/* Platform particular contains */
#if outlined(CMC_MUTEX_WINDOWS)
#embrace <home windows.h>
#elif outlined(CMC_MUTEX_UNIX)
#embrace <errno.h>
#embrace <pthread.h>
#endif
/**
* struct cmc_mutex
*
* A mutex wrapper.
*/
struct cmc_mutex
{
#if outlined(CMC_MUTEX_WINDOWS)
HANDLE mutex;
#elif outlined(CMC_MUTEX_UNIX)
pthread_mutex_t mutex;
#endif
int flag;
};
/**
* Purchase sources for a mutex.
*
* param mtx An uninitialized mutex wrapper.
* return True or false if the mutex was efficiently initialized.
*/
static inline bool cmc_mtx_init(struct cmc_mutex *mtx)
{
#if outlined(CMC_MUTEX_WINDOWS)
mtx->mutex = CreateMutex(NULL, FALSE, NULL);
if (mtx->mutex == NULL)
mtx->flag = cmc_flags.MUTEX;
else
mtx->flag = cmc_flags.OK;
return mtx->mutex != NULL;
#elif outlined(CMC_MUTEX_UNIX)
int err = pthread_mutex_init(&(mtx->mutex), NULL);
if (err != 0)
mtx->flag = cmc_flags.MUTEX;
else
mtx->flag = cmc_flags.OK;
return err == 0;
#endif
}
/**
* Launch all sources from a mutex. Calling this operate on a locked mutex
* causes undefined habits.
*
* param mtx A mutex to be destroyed.
* return True or false if the mutex was efficiently destroyed.
*/
static inline bool cmc_mtx_destroy(struct cmc_mutex *mtx)
{
#if outlined(CMC_MUTEX_WINDOWS)
if (!CloseHandle(mtx->mutex))
{
mtx->flag = cmc_flags.MUTEX;
return false;
}
mtx->flag = cmc_flags.OK;
return true;
#elif outlined(CMC_MUTEX_UNIX)
if (pthread_mutex_destroy(&(mtx->mutex)) != 0)
{
mtx->flag = cmc_flags.MUTEX;
return false;
}
mtx->flag = cmc_flags.OK;
return true;
#endif
}
/**
* Locks a mutex. If the mutex is already locked, the thread is blocked till
* the mutex could be acquired.
*
* param mtx A mutex to be locked.
* return True or false if the lock on the mutex was efficiently acquired.
*/
static inline bool cmc_mtx_lock(struct cmc_mutex *mtx)
{
#if outlined(CMC_MUTEX_WINDOWS)
DWORD consequence = WaitForSingleObject(mtx->mutex, INFINITE);
if (consequence == WAIT_FAILED || consequence == WAIT_ABANDONED)
{
mtx->flag = cmc_flags.MUTEX;
return false;
}
mtx->flag = cmc_flags.OK;
return true;
#elif outlined(CMC_MUTEX_UNIX)
if (pthread_mutex_lock(&mtx->mutex) != 0)
{
mtx->flag = cmc_flags.MUTEX;
return false;
}
mtx->flag = cmc_flags.OK;
return true;
#endif
}
/**
* Unlocks a mutex. If the mutex is already locked, the thread is blocked till
* the mutex could be acquired.
*
* param mtx A mutex to be unlocked.
* return True or false if the mutex was efficiently unlocked.
*/
static inline bool cmc_mtx_unlock(struct cmc_mutex *mtx)
{
#if outlined(CMC_MUTEX_WINDOWS)
if (!ReleaseMutex(mtx->mutex))
mtx->flag = cmc_flags.MUTEX;
else
mtx->flag = cmc_flags.OK;
return mtx->flag == cmc_flags.OK;
#elif outlined(CMC_MUTEX_UNIX)
if (pthread_mutex_unlock(&mtx->mutex) != 0)
mtx->flag = cmc_flags.MUTEX;
else
mtx->flag = cmc_flags.OK;
return mtx->flag == cmc_flags.OK;
#endif
}
/**
* Tries to lock a mutex. If the mutex is already locked, the thread will not be
* blocked and the operate returns false.
*
* param mtx A mutex to attempt to lock.
* return True or false if the lock on the mutex was efficiently acquired.
*/
static inline bool cmc_mtx_trylock(struct cmc_mutex *mtx)
{
#if outlined(CMC_MUTEX_WINDOWS)
DWORD consequence = WaitForSingleObject(mtx->mutex, 0);
if (consequence == WAIT_FAILED || consequence == WAIT_ABANDONED)
{
mtx->flag = cmc_flags.MUTEX;
return false;
}
mtx->flag = cmc_flags.OK;
return consequence != WAIT_TIMEOUT;
#elif outlined(CMC_MUTEX_UNIX)
int err = pthread_mutex_trylock(&mtx->mutex);
if (err == EINVAL || err == EFAULT)
mtx->flag = cmc_flags.MUTEX;
else
mtx->flag = cmc_flags.OK;
return err == 0;
#endif
}
thread.h
/**
* A quite simple, header-only utility for spawning and becoming a member of threads
*
* Varieties
* - cmc_thread
* - cmc_thread_proc
*
* Capabilities
* - cmc_thrd_create
* - cmc_thrd_join
*/
#embrace <inttypes.h>
#embrace <stdbool.h>
#embrace "flags.h"
#if outlined(_WIN32)
#outline CMC_THREAD_WINDOWS
#elif outlined(__unix__)
#outline CMC_THREAD_UNIX
#else
#error "Unknown platform for CMC Threads"
#endif
#if outlined(CMC_THREAD_WINDOWS)
#embrace <home windows.h>
#elif outlined(CMC_THREAD_UNIX)
#embrace <pthread.h>
#endif
/* The kind of a course of run by a thread */
typedef int (*cmc_thread_proc)(void *);
/**
* struct cmc_thread
*
* A thread wrapper.
*/
struct cmc_thread
{
#if outlined(CMC_THREAD_WINDOWS)
HANDLE thread;
#elif outlined(CMC_THREAD_UNIX)
pthread_t thread;
#endif
int flag;
void *args;
cmc_thread_proc course of;
};
/* A thread wrapper */
#if outlined(CMC_THREAD_WINDOWS)
static DWORD cmc_thread_wrapper(void *arg)
#elif outlined(CMC_THREAD_UNIX)
static void *cmc_thread_wrapper(void *arg)
#endif
{
struct cmc_thread *thr = (struct cmc_thread *)arg;
int consequence = thr->course of(thr->args);
#if outlined(CMC_THREAD_WINDOWS)
return consequence;
#elif outlined(CMC_THREAD_UNIX)
return (void *)(intptr_t)consequence;
#endif
}
/**
* Creates and spawns a brand new thread.
*
* param thr An uninitialized thread wrapper.
* param proc Pointer to a operate that can run in a brand new thread.
* param args The arguments handed to the proc operate.
* return True or false if the thread creation was successfull.
*/
static inline bool cmc_thrd_create(struct cmc_thread *thr, cmc_thread_proc proc,
void *args)
{
#if outlined(CMC_THREAD_WINDOWS)
thr->args = args;
thr->course of = proc;
thr->thread = CreateThread(NULL, 0, cmc_thread_wrapper, thr, 0, NULL);
if (!thr->thread)
thr->flag = cmc_flags.THREAD;
else
thr->flag = cmc_flags.OK;
return thr->flag == cmc_flags.OK;
#elif outlined(CMC_THREAD_UNIX)
thr->args = args;
thr->course of = proc;
if (pthread_create(&(thr->thread), NULL, cmc_thread_wrapper, thr) != 0)
thr->flag = cmc_flags.THREAD;
else
thr->flag = cmc_flags.OK;
return thr->flag == cmc_flags.OK;
#endif
}
/**
* Blocks the present thread till the goal thread terminates.
*
* param thr The goal thread to attend on.
* param consequence The worth returned by the goal thread. Optionally available.
* return True or false if the be part of was successfull.
*/
static inline bool cmc_thrd_join(struct cmc_thread *thr, int *consequence)
{
#if outlined(CMC_THREAD_WINDOWS)
if (WaitForSingleObject(thr->thread, INFINITE) == WAIT_FAILED)
{
thr->flag = cmc_flags.THREAD;
return false;
}
DWORD exit_code;
if (consequence)
{
if (GetExitCodeThread(thr->thread, &exit_code) == 0)
{
thr->flag = cmc_flags.THREAD;
return false;
}
else
*consequence = (int)exit_code;
}
thr->flag = cmc_flags.OK;
if (CloseHandle(thr->thread) == 0)
{
thr->flag = cmc_flags.THREAD;
return false;
}
thr->flag = cmc_flags.OK;
return true;
#elif outlined(CMC_THREAD_UNIX)
void *exit_value;
if (pthread_join(thr->thread, &exit_value) != 0)
{
thr->flag = cmc_flags.THREAD;
return false;
}
if (consequence)
{
*consequence = (int)(intptr_t)exit_value;
}
thr->flag = cmc_flags.OK;
return true;
#endif
}
instance.c
#embrace <stdio.h>
#embrace <stdlib.h>
#embrace "mutex.h"
#embrace "thread.h"
#outline NTHREADS 1000
struct cmc_mutex lck;
/* What number of trylock failed */
struct cmc_mutex flck;
int trylock_fails = 0;
int thread_process(void *arg)
{
int *sum = (int *)arg;
if (rand() % 2 == 0)
{
cmc_mtx_lock(&lck);
for (int i = 0; i < 1000; i++)
*sum += 1;
cmc_mtx_unlock(&lck);
}
else
{
if (!cmc_mtx_trylock(&lck))
{
cmc_mtx_lock(&flck);
trylock_fails += 1;
cmc_mtx_unlock(&flck);
return 0;
}
for (int i = 0; i < 1000; i++)
*sum += 1;
cmc_mtx_unlock(&lck);
}
return 0;
}
int essential(void)
{
cmc_mtx_init(&lck);
cmc_mtx_init(&flck);
struct cmc_thread threads[NTHREADS];
int sum = 0;
for (int i = 0; i < NTHREADS; i++)
cmc_thrd_create(&threads[i], thread_process, &sum);
for (int i = 0; i < NTHREADS; i++)
cmc_thrd_join(&threads[i], NULL);
printf("Whole sum : %dn", sum);
printf("Failed trylocks : %dn", trylock_fails);
}
```