线程函数在类里面
1,创建线程 std ::thread
int main() {
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)
{
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);
}
}