-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
60 lines (56 loc) · 1.49 KB
/
main.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include"LockfreeSPSCBuffer.h"
#include<iostream>
#include<string>
#include<thread>
#include<fstream>
#include<vector>
typedef LockfreeSPSCBuffer<char, 4096, BufferAllocUsingNew> LFBUFFER;
void producer(std::string infilestr, LFBUFFER& sb) {
char* wptr;
std::pair<char*, int>wrinfo;
std::ifstream filein;
filein.open(infilestr.c_str(), std::ios::binary);
if (!filein.is_open()) {
std::cout << "error in opening input file" << "\n";
return;
}
while (!filein.eof()) {
if (sb.AquireWritePtr(wrinfo)) {
filein.read(wptr, 1024);
sb.ReleaseWritePtr(filein.gcount());
// std::cout << "count = " << filein.gcount() << "\n";
}
}
sb.SetEOS();
filein.close();
}
void consumer(std::string ofilestr, LFBUFFER& sb) {
std::pair<char*, int> rinfo;
std::ofstream fout;
fout.open(ofilestr.c_str(), std::ios::binary);
if (!fout.is_open()) {
std::cout << "error in opening output file" << "\n";
return;
}
auto writesize = 0;
while (!sb.GetEOS()) {
if (sb.AquireReadPtr(rinfo)) {
fout.write(rinfo.first,rinfo.second);
sb.ReleaseReadPtr(rinfo.second);
}
}
fout.close();
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cout << "Usage " << argv[0] << " <filename>" << "\n";
return 0;
}
LFBUFFER sharedbuf(1024 * 1024);
std::string outfile(argv[1]);
outfile = outfile + "copy";
std::thread t1(producer,argv[1], std::ref(sharedbuf));
std::thread t2(consumer,outfile, std::ref(sharedbuf));
t1.join();
t2.join();
}