3 * Author: Lukas Krejci <krejci.l@centrum.cz>, (C) 2008
4 * Copyright: See COPYING file that comes with this distribution
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>
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;
22 for(size_t i
= 0; i
< nof_loops
; i
++) {
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;
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
) {
35 test_free_list
.erase(it
);
43 for(size_t i
= 0; i
< nof_loops
; i
++) {
44 boost::mutex::scoped_lock
lk(std_list_guard
);
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;
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()) {
57 test_std_list
.erase(it
);
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
);
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...
91 beacon::lock_free_list
<dt
> l
;
93 beacon::lock_free_list
<dt
>::iterator
it(l
.begin());
101 l
.erase(++l
.begin());
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
);