How index is affected when multiple threads are running in a loop

Asked
Active3 hr before
Viewed126 times

4 Answers

runningindex
90%

Though all 5 threads run why the corresponding indexes are not properly outputted? Why do I tend to loose some indexes and get others twice? For example in this case I have lost 3 and got 0 outputted twice. Though using pthread_join along with pthread_create in one loop solves the problem it doesn't schedule all threads to run in parallel. What should be done in this case to get all indexes printed?,Because the main thread, the thread that modifies the value seen by each worker thread, participates in the synchronization, so that each worker function reads the value before the next thread is created, the output will always be in increasing order of i.,You pass a pointer to a variable to each thread, and modify that variable at the same time the thread functions access it. Why would you expect the thread functions to see any particular value? They run concurrently. It is possible for the threads to see a completely impossible, garbled value, if one thread is reading the value while another is modifying it, on some architectures.,For example, on x86-64 (aka AMD64; the 64-bit Intel/AMD architecture), all reads and writes are observed in order, except that stores may be ordered after loads. This means that if initially say i = 0;, and thread A does i = 1;, thread B can still see i == 0 even after thread A modified the variable.

The simplest option is to provide the index to be printed as a value, rather than as a pointer to a variable. In busy(), use

my_busy = (int)(intptr_t) c;
load more v
88%

Loop dependencies can result from stores into arrays that are indexed in the loop by subscripts whose values are not known. For example, indirect addressing could be order dependent if there are repeated values in the index array: ,Under automatic parallelization, the compilers do not parallelize a loop if: ,Some variable in the loop has a loop-carried dependency. ,Compile the changed original routine with parallelization and no -g.

   DO I = 2, N
      A(I) = A(I - 1) * B(I) + C(I)
   END DO
load more v
72%

Hi I want to spawn multiple threads with each thread having an index input argument like,for (int i = 0;i < 5;i++) begin fork thread(i) join_none end,for(int i = 0; i < 5; i++) begin automatic int j; j = i; fork thread(j); join_none end,But since i is a common variable for all threads, all threads are spawned with index 4. I don't want to use join_all as i want all threads to be spawned simultaneously.

for (int i = 0; i < 5; i++) begin
automatic int j;
j = i;
fork
thread(j);
join_none
end
load more v
65%

Now I'll perform the same loop using parallel processing:,And this is the same loop using Parallel.ForEach:,“An application's ability to demonstrate to the user that many things are happening at the same time is often more important than its true measure of speed.”,There are many times when you need an asynchronous process to return a value when it is finished. The Task class makes this very easy as well.

In its simplest form, a task defined by the Task class contains a snippet of code that defines the operation that will execute.

Task task = new Task(() => {
   Console.WriteLine("Task on thread {0} started.",
      Thread.CurrentThread.ManagedThreadId);
   Thread.Sleep(3000);
   Console.WriteLine("Task on thread {0} finished.",
      Thread.CurrentThread.ManagedThreadId);
});
load more v

Other "running-index" queries related to "How index is affected when multiple threads are running in a loop"