13
|
1 /*
|
|
2
|
152
|
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
|
|
4 the GPL version 3 or any later version at your choice available at
|
|
5 http://www.gnu.org/licenses/gpl-3.0.txt
|
13
|
6
|
|
7 */
|
|
8
|
|
9 #include <stdio.h>
|
|
10 #include <unistd.h>
|
|
11 #include <pthread.h>
|
|
12 #include <set>
|
|
13
|
|
14 using namespace std;
|
|
15
|
|
16 struct ltstr {
|
143
|
17 bool operator()(char* s1, char* s2) const {
|
|
18 return strcmp(s1, s2) < 0;
|
|
19 }
|
13
|
20 };
|
|
21
|
143
|
22 typedef set<char *, ltstr> string_set;
|
13
|
23
|
143
|
24 static string_set all_strings; // owns all the strings, only modified by the config loader thread
|
13
|
25
|
|
26 struct stats {
|
143
|
27 bool stop;
|
|
28 bool running;
|
|
29 int counter;
|
|
30 int errors;
|
|
31 stats();
|
13
|
32 };
|
|
33 stats::stats() {
|
143
|
34 stop = false;
|
|
35 running = false;
|
|
36 counter = 0;
|
|
37 errors = 0;
|
13
|
38 }
|
|
39
|
|
40 ////////////////////////////////////////////////
|
|
41 // helper to discard the strings held by a string_set
|
|
42 //
|
|
43 static void discard(string_set &s);
|
|
44 static void discard(string_set &s) {
|
143
|
45 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
46 free(*i);
|
|
47 }
|
|
48 s.clear();
|
13
|
49 }
|
|
50
|
|
51 ////////////////////////////////////////////////
|
|
52 // helper to register a string in a string set
|
|
53 //
|
|
54 static char* register_string(string_set &s, char *name);
|
|
55 static char* register_string(string_set &s, char *name) {
|
143
|
56 string_set::iterator i = s.find(name);
|
|
57 if (i != s.end()) return *i;
|
|
58 char *x = strdup(name);
|
|
59 s.insert(x);
|
|
60 return x;
|
13
|
61 }
|
|
62
|
|
63
|
|
64 ////////////////////////////////////////////////
|
|
65 // thread tester
|
|
66 //
|
|
67 static void* tester(void *arg);
|
|
68 static void* tester(void *arg) {
|
143
|
69 stats &st = *((stats *)arg);
|
|
70 st.running = true;
|
|
71 while (!st.stop) {
|
|
72 const int LIMIT = 1000;
|
|
73 string_set *mine = new string_set;
|
|
74 string_set &me = *mine;
|
|
75 for (int i=0; i<LIMIT; i++) {
|
|
76 char buf[100];
|
|
77 snprintf(buf, sizeof(buf), "this is string %d", i);
|
|
78 register_string(me, buf);
|
|
79 }
|
|
80 for (int i=0; i<LIMIT; i+=5) {
|
|
81 char buf[100];
|
|
82 snprintf(buf, sizeof(buf), "this is string %d", i);
|
|
83 string_set::iterator j = me.find(buf);
|
|
84 if (j == me.end()) st.errors++;
|
|
85 }
|
|
86 discard(me);
|
|
87 delete mine;
|
|
88 st.counter++;
|
|
89 }
|
|
90 st.running = false;
|
|
91 return NULL;
|
13
|
92 }
|
|
93
|
|
94 int main(int argc, char**argv)
|
|
95 {
|
143
|
96 stats st1;
|
|
97 stats st2;
|
|
98 pthread_t tid;
|
|
99 if (pthread_create(&tid, 0, tester, &st1))
|
|
100 fprintf(stdout, "failed to create test thread");
|
|
101 if (pthread_detach(tid))
|
|
102 fprintf(stdout, "failed to detach test thread");
|
|
103 if (pthread_create(&tid, 0, tester, &st2))
|
|
104 fprintf(stdout, "failed to create test thread");
|
|
105 if (pthread_detach(tid))
|
|
106 fprintf(stdout, "failed to detach test thread");
|
13
|
107
|
143
|
108 fprintf(stdout, "tests are running\n");
|
|
109 sleep(60);
|
|
110 st1.stop = true;
|
|
111 st2.stop = true;
|
|
112 while (st1.running || st2.running) {
|
|
113 sleep(1);
|
|
114 }
|
13
|
115
|
143
|
116 fprintf(stdout, "counter 1 = %d\n", st1.counter);
|
|
117 fprintf(stdout, "counter 2 = %d\n", st2.counter);
|
|
118 fprintf(stdout, "errors 1 = %d\n", st1.errors);
|
|
119 fprintf(stdout, "errors 2 = %d\n", st2.errors);
|
|
120 return 0;
|
13
|
121 }
|
|
122
|