0

In pure Python (threads co-exist but not parallel), all threads can be waited to be all done by using join method:

ts = [] # Threads

for i in range(10):
    ts.append(Thread(func=...))
    ts[-1].start()

for i in range(len(ts)):
    ts[i].join() # Main thread waits for all these

In Cython, threads are created using prange:

for i in prange(len(data), nogil=True, num_threads=10):
    # Do something with data[i] in an uncertain thread id
    pass    

# Main thread back here
cython.parallel.join_all_prange_threads() # Need something like this
do_something_with_results_by_prange_threads()

Cython parallel module has prange, parallel, threadid but no join. Any way to wait for all threads in prange to be done first, in main thread? Since the code after depend on the results of prange threads.

Dee
  • 7,455
  • 6
  • 36
  • 70
  • 4
    You don’t have to wait after prange. Prange waits automatically, ie after prange there is only one thread. – ead Oct 28 '21 at 04:56
  • but i'm afraid that after spawning threads, the main thread is back to the next code line after `for prang` immediately – Dee Oct 28 '21 at 06:16
  • 3
    It isn't. `prange` essentially translates to an OpenMP parallel for loop. That doesn't give you the flexibility of creating the threads yourself, but it simplifies "split a loop into threads" so that you don't need to worry about `join` or creating the threads. – DavidW Oct 28 '21 at 08:18
  • @ead+dave i could see the main thread was back only after all threads in prange had been done but wasn't so sure. tks guys, now it's rather confirmed that the join is implied – Dee Oct 28 '21 at 13:43

0 Answers0