thr5.odin 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import "core:fmt"
  3. import "core:thread"
  4. import "core:sync"
  5. import "core:time"
  6. import "core:mem"
  7. fact :: proc(n: u64) -> u64{
  8. return fact_h(n, 1)
  9. }
  10. fact_h :: proc(n: u64, a: u64) -> u64{
  11. if n == 0{
  12. return a
  13. }else{
  14. return fact_h(n - 1, n * a)
  15. }
  16. }
  17. main :: proc(){
  18. wg : sync.Wait_Group
  19. threadPool :thread.Pool
  20. //thread.pool_init(&threadPool, context.allocator, 5)
  21. thread.pool_init(&threadPool, context.allocator, 30_000)
  22. thread.pool_start(&threadPool)
  23. defer thread.pool_destroy(&threadPool)
  24. client_arena :mem.Arena
  25. //mem.arena_init(&client_arena, make([]u8, 1024))
  26. mem.arena_init(&client_arena, make([]u8, 64))
  27. client_allocator := mem.arena_allocator(&client_arena)
  28. //for i := 0; i < 5; i += 1 {
  29. for i := 0; i < 30_000; i += 1 {
  30. thread.pool_add_task(&threadPool, client_allocator, worker, &{}, i)
  31. }
  32. thread.pool_finish(&threadPool)
  33. fmt.println("The End")
  34. }
  35. worker :: proc (t: thread.Task){
  36. fmt.printf("working on thread %d\n", t.user_index)
  37. //time.sleep(time.Second)
  38. v20: u64
  39. for {
  40. time.sleep(3 * time.Second)
  41. //v20 := fact(20)
  42. v20 = fact(20)
  43. fmt.printf("worker = %d fact 20 = %d\n", t.user_index, v20)
  44. }
  45. fmt.printf("worker finish %d\n", t.user_index)
  46. }
  47. /*
  48. make o thr5
  49. odin run thr5.odin -file
  50. working on thread 1
  51. working on thread 0
  52. working on thread 4
  53. working on thread 3
  54. working on thread 2
  55. End
  56. make o thr5
  57. odin run thr5.odin -file
  58. working on thread 0
  59. working on thread 1
  60. working on thread 2
  61. working on thread 3
  62. working on thread 4
  63. End
  64. */