-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_lock_profile.cpp
70 lines (54 loc) · 1.45 KB
/
single_lock_profile.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
62
63
64
65
66
67
68
69
#include "single_lock_counter.hpp"
#include "ticks_clock.hpp"
#include <tr1/functional>
#include <pthread.h>
#include <stdlib.h>
namespace{
using std::tr1::bind;
using base::Single_Lock_Counter;
using base::TicksClock;
const int REPEAT_TIME = 1000;
const int MEGA_REPEAT_TIME = 1000000;
static double getTotal(TicksClock::Ticks start,
TicksClock::Ticks end) {
double duration = end - start;
double time = duration / TicksClock::ticksPerSecond();
return time;
}
Single_Lock_Counter *counter;
int thread_num;
pthread_t *threads;
void *test(void *thread_id){
//long tid = (long) thread_id;
TicksClock::Ticks start = TicksClock::getTicks();
for(int i = 0 ; i < MEGA_REPEAT_TIME; i++){
counter->getAndIncrement();
}
TicksClock::Ticks end = TicksClock::getTicks();
double duration = getTotal(start,end)*1e9/MEGA_REPEAT_TIME;
printf("average:%f\n",duration);
pthread_exit((void*) thread_id);
}
} // end of namespace
int main(int argc, char *argv[]){
if(argc < 2){
thread_num = 8;
}
else thread_num = atoi(argv[1]);
counter = new Single_Lock_Counter();
threads = new pthread_t[thread_num];
int rc;
for(int i = 0 ; i < thread_num; i++){
rc = pthread_create(&threads[i],NULL, test, (void *)i);
if (rc) {
printf("ERROR; ");
exit(-1);
}
}
for(int i = 0 ; i < thread_num; i++){
pthread_join(threads[i], NULL);
}
printf("%d\n", counter->getResult());
delete []threads;
delete counter;
}