Skip to content

Tutorial

木头云 edited this page Jan 2, 2019 · 15 revisions

ipc::circ::queue

struct msg_t {
    int pid_;
    int dat_;
};

using queue_t = ipc::circ::queue<msg_t>;
auto msg_buff = new queue_t::array_t;

// thread producer
std::thread {
    [] {
        queue_t queue { msg_buff };
        // waiting for connection
        while (queue.conn_count() != 1) {
            std::this_thread::yield();
        }
        // sending datas
        for (int i = 0; i < 10; ++i) {
            queue.push(msg_t { 0, i });
        }
        // quit
        queue.push(msg_t { -1, 0 });
    }
}.detach();

// thread consumer
std::thread {
    [] {
        queue_t queue { msg_buff };
        queue.connect();
        while(1) {
            auto msg = queue.pop();
            if (msg.pid_ < 0) break;
            std::printf("msg[%d]: %d\n", msg.pid_, msg.dat_);
        }
        queue.disconnect();
    }
}.join();

ipc::route

std::vector<char const *> const datas = {
    "hello!",
    "foo",
    "bar",
    "ISO/IEC",
    "14882:2011",
    "ISO/IEC 14882:2017 Information technology - Programming languages - C++",
    "ISO/IEC 14882:2020",
    "Modern C++ Design: Generic Programming and Design Patterns Applied"
};

// thread producer
std::thread t1 {[&] {
    ipc::route cc { "my-ipc-route" };
    // waiting for connection
    while (cc.recv_count() == 0) {
        std::this_thread::yield();
    }
    // sending datas
    for (std::size_t i = 0; i < datas.size(); ++i) {
        cc.send(datas[i]);
    }
    // quit
    cc.send(ipc::buff_t('\0'));
}};

// thread consumer
std::thread t2 {[&] {
    ipc::route cc { "my-ipc-route" };
    while (1) {
        ipc::buff_t dd = cc.recv();
        auto str = static_cast<char*>(dd.data());
        if (str == nullptr || str[0] == '\0') return;
        std::printf("recv: %s\n", str);
    }
}};

t1.join();
t2.join();

Home
Tutorial

namespaces

classes

head files

Clone this wiki locally