多线程本地图片加载示例【OpenCV】【Pthread】
【摘要】
Pthread barrier的简单使用示例:
C++代码如下:
// ThreadingLoadImages.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#include <pthread.h>#include <opencv2/opencv.hpp> #define...
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线程使用小结
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/53216616
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)