Guest User

Untitled

a guest
Oct 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // lab1
  4. //
  5. // Created by Alik on 10/21/18.
  6. // Copyright © 2018 Oleksandr Vovkotrub. All rights reserved.
  7. //
  8.  
  9. #include <fstream>
  10. #include <iostream>
  11. #include <future>
  12. #include <string>
  13.  
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16.  
  17. #include <unistd.h>
  18.  
  19. #define let auto
  20.  
  21. using namespace std;
  22.  
  23. fstream create_pipe(string name) {
  24. mkfifo(name.c_str(), 666);
  25. return fstream { name };
  26. }
  27.  
  28. future<int> read_pipe(fstream &pipe) {
  29. return async(launch::async, [&] {
  30. int result;
  31. pipe >> result;
  32. return result;
  33. });
  34. }
  35.  
  36. int a(fstream &pipe) {
  37. sleep(3);
  38. pipe << 2;
  39. return 0;
  40. }
  41.  
  42. int b(fstream &pipe) {
  43. sleep(3);
  44. pipe << 1;
  45. return 0;
  46. }
  47.  
  48. int main() {
  49. let first_pipe = create_pipe("first_pipe");
  50. let second_pipe = create_pipe("second_pipe");
  51.  
  52. let a_future = async(launch::async, a, ref(first_pipe));
  53. let b_future = async(launch::async, b, ref(second_pipe));
  54.  
  55. let first_pipe_result = read_pipe(first_pipe);
  56. let second_pipe_result = read_pipe(second_pipe);
  57.  
  58. cout << first_pipe_result.get() + second_pipe_result.get() << endl;
  59. }
Add Comment
Please, Sign In to add comment