From d85a4e9e396688b6b5b29c0ffb07de7ce9013b74 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 11 Jan 2022 20:51:50 +0100 Subject: Added tinycthread: include/threads.h, src/threads.c. Ex: sptr_threads.c - emulates standard C11 threads library for compilers not supporting C11 threads.h. Fixed and cleanup of CMake build. --- examples/sptr_pthread.c | 61 ------------------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 examples/sptr_pthread.c (limited to 'examples/sptr_pthread.c') diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c deleted file mode 100644 index d81d9810..00000000 --- a/examples/sptr_pthread.c +++ /dev/null @@ -1,61 +0,0 @@ -// example based on https://en.cppreference.com/w/cpp/memory/shared_ptr -#if defined __GNUC__ || defined __linux__ - -#include -#include -#include -#include - -struct Base -{ - int value; -} typedef Base; - -#define i_type BaseRc -#define i_val Base -#define i_valdrop(x) printf("Drop Base: %d\n", (x)->value) -#define i_opt c_no_cmp -#include - -void* thr(BaseRc* lp) -{ - sleep(1); - static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; - c_autoscope (pthread_mutex_lock(&mtx), pthread_mutex_unlock(&mtx)) - { - printf("local pointer in a thread:\n" - " p.get() = %p, p.use_count() = %ld\n", (void*)lp->get, *lp->use_count); - /* safe to modify base here */ - lp->get->value += 1; - } - /* atomically decrease ref. */ - BaseRc_drop(lp); - return NULL; -} - -int main() -{ - BaseRc p = BaseRc_from((Base){0}); - - printf("Created a Base\n" - " p.get() = %p, p.use_count() = %ld\n", (void*)p.get, *p.use_count); - enum {N = 3}; - pthread_t t[N]; - BaseRc c[N]; - c_forrange (i, N) { - c[i] = BaseRc_clone(p); - pthread_create(&t[i], NULL, (void*(*)(void*))thr, &c[i]); - } - - printf("Shared ownership between %d threads and released\n" - "ownership from main:\n" - " p.get() = %p, p.use_count() = %ld\n", N, (void*)p.get, *p.use_count); - BaseRc_reset(&p); - - c_forrange (i, N) pthread_join(t[i], NULL); - printf("All threads completed, the last one deleted Base\n"); -} - -#else -int main() {} -#endif -- cgit v1.2.3