-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.cpp
55 lines (41 loc) · 1.14 KB
/
test.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
#include <algorithm>
#include <string>
#include <gtest/gtest.h>
#include <fstream>
#include <chrono>
#include <vector>
#include <atomic>
#include <thread>
#include "lfstack.h"
lfstack_t results;
void *worker(void *arg)
{
int i = 10000;
long long *int_data;
while (i--) {
int_data = (long long*) malloc(sizeof(long long));
assert(int_data != NULL);
*int_data = i;
while (lfstack_push(&results, int_data) == -1) ;
while ( (int_data =(long long*) lfstack_pop(&results)) == NULL);
free(int_data);
}
return NULL;
}
TEST(lfstackTest, CROSS_THREAD_MAIN_CALL) {
int nthreads = sysconf(_SC_NPROCESSORS_ONLN); // Linux
int i;
lfstack_init(&results);
/* Spawn threads. */
pthread_t threads[nthreads];
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
for (i = 0; i < nthreads; i++)
pthread_create(threads + i, NULL, worker, NULL);
for (i = 0; i < nthreads; i++)
pthread_join(threads[i], NULL);
lfstack_destroy(&results);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}