#include <stdio.h>
+#include <stdlib.h>
#include "libthreads.h"
#include "libatomic.h"
for (i = 0; i < 2; i++) {
printf("Thread %d, loop %d\n", thrd_current(), i);
- switch (i ) {
+ switch (i % 2) {
case 1:
ret = atomic_load(obj);
printf("Read value: %d\n", ret);
void user_main()
{
thrd_t t1, t2;
- atomic_int obj;
+ atomic_int *obj;
- atomic_init(&obj, 0);
+ obj = malloc(sizeof(*obj));
+
+ atomic_init(obj, 0);
printf("Thread %d: creating 2 threads\n", thrd_current());
- thrd_create(&t1, (thrd_start_t)&a, &obj);
- thrd_create(&t2, (thrd_start_t)&a, &obj);
+ thrd_create(&t1, (thrd_start_t)&a, obj);
+ thrd_create(&t2, (thrd_start_t)&a, obj);
thrd_join(t1);
thrd_join(t2);
+ free(obj);
printf("Thread %d is finished\n", thrd_current());
}