Threads (POSIX Threads) on C. As each iteration to complete half of the threads?

Hello, I have a problem. You need to write a multithreaded program to sort the array merging algorithm (merge sort). The program receives two parameters, the size of the array (for sorting) and the number of threads. Filled with an array of randomname numbers (for training purposes). The size of the array and the number of threads is the number of degree 2. (I.e., 2, 4, 8, 16, etc - also for ease of training purposes). Example array of 16 elements and the number of threads 4. When a thread is created passing in the array number "I" by which it calculates the index into the array which will work, that is an example of "thread 0" will sort from 0 to 4 (0,1,2,3). And so all of the threads.

That's what I have a problem. After sorting, you need to "kill" the flow of neighbor and merge it with an array. For example, stream "0" kills the flow "1" and call the merge function. (from position 0 to 8). And so to continue until there is one thread that will merge the two pieces of the array. There is an array which stores the thread ID, the array size is always 64 (the maximum number of threads of the program).
I understand the need to do all through the While loop until the number of threads is equal to 1. At each iteration, the threads will wait until all finish, and then "murder" :))))) But how to synchronize the flows that would be after completed every second thread, and the one that was called the mail merge feature?

P. S Sorry maybe I am very stupid, but if someone understand and can help then thank You very much))))
June 10th 19 at 16:30
0 answer

Find more questions by tags Cmultithreading