Take the 2-minute tour ×

I have a main function to take image from somewhere, and it should be displayed within a new thread. This is what I'm doing:

int main(int argc, char** argv)
    Mat frame = imread("img.jpg");

    auto_ptr<SomeClass> thread1(new SomeClass(1, frame));
    auto_ptr<SomeClass> thread2(new SomeClass(2, frame));


    int result1 = reinterpret_cast<int>(thread1->Join());
    int result2 = reinterpret_cast<int>(thread2->Join());

    return 0;

I have found many source codes to implement a simple pthread wrapper class, which ones are working well, as long as I don't try to pass the "cv::Mat frame" image to the derived thread (through its constructor).

The pthread wrapper class:

/* ********************* THREAD.H ********************* */
extern "C++" class __declspec(dllexport) Thread 
    virtual ~Thread(void);

    void Start(void);
    void* Join(void);

    virtual void* Run(void) = 0;

    static void* StartThread(void* pVoid);

    pthread_t threadID_;
    pthread_attr_t threadAttribute_;
    void* result_;

/* ********************* THREAD.CPP ********************* */
Thread::Thread() {}

Thread::~Thread() {}

void* Thread::StartThread(void* pVoid) 
    Thread* aThread = static_cast<Thread*>(pVoid);
    aThread->result_ = aThread->Run();
    return aThread->result_;

void Thread::Start() 
    pthread_attr_setscope(&threadAttribute_, PTHREAD_SCOPE_SYSTEM);

    pthread_create(&threadID_, &threadAttribute_, Thread::StartThread, (void*)this);

void* Thread::Join() 
    pthread_join(threadID_, NULL);
    return result_;

The subclass of Thread can look like below:

/* ********************* SOMECLASS.H ********************* */
extern "C++" class __declspec(dllexport) SomeClass : public Thread
    SomeClass(int id, cv::Mat& frame);

    virtual void* Run();
    int id_;
    cv::Mat frame_;

/* ********************* SOMECLASS.CPP ********************* */
SomeClass::SomeClass(int id, cv::Mat& frame) : id_(id)
    frame_ = frame.clone();

SomeClass::~SomeClass(void) {}

void* SomeClass::Run() 
    cout << id_ << ", " << frame_.cols << ", " << frame_.rows << endl;

    imshow("frame " + id_, frame_);

    return reinterpret_cast<void*>(id_);

The execution breaks at line imshow()... Any idea that what can cause this problem or how can I solve it?

share|improve this question
do you have a compiler that supports c++11, or access to boost::thread? –  juanchopanza Apr 21 '12 at 15:03
How does it break? Does it work if you do it in a single thread? –  Alan Stokes Apr 21 '12 at 15:08
I'm using VS2010, which one doesn't support c++11, and I don't like to use boost. EDIT: It brakes with an "Access violation".. Writes the resolution (frame_.cols and frame_.rows) well, then brakes at the line imshow(). It is also working as well in a single thread. –  Kornel Apr 21 '12 at 15:19
add comment

1 Answer

The problem has been solved. If anybody has a similar trouble, then: the whole OpenCV project need to be rebuilt with Intel TBB support. (It's working well with OpenCV 2.3.1)

share|improve this answer
add comment

Your Answer


