-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo1.cpp
More file actions
50 lines (44 loc) · 955 Bytes
/
demo1.cpp
File metadata and controls
50 lines (44 loc) · 955 Bytes
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
/*co_routine.h基础测试,
**只用到poll, co_eventloop ,co_enable_hook_sys();
**程序实现两个子协程交替运行,但程序是不会退出的
*/
#include <iostream>
#include <unistd.h>
#include <ctime>
#include <sys/select.h>
#include "co_routine.h"
using namespace std;
void* f1(void *arg)
{
co_enable_hook_sys();
for(int i=0;i<10;i++)
{
cout<<__func__<<" "<<i<<endl;
poll(NULL,0,100);
}
return NULL;
}
void* f2(void *arg)
{
co_enable_hook_sys();
for(int i=0;i<10;i++)
{
cout<<__func__<<" "<<i<<endl;
poll(NULL,0,100);
}
return NULL;
}
int main()
{
stCoCond_t* cond=co_cond_alloc();
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);
cout<<__func__<<" after "<<endl;
return 0;
}