-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
78 lines (66 loc) · 1.64 KB
/
test.cpp
File metadata and controls
78 lines (66 loc) · 1.64 KB
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
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <unistd.h>
#include <ctime>
#include <sys/select.h>
#include "co_routine.h"
using namespace std;
void* f1(void *arg)
{
stCoCond_t* cond=(stCoCond_t*)arg;
for(int s=0;s<5;s++)
{
cout<<__func__<<endl;
co_enable_hook_sys();
for(uint64_t i =0;i<10000;i++)
for(uint64_t j=0;j<10000;j++)
int k=i*j+j-i;
cout<<__func__<<" for end "<<endl;
timeval delay;
delay.tv_sec = 0;
delay.tv_usec = 20 * 1000; // 20 ms
select(0, NULL, NULL, NULL, &delay);
co_cond_signal(cond);
poll(NULL,0,1);
}
co_yield_ct();
return NULL;
}
void* f2(void * arg)
{
stCoCond_t* cond=(stCoCond_t*)arg;
for(int s=0;s<5;s++)
{
cout<<__func__<<endl;
co_enable_hook_sys();
for(uint64_t i =0;i<10000;i++)
for(uint64_t j=0;j<10000;j++)
int k=i*j+j-i;
cout<<__func__<<" for end "<<endl;
timeval delay;
delay.tv_sec = 0;
delay.tv_usec = 20 * 1000; // 20 ms
select(0, NULL, NULL, NULL, &delay);
cout<<__func__<<" delay end"<<endl;
co_cond_signal(cond);
poll(NULL,0,100);
}
co_yield_ct();
return NULL;
}
int main()
{
stCoCond_t* cond=co_cond_alloc();
double start_time=clock();
stCoRoutine_t* f1_routine;
co_create(&f1_routine,NULL,f1,cond);
co_resume(f1_routine);
stCoRoutine_t* f2_routine;
co_create(&f2_routine,NULL,f2,cond);
co_resume(f2_routine);
cout<<__func__<<endl;
co_eventloop(co_get_epoll_ct(),NULL,NULL);
double end_time=clock();
double all_time=(end_time-start_time)/1000.0;
cout<<"执行时间:"<<all_time<<"ms"<<endl;
return 0;
}