首页 > 代码库 > 多线程本地图片加载示例【OpenCV】【Pthread】
多线程本地图片加载示例【OpenCV】【Pthread】
Pthread barrier的简单使用示例:
C++代码如下:
// ThreadingLoadImages.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <pthread.h> #include <opencv2/opencv.hpp> #define THREADS 3 // Barrier variable pthread_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); printf("The %d thread is doing job! \n", index); // Synchronization point int 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 initialization if (pthread_barrier_init(&barr, NULL, THREADS)) { printf("Could not create a barrier\n"); return -1; } //int t[THREADS]; params[0].file_path = "../micky.png"; params[1].file_path = "../umbrella.png"; params[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; }
相关资料:pthread线程使用小结
多线程本地图片加载示例【OpenCV】【Pthread】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。