undefing the numerous magical defs in the preprocessor iteration
[beacon-ss.git] / lock_free_list_test.cpp
blobf065c041542fd29635a90ad4dd3288fb20d1f41b
1 /**
2 * beacon
3 * Author: Lukas Krejci <krejci.l@centrum.cz>, (C) 2008
4 * Copyright: See COPYING file that comes with this distribution
5 */
7 #include <beacon/experiments/lock_free_list.hpp>
8 #include <boost/thread/xtime.hpp>
9 #include <boost/thread.hpp>
10 #include <boost/thread/mutex.hpp>
11 #include <list>
12 #include <iostream>
13 #include <cstdlib>
15 beacon::lock_free_list<int> test_free_list;
16 std::list<int> test_std_list;
17 boost::mutex std_list_guard;
19 size_t nof_loops = 10;
21 void lf_pay_load() {
22 for(size_t i = 0; i < nof_loops; i++) {
23 switch (rand() % 4) {
24 case 0: test_free_list.push_front(0); break;
25 case 1: test_free_list.push_back(1); break;
26 case 2: test_free_list.erase(test_free_list.begin()); break;
27 case 3:
28 if (test_free_list.size() > 0) {
29 int x = rand() % test_free_list.size();
30 beacon::lock_free_list<int>::iterator it = test_free_list.begin();
31 beacon::lock_free_list<int>::iterator end = test_free_list.end();
32 while (x-- > 0 && it != end) {
33 ++it;
35 test_free_list.erase(it);
37 break;
42 void std_pay_load() {
43 for(size_t i = 0; i < nof_loops; i++) {
44 boost::mutex::scoped_lock lk(std_list_guard);
46 switch (rand() % 4) {
47 case 0: test_std_list.push_front(0); break;
48 case 1: test_std_list.push_back(1); break;
49 case 2: if (test_std_list.size() > 0) test_std_list.erase(test_std_list.begin()); break;
50 case 3:
51 if (test_std_list.size() > 0) {
52 int x = rand() % test_std_list.size();
53 std::list<int>::iterator it = test_std_list.begin();
54 while (x-- > 0 && it != test_std_list.end()) {
55 ++it;
57 test_std_list.erase(it);
59 break;
64 void run_test(char const * name, size_t const nof_threads, void (* pay_load)()) {
65 boost::xtime start, end;
67 boost::thread_group tg;
69 boost::xtime_get(&start, boost::TIME_UTC);
71 for(size_t i = 1; i < nof_threads; ++i) {
72 tg.create_thread(pay_load);
75 pay_load();
77 tg.join_all();
79 boost::xtime_get(&end, boost::TIME_UTC);
81 boost::xtime_get(&end, boost::TIME_UTC);
82 double diff = end.sec - start.sec + (end.nsec - start.nsec) / 1e9;
83 std::cout << name << " (" << nof_threads << " threads) time: " << diff << std::endl;
86 int main(int argc, char * argv[]) {
88 //TODO create thorough speed and functionality tests...
89 typedef int dt;
90 dt v(1);
91 beacon::lock_free_list<dt> l;
93 beacon::lock_free_list<dt>::iterator it(l.begin());
95 l.push_front(v);
97 l.push_back(dt(2));
99 debug_print_list(l);
101 l.erase(++l.begin());
103 debug_print_list(l);
105 l.push_back(dt(3));
107 debug_print_list(l);
109 nof_loops = 10000;
111 for(size_t n = 1; n < 20; n++) {
112 run_test("std", n, std_pay_load);
113 run_test("lf ", n, lf_pay_load);
116 return 0;