thr5.odin 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, 35_000) // fails
  22. thread.pool_init(&threadPool, context.allocator, 30_000)
  23. thread.pool_start(&threadPool)
  24. defer thread.pool_destroy(&threadPool)
  25. client_arena :mem.Arena
  26. //mem.arena_init(&client_arena, make([]u8, 1024))
  27. mem.arena_init(&client_arena, make([]u8, 64))
  28. client_allocator := mem.arena_allocator(&client_arena)
  29. //for i := 0; i < 5; i += 1 {
  30. //for i := 0; i < 35_000; i += 1 { // fails
  31. for i := 0; i < 30_000; i += 1 {
  32. thread.pool_add_task(&threadPool, client_allocator, worker, &{}, i)
  33. }
  34. thread.pool_finish(&threadPool)
  35. fmt.println("The End")
  36. }
  37. worker :: proc (t: thread.Task){
  38. fmt.printf("working on thread %d\n", t.user_index)
  39. //time.sleep(time.Second)
  40. v20: u64
  41. for {
  42. time.sleep(3 * time.Second)
  43. //v20 := fact(20)
  44. v20 = fact(20)
  45. fmt.printf("worker = %d fact 20 = %d\n", t.user_index, v20)
  46. }
  47. fmt.printf("worker finish %d\n", t.user_index)
  48. }
  49. /*
  50. make o thr5
  51. odin run thr5.odin -file
  52. working on thread 1
  53. working on thread 0
  54. working on thread 4
  55. working on thread 3
  56. working on thread 2
  57. End
  58. make o thr5
  59. odin run thr5.odin -file
  60. working on thread 0
  61. working on thread 1
  62. working on thread 2
  63. working on thread 3
  64. working on thread 4
  65. End
  66. */
  67. /*
  68. ...
  69. worker = 29573 fact 20 = 2432902008176640000
  70. worker = 22138 fact 20 = 2432902008176640000
  71. worker = 29496 fact 20 = 2432902008176640000
  72. worker = 27826 fact 20 = 2432902008176640000
  73. worker = 21968 fact 20 = 2432902008176640000
  74. ...
  75. */