概述
进程间管道通信,同文件共享通信几乎完全一样(利用文件实现进程间共享数据)
唯一核心不一样的地方:
int ret = mkfifo(kSharedFile.c_str(), 0600);
测试代码
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
std::string kSharedFile = "/tmp/test.fifo";
template<class T>
class Writer {
private:
std::ofstream out;
public:
Writer(std::string& name):
out(name, std::ofstream::binary) {}
void Write(const T& data) {
out.write(reinterpret_cast<const char*>(&data), sizeof(T));
out.flush();
}
};
template<class T>
class Reader {
private:
std::ifstream in;
publ