aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-01-02 15:07:12 +0000
committergingerBill <bill@gingerbill.org>2020-01-02 15:07:12 +0000
commit3bd00fd6b7bea465ab9882e6b0f1b8e0842a4fa1 (patch)
tree0a606061b785be4090fce211f2ca3d396771e174 /examples
parent16a7c55334fafa48d5fd84e86071263f43a3724f (diff)
Add `thread.Pool` with example in demo.odin; Update linalg to support handness changes for projection matrices
Diffstat (limited to 'examples')
-rw-r--r--examples/demo/demo.odin76
1 files changed, 52 insertions, 24 deletions
diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin
index 7b3a07f73..935b24286 100644
--- a/examples/demo/demo.odin
+++ b/examples/demo/demo.odin
@@ -4,6 +4,7 @@ import "core:fmt"
import "core:mem"
import "core:os"
import "core:thread"
+import "core:time"
import "core:reflect"
import "intrinsics"
@@ -1102,38 +1103,65 @@ prefix_table := [?]string{
threading_example :: proc() {
fmt.println("\n# threading_example");
- worker_proc :: proc(t: ^thread.Thread) {
- for iteration in 1..5 {
- fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
- fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
- // win32.sleep(1);
+ { // Basic Threads
+ fmt.println("\n## Basic Threads");
+ worker_proc :: proc(t: ^thread.Thread) {
+ for iteration in 1..5 {
+ fmt.printf("Thread %d is on iteration %d\n", t.user_index, iteration);
+ fmt.printf("`%s`: iteration %d\n", prefix_table[t.user_index], iteration);
+ time.sleep(1 * time.Millisecond);
+ }
}
- }
- threads := make([dynamic]^thread.Thread, 0, len(prefix_table));
- defer delete(threads);
+ threads := make([dynamic]^thread.Thread, 0, len(prefix_table));
+ defer delete(threads);
- for in prefix_table {
- if t := thread.create(worker_proc); t != nil {
- t.init_context = context;
- t.use_init_context = true;
- t.user_index = len(threads);
- append(&threads, t);
- thread.start(t);
+ for in prefix_table {
+ if t := thread.create(worker_proc); t != nil {
+ t.init_context = context;
+ t.use_init_context = true;
+ t.user_index = len(threads);
+ append(&threads, t);
+ thread.start(t);
+ }
}
- }
- for len(threads) > 0 {
- for i := 0; i < len(threads); /**/ {
- if t := threads[i]; thread.is_done(t) {
- fmt.printf("Thread %d is done\n", t.user_index);
- thread.destroy(t);
+ for len(threads) > 0 {
+ for i := 0; i < len(threads); /**/ {
+ if t := threads[i]; thread.is_done(t) {
+ fmt.printf("Thread %d is done\n", t.user_index);
+ thread.destroy(t);
+
+ ordered_remove(&threads, i);
+ } else {
+ i += 1;
+ }
+ }
+ }
+ }
- ordered_remove(&threads, i);
- } else {
- i += 1;
+ { // Thread Pool
+ fmt.println("\n## Thread Pool");
+ task_proc :: proc(t: ^thread.Task) {
+ index := t.user_index % len(prefix_table);
+ for iteration in 1..5 {
+ fmt.printf("Worker Task %d is on iteration %d\n", t.user_index, iteration);
+ fmt.printf("`%s`: iteration %d\n", prefix_table[index], iteration);
+ time.sleep(1 * time.Millisecond);
}
}
+
+ pool: thread.Pool;
+ thread.pool_init(pool=&pool, thread_count=3);
+ defer thread.pool_destroy(&pool);
+
+
+ for i in 0..<30 {
+ thread.pool_add_task(pool=&pool, procedure=task_proc, data=nil, user_index=i);
+ }
+
+ thread.pool_start(&pool);
+ thread.pool_wait_and_process(&pool);
}
}