12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package main
- import "core:fmt"
- import "core:thread"
- import "core:sync"
- import "core:time"
- import "core:mem"
- fact :: proc(n: u64) -> u64{
- return fact_h(n, 1)
- }
- fact_h :: proc(n: u64, a: u64) -> u64{
- if n == 0{
- return a
- }else{
- return fact_h(n - 1, n * a)
- }
- }
- main :: proc(){
- wg : sync.Wait_Group
-
- threadPool :thread.Pool
- //thread.pool_init(&threadPool, context.allocator, 5)
- //thread.pool_init(&threadPool, context.allocator, 35_000) // fails
- thread.pool_init(&threadPool, context.allocator, 30_000)
- thread.pool_start(&threadPool)
- defer thread.pool_destroy(&threadPool)
-
- client_arena :mem.Arena
- //mem.arena_init(&client_arena, make([]u8, 1024))
- mem.arena_init(&client_arena, make([]u8, 64))
- client_allocator := mem.arena_allocator(&client_arena)
- //for i := 0; i < 5; i += 1 {
- //for i := 0; i < 35_000; i += 1 { // fails
- for i := 0; i < 30_000; i += 1 {
- thread.pool_add_task(&threadPool, client_allocator, worker, &{}, i)
- }
- thread.pool_finish(&threadPool)
- fmt.println("The End")
- }
- worker :: proc (t: thread.Task){
- fmt.printf("working on thread %d\n", t.user_index)
- //time.sleep(time.Second)
- v20: u64
- for {
- time.sleep(3 * time.Second)
- //v20 := fact(20)
- v20 = fact(20)
- fmt.printf("worker = %d fact 20 = %d\n", t.user_index, v20)
- }
- fmt.printf("worker finish %d\n", t.user_index)
- }
- /*
- make o thr5
- odin run thr5.odin -file
- working on thread 1
- working on thread 0
- working on thread 4
- working on thread 3
- working on thread 2
- End
- make o thr5
- odin run thr5.odin -file
- working on thread 0
- working on thread 1
- working on thread 2
- working on thread 3
- working on thread 4
- End
- */
- /*
- ...
- worker = 29573 fact 20 = 2432902008176640000
- worker = 22138 fact 20 = 2432902008176640000
- worker = 29496 fact 20 = 2432902008176640000
- worker = 27826 fact 20 = 2432902008176640000
- worker = 21968 fact 20 = 2432902008176640000
- ...
- */
|