ThreadingΒΆ

#import "std/thread"
#import "std/sync"

mutex: Sync.Mutex;

main :: fn () s32 {
    print("Start thread!!!\n");
    defer print("Threads joined!!!\n");

    Sync.init(&mutex);
    defer Sync.terminate(&mutex);

    t1 :: Thread.create(&first);
    t2 :: Thread.create(&second);

    Thread.join(t1);
    Thread.join(t2);

    return 0;
}

first :: fn (args: *u8) s32 {
    loop i := 0; i < 10; i += 1 {
        Sync.lock(&mutex);
        
        print("Hello from first thread!\n");

        Sync.unlock(&mutex);
        os_sleep_ms(50.0);
    }
    return 0;
}

second :: fn (args: *u8) s32 {
    loop i := 0; i < 10; i += 1 {
        Sync.lock(&mutex);
        
        print("Hello from second thread!\n");

        Sync.unlock(&mutex);
        os_sleep_ms(100.0);
    }
    return 0;
}