mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Revert "Use sync.Mutex rather than a spin lock for the threading example"
This reverts commit 8d1f46d837.
This commit is contained in:
+24
-21
@@ -1,15 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:intrinsics"
|
|
||||||
import "core:math/big"
|
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:reflect"
|
|
||||||
import "core:runtime"
|
|
||||||
import "core:sync"
|
|
||||||
import "core:thread"
|
import "core:thread"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
import "core:reflect"
|
||||||
|
import "core:runtime"
|
||||||
|
import "core:intrinsics"
|
||||||
|
import "core:math/big"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Odin is a general-purpose programming language with distinct typing built
|
Odin is a general-purpose programming language with distinct typing built
|
||||||
@@ -1101,18 +1100,19 @@ parametric_polymorphism :: proc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
prefix_table := [?]string{
|
||||||
|
"White",
|
||||||
|
"Red",
|
||||||
|
"Green",
|
||||||
|
"Blue",
|
||||||
|
"Octarine",
|
||||||
|
"Black",
|
||||||
|
}
|
||||||
|
|
||||||
|
print_mutex := b64(false)
|
||||||
|
|
||||||
threading_example :: proc() {
|
threading_example :: proc() {
|
||||||
@static prefix_table := [?]string{
|
|
||||||
"White",
|
|
||||||
"Red",
|
|
||||||
"Green",
|
|
||||||
"Blue",
|
|
||||||
"Octarine",
|
|
||||||
"Black",
|
|
||||||
}
|
|
||||||
|
|
||||||
@static print_mutex: sync.Mutex
|
|
||||||
|
|
||||||
fmt.println("\n# threading_example")
|
fmt.println("\n# threading_example")
|
||||||
|
|
||||||
did_acquire :: proc(m: ^b64) -> (acquired: bool) {
|
did_acquire :: proc(m: ^b64) -> (acquired: bool) {
|
||||||
@@ -1161,10 +1161,12 @@ threading_example :: proc() {
|
|||||||
task_proc :: proc(t: thread.Task) {
|
task_proc :: proc(t: thread.Task) {
|
||||||
index := t.user_index % len(prefix_table)
|
index := t.user_index % len(prefix_table)
|
||||||
for iteration in 1..=5 {
|
for iteration in 1..=5 {
|
||||||
if sync.guard(&print_mutex) {
|
for !did_acquire(&print_mutex) { thread.yield() } // Allow one thread to print at a time.
|
||||||
fmt.printf("Worker Task %d is on iteration %d\n", t.user_index, iteration)
|
|
||||||
fmt.printf("`%s`: iteration %d\n", prefix_table[index], iteration)
|
fmt.printf("Worker Task %d is on iteration %d\n", t.user_index, iteration)
|
||||||
}
|
fmt.printf("`%s`: iteration %d\n", prefix_table[index], iteration)
|
||||||
|
|
||||||
|
print_mutex = false
|
||||||
|
|
||||||
time.sleep(1 * time.Millisecond)
|
time.sleep(1 * time.Millisecond)
|
||||||
}
|
}
|
||||||
@@ -1189,10 +1191,11 @@ threading_example :: proc() {
|
|||||||
time.sleep(5 * time.Millisecond)
|
time.sleep(5 * time.Millisecond)
|
||||||
|
|
||||||
// Allow one thread to print at a time.
|
// Allow one thread to print at a time.
|
||||||
sync.guard(&print_mutex)
|
for !did_acquire(&print_mutex) { thread.yield() }
|
||||||
|
|
||||||
thread.terminate(pool.threads[N - 1], 0)
|
thread.terminate(pool.threads[N - 1], 0)
|
||||||
fmt.println("Canceled last thread")
|
fmt.println("Canceled last thread")
|
||||||
|
print_mutex = false
|
||||||
}
|
}
|
||||||
|
|
||||||
thread.pool_finish(&pool)
|
thread.pool_finish(&pool)
|
||||||
|
|||||||
Reference in New Issue
Block a user