File: concurrent_read.cc

package info (click to toggle)
mp3fs 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 888 kB
  • sloc: cpp: 1,884; ansic: 564; sh: 298; makefile: 82
file content (37 lines) | stat: -rw-r--r-- 740 bytes parent folder | download | duplicates (2)
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
#include <fcntl.h>
#include <unistd.h>

#include <cstdlib>
#include <thread>
#include <vector>

int fd;

void read_from_offset(int off) {
    const int buffer_size = 10000;
    char buffer[buffer_size];
    if (pread(fd, buffer, sizeof(buffer), off) == -1) {
        exit(1);
    }
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        return 2;
    }
    fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        return 1;
    }
    std::vector<std::thread> threads;
    int count = 4;
    const int interval = 30000;
    for (int off = 0; off < count * interval; off += interval) {
        threads.emplace_back(read_from_offset, off);
    }
    for (auto& t : threads) {
        t.join();
    }
    close(fd);
    return 0;
}