Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // lab1
- //
- // Created by Alik on 10/21/18.
- // Copyright © 2018 Oleksandr Vovkotrub. All rights reserved.
- //
- #include <fstream>
- #include <iostream>
- #include <future>
- #include <string>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #define let auto
- using namespace std;
- fstream create_pipe(string name) {
- mkfifo(name.c_str(), 666);
- return fstream { name };
- }
- future<int> read_pipe(fstream &pipe) {
- return async(launch::async, [&] {
- int result;
- pipe >> result;
- return result;
- });
- }
- int a(fstream &pipe) {
- sleep(3);
- pipe << 2;
- return 0;
- }
- int b(fstream &pipe) {
- sleep(3);
- pipe << 1;
- return 0;
- }
- int main() {
- let first_pipe = create_pipe("first_pipe");
- let second_pipe = create_pipe("second_pipe");
- let a_future = async(launch::async, a, ref(first_pipe));
- let b_future = async(launch::async, b, ref(second_pipe));
- let first_pipe_result = read_pipe(first_pipe);
- let second_pipe_result = read_pipe(second_pipe);
- cout << first_pipe_result.get() + second_pipe_result.get() << endl;
- }
Add Comment
Please, Sign In to add comment