summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-01-14 15:33:12 +0100
committerTyge Løvset <[email protected]>2022-01-14 15:33:12 +0100
commit349ba35358f10923a62947aaee8056d14f1be74a (patch)
tree22efc73801246d9bcb233d807485d189593dd331 /examples
parent64c15e66853f766102aac2642a45d4940d0bb42c (diff)
downloadSTC-modified-349ba35358f10923a62947aaee8056d14f1be74a.tar.gz
STC-modified-349ba35358f10923a62947aaee8056d14f1be74a.zip
Fixed function linkage spec in cstr. Moved typedefs of cstr and csview to forward.h-
Diffstat (limited to 'examples')
-rw-r--r--examples/threads_demo.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/examples/threads_demo.c b/examples/threads_demo.c
index 667ffb5b..24ac951a 100644
--- a/examples/threads_demo.c
+++ b/examples/threads_demo.c
@@ -19,10 +19,10 @@ mtx_t mtx;
void* thr(BaseArc* p)
{
- thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
+ struct timespec one_sec = {.tv_sec=1};
+ thrd_sleep(&one_sec, NULL);
BaseArc lp = BaseArc_clone(*p); // thread-safe, even though the
// shared use_count is incremented
-
c_autoscope (mtx_lock(&mtx), mtx_unlock(&mtx))
{
printf("local pointer in a thread:\n"
@@ -30,7 +30,6 @@ void* thr(BaseArc* p)
/* safe to modify base here */
lp.get->value += 1;
}
- /* atomically decrease ref. */
BaseArc_drop(&lp);
BaseArc_drop(p);
return NULL;
@@ -43,17 +42,19 @@ int main()
mtx_init(&mtx, mtx_plain);
printf("Created a shared Base\n"
" p.get() = %p, p.use_count() = %ld\n", (void*)p.get, BaseArc_use_count(p));
- thrd_t t1, t2, t3;
- thrd_create(&t1, (thrd_start_t)thr, (BaseArc[]){BaseArc_clone(p)});
- thrd_create(&t2, (thrd_start_t)thr, (BaseArc[]){BaseArc_clone(p)});
- thrd_create(&t3, (thrd_start_t)thr, (BaseArc[]){BaseArc_clone(p)});
+ enum {N = 3};
+ struct { thrd_t t; BaseArc p; } task[N];
+ c_forrange (i, N) {
+ task[i].p = BaseArc_clone(p);
+ thrd_create(&task[i].t, (thrd_start_t)thr, &task[i].p);
+ }
BaseArc_reset(&p);
- printf("Shared ownership between 3 threads and released\n"
+ printf("Shared ownership between %d threads and released\n"
"ownership from main:\n"
- " p.get() = %p, p.use_count() = %ld\n", (void*)p.get, BaseArc_use_count(p));
+ " p.get() = %p, p.use_count() = %ld\n", N, (void*)p.get, BaseArc_use_count(p));
- thrd_join(t1, NULL); thrd_join(t3, NULL); thrd_join(t3, NULL);
+ c_forrange (i, N) thrd_join(task[i].t, NULL);
printf("All threads completed, the last one deleted Base\n");
}