mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-26 01:13:48 +00:00
Implement pthread_cancel.
This commit is contained in:
+28
-1
@@ -1110,9 +1110,16 @@ prefix_table := [?]string{
|
||||
"Black",
|
||||
}
|
||||
|
||||
print_mutex := b64(false)
|
||||
|
||||
threading_example :: proc() {
|
||||
fmt.println("\n# threading_example")
|
||||
|
||||
did_acquire :: proc(m: ^b64) -> (acquired: bool) {
|
||||
res, ok := intrinsics.atomic_compare_exchange_strong(m, false, true)
|
||||
return ok && res == false
|
||||
}
|
||||
|
||||
{ // Basic Threads
|
||||
fmt.println("\n## Basic Threads")
|
||||
worker_proc :: proc(t: ^thread.Thread) {
|
||||
@@ -1154,14 +1161,21 @@ threading_example :: proc() {
|
||||
task_proc :: proc(t: thread.Task) {
|
||||
index := t.user_index % len(prefix_table)
|
||||
for iteration in 1..=5 {
|
||||
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)
|
||||
|
||||
print_mutex = false
|
||||
|
||||
time.sleep(1 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
N :: 3
|
||||
|
||||
pool: thread.Pool
|
||||
thread.pool_init(pool=&pool, thread_count=3, allocator=context.allocator)
|
||||
thread.pool_init(pool=&pool, thread_count=N, allocator=context.allocator)
|
||||
defer thread.pool_destroy(&pool)
|
||||
|
||||
|
||||
@@ -1171,6 +1185,19 @@ threading_example :: proc() {
|
||||
}
|
||||
|
||||
thread.pool_start(&pool)
|
||||
|
||||
{
|
||||
// Wait a moment before we cancel a thread
|
||||
time.sleep(5 * time.Millisecond)
|
||||
|
||||
// Allow one thread to print at a time.
|
||||
for !did_acquire(&print_mutex) { thread.yield() }
|
||||
|
||||
thread.terminate(pool.threads[N - 1], 0)
|
||||
fmt.println("Canceled last thread")
|
||||
print_mutex = false
|
||||
}
|
||||
|
||||
thread.pool_finish(&pool)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user