Trying to synchronize the thread using Pthread_barrier_wait (), but the thread is not being synchronized if the number of iterations in the MAIN for the loop is greater than one here (S & LT; 2) is written, So for two iterations I have mentioned the output given below which is not synchronized.
Thank you.
This is my code:
#include & lt; Pthread.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Iostream & gt; #define ARRAYSIZE 6 # Define NUMTHREADS 3 using the namespace std; // barrier variable pthread_barrier_t barr; Unsigned int count = NUMTHREADS; Pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Straight threaddata {int start, stop, tid; }; Zero * Square (Zero * TD) {Straight Thread Data * Data = (Straight Threaded Data *) TD; Int start = data-> Start; Int stop = data-> Stop; Int TID = Data - & gt; Int i, s; // For the main loop (S = 0; S> <2; s ++) {for (i = start; i & lt; stop; i ++) {printf ("Thread number.% Is typing: \ N ", tid); } // synchronization point int RC = Pthread_barrier_wait (& barr); If (RC! = 0 & amp; RC! = PTHREAD_BARRIER_SERIAL_THREAD) {printf ("Could not wait on the hindrance \ n"); Exit (-1); } For (i = start; i & lt; stop; i ++) {printf ("thread number.% Is implemented: \ n", tid); }} Return tap; } Int main (zero) {pthread_t thread [NUMTHREADS]; Thread data data [NUMTHREADS]; // Barrier initialization if (pthread_barrier_init (& amp; barr, NULL, NUMTHREADS)) {printf ("could not create an obstacle \ n"); Return -1; } Int i; Int. Carpaththread = (ARRAYSIZE + NUMTHREADS-1) / NUMTHREADS; / * Divide the work for the thread, prepare the parameter / / (i = 0; i & lt; NUMTHREADS; i ++) {data [i] .start = i * tasksPerThread; Data [i] .stop = (i + 1) * tasksPerThread; Data [ii] .tid = ii; } / * The last thread should not go by the end of the array * / data [NUMTHREADS-1] .stop = ARRAYSIZE; / * For launches threads * / for (i = 0; i & lt; NUMTHREADS; i ++) {pthread_create (and thread [i], NULL, worker, and data [i]); } / * Wait for the thread to end * / for (i = 0; i
Here is the output:
Thread number. 2 is writing:
thread is writing no 2:
thread is writing no: 0 thread is not writing 0:
thread is not writing 1:
Thread is not writing 1:
Thread No.1 is implemented: Thread number 1 is implemented:
Thread is writing number 1:
Not writing thread 1:
No thread 0 is implemented: Thread number 0 is implemented: Thread number 0 is writing:
Thread is not writing 0:
Thread No 2 is implemented:
thread number2 is implemented:
thread number 2 is writing:
thread is writing no 2:
thread none is implemented:
thread number 2 is implemented:
thread number 0 is implemented: Thread number 0 is implemented: Thread number 1 is implemented: Thread number 1 is implemented:
The output code you typed is synchronized properly. Unless the 'writing' phase of all threads is finished, a thread 'execution' phase does not start.
In addition to this, you should not make any thread, after the next 'Write' phase, all the threads have been 'implemented' step, after the 'implementation' stage you will get the pthread_barrier_wait () .