I am trying to use pthread in my OpenCV Project. Intially I am simply trying to open two different images using two different threads. On Windows7 + VS2010 + pthreads-win32 lib, the program runs well.
But on my Debian jessei machine (Opencv 2.4.1), the same code, although compiles well, but its execution crashes with the following error.
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
pthreadTest: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted
Interestingly when only 1 thread is created [for (i=0; i<1; i++)], it runs fine, and the image is displayed.
I have already spent 1.5 days trying to solve it, but no luck. Does anyone know, what am i doing wrong ?
Here is the code:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <cstdio>
#include <iostream>
#include <pthread.h>
using namespace cv;
using namespace std;
struct thread_data {
bool isBig;
string fileName;
};
void *processImg(void *args)
{
struct thread_data *data = (struct thread_data *) args;
const char * inputImgWinName = data->isBig ? "Big Img" : "Small Img";
cv::Mat imgInput = imread(data->fileName, 1);
cv::namedWindow(inputImgWinName, cv::WINDOW_AUTOSIZE);
cv::imshow(inputImgWinName, imgInput);
cv::waitKey();
pthread_exit(NULL);
//return NULL;
}
int main( int argc, char** argv )
{
struct thread_data data[2];
data[0].isBig = true;
data[0].fileName = "img1.png";
data[1].isBig = false;
data[1].fileName = "img2.png";
pthread_t threads[2];
pthread_attr_t attr;
void *status;
// Initialize and set thread joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Create Threads
int rc;
for (int i=0; i<2; i++) {
rc = pthread_create(&threads[i], &attr, processImg, (void *)&(data[i]));
if (rc) {
cout << "Error: Unable to create thread";
return -1;
}
}
// free attribute and wait for the other threads
pthread_attr_destroy(&attr);
for (int i=0; i<2; i++) {
rc = pthread_join(threads[i], &status);
if (rc){
cout << "Error:unable to join," << rc << endl;
exit(-1);
}
cout << "Thread: "<< i <<" exiting with status: " << status << endl;
}
pthread_exit(NULL);
return 0;
}
PS: I cannot using C++11 thread.h for some reason.
namedWindow, waitKey should go out of your threads, you're interfering with desktop/gui here