thr3.odin 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import "core:fmt"
  3. import "core:thread"
  4. import "core:time"
  5. import "core:sync"
  6. myMutex :sync.Mutex
  7. arr := []int{1,2,3}
  8. main :: proc(){
  9. // creating a dynamic array of len of array which stores pointer to a thread
  10. threadPool := make([dynamic]^thread.Thread, 0, len(arr))
  11. defer delete(threadPool)
  12. for i in 0..<len(arr){
  13. thr := thread.create(worker) // creating a thread
  14. if thr != nil{
  15. // setting up context for our thread
  16. thr.init_context = context
  17. // giving our thread id
  18. thr.user_index = i
  19. // Adding our thread to thread pool
  20. append(&threadPool, thr)
  21. thread.start(thr) // running our thread
  22. }
  23. }
  24. // looping till all of our threads have done their work
  25. // i.e till our threadPool is empty
  26. // our threadPool becomes empty because after each
  27. for len(threadPool) > 0{
  28. for i := 0; i < len(threadPool); {
  29. // Getting a threads address at index i
  30. t := threadPool[i]
  31. if thread.is_done(t){
  32. fmt.printf("Thread %d is done\n\n", arr[t.user_index])
  33. thread.destroy(t)
  34. // removing address of destroyed thread from out pool
  35. ordered_remove(&threadPool, i)
  36. }else{
  37. // If current thread process is not done then go to next one
  38. i += 1
  39. }
  40. }
  41. }
  42. }
  43. /*
  44. worker :: proc(t: ^thread.Thread){
  45. // with the help of context we get what thread we are using.
  46. fmt.printf("work of t%d\n", arr[t.user_index])
  47. }
  48. */
  49. worker :: proc(t: ^thread.Thread){
  50. /*
  51. when we come to this line
  52. it will check if myMutex is available or not
  53. if it isn't then it will wait for it
  54. and once its available then it will pass and run below code
  55. */
  56. //sync.lock(myMutex) // locking before we do our opeartion
  57. sync.mutex_lock(&myMutex) // locking before we do our opeartion
  58. fmt.printf("work of t%d started\n", arr[t.user_index])
  59. time.sleep(time.Second)
  60. fmt.printf("work of t%d completed\n", arr[t.user_index])
  61. //fmt.println()
  62. //sync.unlock(myMutex) // unlocking for other threads to use it
  63. sync.mutex_unlock(&myMutex) // unlocking for other threads to use it
  64. }
  65. /*
  66. odin run thr3.odin -file
  67. work of t1 started
  68. work of t1 completed
  69. Thread 1 is done
  70. work of t2 started
  71. work of t2 completed
  72. Thread 2 is done
  73. work of t3 started
  74. work of t3 completed
  75. Thread 3 is done
  76. odin run thr3.odin -file
  77. work of t2 started
  78. work of t2 completed
  79. Thread 2 is done
  80. work of t1 started
  81. work of t1 completed
  82. Thread 1 is done
  83. work of t3 started
  84. work of t3 completed
  85. Thread 3 is done
  86. */