1

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.

kernelman
  • 992
  • 1
  • 13
  • 28
  • I am not using C++11 **thread**. Instead i am using **POSIX thread** (pthread), which requires **pthread.h** – kernelman Feb 06 '16 at 05:02
  • namedWindow, waitKey should go out of your threads, you're interfering with desktop/gui here. – berak Feb 06 '16 at 06:02
  • @berak , i just moved the namedWindow and waitKey to main(), and it worked !!! Thanks a ton !! you are a savior. Could you please copy your comment as answer, i will mark it answered. – kernelman Feb 06 '16 at 07:50
  • @berak, just wondering, if namedWindow interferes with desktop/gui, why does the same code works on windows ? Does Windows gui has better thread management ? – kernelman Feb 06 '16 at 09:22
  • 1
    it definitely depends on the gui toolkit used under the hood. can't say, what's better or worse there, sorry. – berak Feb 06 '16 at 09:33
  • @berak, although its a totally different ques, but since the context is already here i'll ask anyways. The above program doesn't terminate. After the _"Thread i exiting with status 0"_ output for both the threads, the program halts. I have to Ctrl-C to terminate it. What is the right way to end such prog ? – kernelman Feb 06 '16 at 10:23

2 Answers2

2

namedWindow, waitKey should go out of your threads, you're interfering with desktop/gui here

berak
  • 39,159
  • 9
  • 91
  • 89
0

I know this is an old question, but I had fun solving this bug. I wanted to write to the GUI from multi threads. A solution is to write to a queue from multi threads, and then process this queue from the main thread. This makes it so much easier.

Of course use a lock when you are writing to queue etc.

nnrales
  • 1,481
  • 1
  • 17
  • 26