Multi-threaded Local picture loading example "OpenCV" "Pthread"

Source: Internet
Author: User

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 *) &params[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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.