Ask Your Question
0

how to enable multi-thread in CMake (centos) ?

asked 2018-06-18 12:26:01 -0600

azdoud.y gravatar image

updated 2018-06-18 12:26:31 -0600

hi,

I've issue with multi thread and CMAKE

I've written this code using multi-thread it works fine in windows, however, when I tried to run it in CentOS using CMAKE it works in a sequential manner which is weird

project structure :

image description

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
                project( main )
                find_package( OpenCV REQUIRED )
                add_executable( main main.cpp )
                target_link_libraries( main ${OpenCV_LIBS} )

code

#include <iostream> // for standard I/O
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O

// uncomment this to run it in Centos
//#include "opencv2/imgproc/imgproc_c.h"
//#include <opencv2/highgui/highgui_c.h>
//#include "opencv2/videoio/videoio_c.h"

#include <thread>
#include <future>

using namespace std;
using namespace cv;

int loadVideo( VideoCapture cap1, int seek_step,int thrd){
    int frameNum = 0;
    Mat frame;
    char c;
    cap1.set(CV_CAP_PROP_POS_FRAMES, seek_step );
    while (1){
        cap1 >> frame;
        if (frame.empty())
        {
            cout << " < < <  Channel stream ended!  > > > ";
            break;
        }
    cout<<"\nseek_step" <<seek_step << "frameNum" << frameNum << endl;
    ++frameNum;
    }
    return frameNum;
}

int main()
{
    string sourceVideoPath;
    sourceVideoPath = "C:\\opencvVid\\case1_320_240\\case320_240fps10.mp4";
//    sourceVideoPath = "./media/case320_240fps10.mp4";
    VideoCapture cap1(sourceVideoPath);
    VideoCapture cap2(sourceVideoPath);
    VideoCapture cap3(sourceVideoPath);

    cout << "Stream frame numbre : " << cap1.get(CV_CAP_PROP_FRAME_COUNT)<<endl;
    if (!cap1.isOpened())
    {
        cout  << "Could not open reference " << sourceVideoPath << endl;
        return -1;
    }

    auto future1 = std::async(loadVideo, cap1,0,1);
    auto future2 = std::async(loadVideo, cap2,750,2);
    auto future3 = std::async(loadVideo, cap3,1000,3);

    cout << future1.get() << endl;
    cout << future2.get() << endl;
    cout << future3.get() << endl;

    return 0;
}

output in windows :

Stream frame numbre : 36206

seek_step0frameNum0

seek_step0frameNum
seek_step1
750frameNum
seek_step0
0
seek_stepframeNum750frameNum2
1

seek_step1000frameNum
seek_step
seek_step750frameNum2
0
0frameNum3

seek_step
seek_step
seek_step01000750frameNum3
frameNum1
frameNum
seek_step7504
seek_step1000frameNum2
frameNum4


seek_step
seek_step
seek_step7501000frameNum3
frameNum5
0
seek_step1000frameNum4

seek_step750frameNum6frameNum5

output in CentOS:

[azdoud@video-processor proj1]$ cmake -DCMAKE_CXX_FLAGS="-std=c++11"
[azdoud@video-processor proj1]$ make
[azdoud@video-processor proj1]$ ./main
Stream frame numbre : 36206
seek_step0frameNum0
seek_step0frameNum1
seek_step0frameNum2
seek_step0frameNum3
seek_step0frameNum4
seek_step0frameNum5
seek_step0frameNum6
seek_step0frameNum7
seek_step0frameNum8
seek_step0frameNum9
seek_step0frameNum10
seek_step0frameNum11
seek_step0frameNum12
seek_step0frameNum13
seek_step0frameNum14
seek_step0frameNum15
seek_step0frameNum16
seek_step0frameNum17
seek_step0frameNum18
seek_step0frameNum19
seek_step0frameNum20
seek_step0frameNum21
seek_step0frameNum22
seek_step0frameNum23
seek_step0frameNum24
seek_step0frameNum25
seek_step0frameNum26
seek_step0frameNum27
seek_step0frameNum28
seek_step0frameNum29
seek_step0frameNum30
seek_step0frameNum31
seek_step0frameNum32
seek_step0frameNum33
seek_step0frameNum34
seek_step0frameNum35
seek_step0frameNum36
seek_step0frameNum37
seek_step0frameNum38
seek_step0frameNum39
seek_step0frameNum40
seek_step0frameNum41
seek_step0frameNum42
seek_step0frameNum43
seek_step0frameNum44
seek_step0frameNum45
seek_step0frameNum46
seek_step0frameNum47
seek_step0frameNum48
edit retag flag offensive close merge delete

Comments

Is there no explanation ?

azdoud.y gravatar imageazdoud.y ( 2018-06-19 04:38:48 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2018-06-21 04:15:44 -0600

azdoud.y gravatar image

updated 2018-06-21 10:46:50 -0600

I solved this issue by upgrading my GCC compiler on centOS, Initially, I had GCC version 4.8.0 and when I upgraded it to GCC 7.3.0. this problem of sequential running had gone.

upgrading GCC 7.3 on CentOS 7

1 - update installed packages

sudo yum -y update

2 - Install GCC from the official CentOS repositories

sudo yum -y install gcc

3 - also install gcc-c++

sudo yum -y install gcc-c++

4 - install gcc from source, in fact, it is recommended to start a screen session before starting.

screen -U -S gcc

5 - get the tarball of the GCC

wget http://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-7.3.0/gcc-7.3.0.tar.gz

6 - Unpack the archive and get in the current working directory

sudo tar zxf gcc-7.3.0.tar.gz
cd gcc-7.3.0

7 - setup bzip2 and run the ‘download_prerequisites’ script. You have to run this from the top level of the GCC source tree.

sudo yum -y install bzip2
./contrib/download_prerequisites

8 - Once you get downloaded prerequisites, run this command to start configuring the GCC build environment

./configure --disable-multilib --enable-languages=c,c++

9 - Once it finishes, you've to compile the source code. It may take a few hours to complete, so be patient.

sudo make -j 4
sudo make install

I also added in my CMakeLists.txt file this :

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-18 12:26:01 -0600

Seen: 3,951 times

Last updated: Jun 21 '18