C++多线程,创建线程

线程函数在类里面

1,创建线程 std ::thread

int main() {
 // create a thread to read image by videocapture
    ReadFifo fifo_1(fifo_queue, FIFO_PATH_1, pipeline_run_flag);
    std::thread fifothread_1(&ReadFifo::read_fifo, &fifo_1);

    ReadFifo fifo_2(fifo_queue, FIFO_PATH_2, pipeline_run_flag);
    std::thread fifothread_2(&ReadFifo::read_fifo, &fifo_2);

	...
	...
	...

	fifothread_1.join();
    fifothread_2.join();
}

2,类

class ReadFifo {
    private:
        TQueueConcurrent<std::vector<std::string>> &fifo_queue;
        bool &pipeline_run_flag;
        std::string fifo_path;
        int fifo_fd;
        
    public:
        ReadFifo(TQueueConcurrent<std::vector<std::string>> &fifo_queue, 
                std::string fifo_path,bool &pipeline_run_flag);
        ~ReadFifo();
        void read_fifo();
};

3,类函数定义

ReadFifo::ReadFifo(TQueueConcurrent<std::vector<std::string>> &fifo_queue, 
        std::string fifo_path,bool &pipeline_run_flag)
    :
        fifo_queue(fifo_queue), fifo_path(fifo_path), 
        pipeline_run_flag(pipeline_run_flag)
{
    // open fifo
    if((fifo_fd = open(fifo_path.c_str(), O_RDONLY)) < 0) {
        perror("counter open read fifo");
        pipeline_run_flag = false;
    }
}

ReadFifo::~ReadFifo() {}

void ReadFifo::read_fifo() {
    char buf[FIFO_SIZE];
    char *p;
    std::vector<std::string> string_ret;
    
    while(pipeline_run_flag) {
	    string_ret.clear();
        read(fifo_fd, buf, FIFO_SIZE);
        p = buf;
        char *q = p;
        while(*p) {
            if(*p == ',') {
                *p = '\0';
                char* schar = new char[strlen(q)+1];
                strcpy(schar, q);
                std::string s = schar;
                string_ret.emplace_back(s);
                q = p+1;
            }
            p++;
        }
        std::string s = q;
        string_ret.emplace_back(s);    
        fifo_queue.emplace_back(string_ret);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值