-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopen_simplified_test.cc
More file actions
41 lines (36 loc) · 863 Bytes
/
popen_simplified_test.cc
File metadata and controls
41 lines (36 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "popen_simplified.h"
#include <thread>
#include <cassert>
#ifndef POPEN_SIMPLIFIED
# define POPEN popen
# define PCLOSE pclose
#else
# define POPEN popen_simplified
# define PCLOSE pclose_simplified
#endif
void write_data(const char* data)
{
FILE* output = POPEN("wc", "w");
fprintf(output, "%s", data);
int exit_status = PCLOSE(output);
assert(exit_status == 0);
}
void read_data(char* data)
{
FILE* input = POPEN("echo foobar", "r");
fscanf(input, "%s", data);
int exit_status = PCLOSE(input);
assert(exit_status == 0);
}
int main()
{
char buf[10]{};
std::thread t1(write_data, "one two three");
std::thread t2(read_data, buf);
t1.join();
t2.join();
fprintf(stdout, "all threads finished\n");
fprintf(stdout, "data read:\n");
fprintf(stdout, "%s\n", buf);
return 0;
}