Simple use examples of Pthread barrier:
The C + + code is as follows:
ThreadingLoadImages.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <pthread.h> #include <opencv2/opencv.hpp> #define THREADS 3//Barrier variablepthread_barrier_t barr;struct param{cv::mat img;int index;char* file_path;} params[THREADS];void* Entry_point (void* Arg) {param* p = (param*) arg;int index = p->index;char* File_path = p->file_path;p->img = Cv::imread (file_path);p rint F ("The%d thread is doing job! \ n ", index);//synchronization Pointint rc = pthread_barrier_wait (&barr); if (rc! = 0 && rc! = Pthread_barrier_ Serial_thread) {printf ("Could not wait on barrier\n"); exit (-1);}} int main () {pthread_t thr[threads];//pthread_mutex_init (&g_mtx, NULL);//Barrier initializationif (pthread_ Barrier_init (&barr, NULL, THREADS)) {printf ("Could not create a barrier\n"); return-1;} int t[threads];p Arams[0].file_path = "... /micky.png ";p Arams[1].file_path =". /umbrella.png ";p Arams[2].file_path =". /beard.jpg "; for (int i = 0; i < THREADS; ++i) {//t[i] = I;paRams[i].index = I;if (Pthread_create (&thr[i], NULL, &entry_point, (void *) ¶ms[i])) {printf ("Could not Create thread%d\n ", i); return-1;}} for (int i = 0; i < THREADS; ++i) {if (Pthread_join (Thr[i], NULL)) {printf ("Could not join thread%d\n", i); return-1;}} printf ("All Threads finish jobs! \ n "); for (int i=0; i<threads; i++) cv::imshow (Params[i].file_path, params[i].img); Cv::waitkey (0); return 0;}
Effect
Related information: Pthread Thread Usage Summary
Multi-threaded Local picture loading example "OpenCV" "Pthread"