odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.44k stars 565 forks source link

Odin join does not wait for thread to start before joining. #3924

Open xzores opened 1 month ago

xzores commented 1 month ago

Context

Observe the following code, using the build in libraries:

main :: proc () {

    some_thread_func :: proc () {
        fmt.printf("Thread 2 started");
        time.sleep(10 * time.Second);
        fmt.printf("Thread 2 ended");
    }

    my_thread := thread.create_and_start(some_thread_func);
    thread.join(my_thread);

    fmt.printf("Finished without crashing");
}

The output is : Finished without crashing and nothing more.

Odin: dev-2024-07-nightly:b4ca044 OS: Windows 11 Home Basic (version: 23H2), build 22631.3880 CPU: 13th Gen Intel(R) Core(TM) i9-13900HX RAM: 32507 MiB Backend: LLVM 17.0.1

Expected Behavior

I would expect join to wait until the thread is done. So I would expect the output to be:

Thread 2 started
Thread 2 ended
Finished without crashing

Current Behavior

In the current behavior the program terminates almost instantly, it does not wait for the second thread.

Failure Information (for bugs)

It will wait for the thread, if there is placed a signifiently long time between create_and_start and join. This can be observed in this example:

main :: proc () {

    some_thread_func :: proc () {
        fmt.printf("Thread 2 started\n");
        time.sleep(10 * time.Second);
        fmt.printf("Thread 2 ended\n");
    }

    my_thread := thread.create_and_start(some_thread_func);
    time.sleep(1 * time.Second);
    thread.join(my_thread);

    fmt.printf("Finished without crashing\n");
}

Where the output is:

Thread 2 started
Thread 2 ended
Finished without crashing

Steps to Reproduce

Cope pasta this:

import "core:thread"
import "core:time"
import "core:fmt"
main :: proc () {

    some_thread_func :: proc () {
        fmt.printf("Thread 2 started\n");
        time.sleep(10 * time.Second);
        fmt.printf("Thread 2 ended\n");
    }

    my_thread := thread.create_and_start(some_thread_func);
    thread.join(my_thread);

    fmt.printf("Finished without crashing\n");
}
Kelimion commented 1 month ago

We have a full rework of core:thread in the wings that needs a little TLC. Should come in within the next fortnight or so.

xzores commented 1 month ago

awesome, should I leave this open?

Kelimion commented 1 month ago

awesome, should I leave this open?

Please do.

xzores commented 1 month ago

I want to add: I also had a problem with self_clean, where it crashed and I wanted to make an example which lead me to this problem. It crashed when self_clean = true. It crashed on join

Kelimion commented 1 month ago

Noted, thanks for the extra clarification.