diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2019-12-01 11:33:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-01 11:33:23 +0000 |
| commit | 3fd5c3cd851d8f4dfd441141ca7e96889f069933 (patch) | |
| tree | 67f47e79f5c5bb80a3ed1b1e9d79a61c08c0a29d /examples | |
| parent | 0c0c83ee295fe8787a4bdc8b826a5432abba2ca9 (diff) | |
| parent | 99121d6ff2b02f3d16b791eb103bb9f9e8b96475 (diff) | |
Merge pull request #458 from Tetralux/linux-threads
Implement core:thread and core:sync on Unix using pthreads
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/demo/demo.odin | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index 604d56695..67435f4ea 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -3,6 +3,7 @@ package main import "core:fmt" import "core:mem" import "core:os" +import "core:thread" import "core:reflect" import "intrinsics" @@ -1088,6 +1089,54 @@ parametric_polymorphism :: proc() { } +prefix_table := [?]string{ + "White", + "Red", + "Green", + "Blue", + "Octarine", + "Black", +}; + +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); + } + } + + 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 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; + } + } + } +} + + array_programming :: proc() { fmt.println("\n# array programming"); { @@ -1783,7 +1832,10 @@ main :: proc() { ranged_fields_for_array_compound_literals(); deprecated_attribute(); range_statements_with_multiple_return_values(); - soa_struct_layout(); + threading_example(); + + // TODO(tetra): When bill fixes SOA array comparison to nil in reserve_soa, we can re-enable this. + // soa_struct_layout(); } } |