1

I have 3 functions:

  1. ReadInput (Reads the array of strings).
  2. Process the string (Reverses the string) [PARALLEL PROCESSING in MULTITHREADs]
  3. WriteOutput (Write the processed string to an output array)

For example: String Inputarr[] = {"RAM", "SHYAM", "CAT"}; It should write into Output Array as {"MAR", "MAYHS", "TAC"}

So the problem here is: I am reading from the InputArray in a sequence and then it is processed by multiple threads parallely, then how the threads will write into OutputArray in the same sequence as it was read from InputArray? Who decides the threads order to write?

Jay
  • 207
  • 2
  • 12
  • The answer to this depends on the code you've written. – Galik Aug 31 '16 at 04:21
  • Related: https://stackoverflow.com/questions/3227042/maintaining-order-in-a-multi-threaded-pipeline, https://stackoverflow.com/questions/36433652/multithreaded-execution-where-order-of-finished-work-items-is-preserved – Amit Naidu Mar 27 '23 at 22:43

2 Answers2

0

You need to provide some explicit synchronization to make this work. That's true both in the sense that you want to preserve the order and in the sense that your threads may attempt to write to the same element of your output array simultaneously, which will create an error.

Brick
  • 3,998
  • 8
  • 27
  • 47
0

then how the threads will write into OutputArray in the same sequence as it was read from InputArray? Who decides the threads order to write?

Create the thread functions as lambdas which captures by value the index of the destination slot.

Adrian Colomitchi
  • 3,974
  • 1
  • 14
  • 23