-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite3.cpp
61 lines (53 loc) · 1.21 KB
/
write3.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
61
#include <unistd.h>
#include <atomic>
#include <algorithm>
#include <thread>
#include <vector>
#include <string>
#include "util.h"
#include "write.h"
#include "tests/tests.h"
#define THREADS 50
static lava_writer& writer = lava_writer::instance();
static std::atomic_int used[THREADS];
static void thread_test_stress()
{
set_thread_name("worker");
if (random() % 5 == 1) usleep(random() % 3 * 10000); // introduce some pseudo-random timings
lava_file_writer& file = writer.file_writer();
int tid = file.thread_index();
assert(tid < THREADS);
assert(used[tid] == 0);
used[tid] = 1;
file.write_uint32_t(42);
if (tid % 2 == 1) usleep(tid * 10000); // introduce some pseudo-random timings
file.write_uint32_t(84);
file.write_string("supercalifragilisticexpialidocious");
file.write_uint64_t(3);
file.finalize();
}
static void thread_test()
{
std::vector<std::thread*> threads(THREADS);
for (auto& t : threads)
{
t = new std::thread(thread_test_stress);
}
for (std::thread* t : threads)
{
t->join();
delete t;
}
threads.clear();
}
int main()
{
for (int i = 0; i < THREADS; i++) used[i] = 0;
writer.set("write_3", 1);
thread_test();
for (int i = 0; i < THREADS; i++)
{
assert(used[i] == 1);
}
return 0;
}