测试程序
2022/5/11大约 3 分钟
测试程序
生产者和消费者
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include "co_routine.h"
void* Producer(void* arg);
void* Consumer(void* arg);
struct stPara_t
{
// 条件变量
stCoCond_t* cond;
// 数据池
std::vector<int> vecData;
// 数据ID
int id;
// 协程id
int cid;
};
int main()
{
stPara_t p;
p.cond = co_cond_alloc();
p.cid = p.id = 0;
srand(time(NULL));
// 协程对象(CCB),一个生产者,多个消费者
const int nConsumer = 2;
stCoRoutine_t* pProducerCo = NULL;
stCoRoutine_t* pConsumerCo[nConsumer] = { NULL };
// 创建启动生产者协程
// 看源码可知该函数必返回0
co_create(&pProducerCo, NULL, Producer, &p);
co_resume(pProducerCo);
std::cout << "start producer coroutine success" << std::endl;
// 创建启动消费者协程
for (int i = 0; i < nConsumer; i++)
{
co_create(&pConsumerCo[i], NULL, Consumer, &p);
co_resume(pConsumerCo[i]);
}
std::cout << "start consumer coroutine success" << std::endl;
// 启动循环事件
co_eventloop(co_get_epoll_ct(), NULL, NULL);
return 0;
}
void* Producer(void* arg)
{
// 启用协程HOOK项
co_enable_hook_sys();
stPara_t* p = (stPara_t*)arg;
int cid = ++p->cid;
while (true)
{
// 产生随机个数据
for (int i = rand() % 5 + 1; i > 0; i--)
{
p->vecData.push_back(++p->id);
std::cout << "[" << cid << "] + add new data:" << p->id << std::endl;
}
// 通知消费者
co_cond_signal(p->cond);
// 必须使用poll等待
poll(NULL, 0, 1000);
}
return NULL;
}
void* Consumer(void* arg)
{
// 启用协程HOOK项
co_enable_hook_sys();
stPara_t* p = (stPara_t*)arg;
int cid = ++p->cid;
while (true)
{
// 检查数据池,无数据则等待通知
if (p->vecData.empty())
{
co_cond_timedwait(p->cond, -1);
continue;
}
// 消费数据
std::cout << "[" << cid << "] - del data:" << p->vecData.front() << std::endl;
p->vecData.erase(p->vecData.begin());
}
return NULL;
}输出结果,生产者随机生成1-5个数据,让消费者去消费,等消费者执行完后生产者交替继续生成数据:
[root@iZuf61kbf845xt6tz10abgZ libco]# ./example_proandconsume
[1] + add new data:1
start producer coroutine success
[2] - del data:1
start consumer coroutine success
[1] + add new data:2
[1] + add new data:3
[1] + add new data:4
[2] - del data:2
[2] - del data:3
[2] - del data:4
[1] + add new data:5
[3] - del data:5
[1] + add new data:6
[1] + add new data:7
[2] - del data:6
[2] - del data:7
[1] + add new data:8
[3] - del data:8共享栈的使用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
#include <string.h>
#include "coctx.h"
#include "co_routine.h"
#include "co_routine_inner.h"
void* RoutineFunc(void* args)
{
co_enable_hook_sys();
int* routineid = (int*)args;
while (true)
{
char sBuff[128];
sprintf(sBuff, "from routineid %d stack addr %p\n", *routineid, sBuff);
printf("%s", sBuff);
poll(NULL, 0, 1000); //sleep 1s
}
return NULL;
}
int main()
{
stShareStack_t* share_stack= co_alloc_sharestack(1, 1024 * 128);
stCoRoutineAttr_t attr;
attr.stack_size = 0;
attr.share_stack = share_stack;
stCoRoutine_t* co[2];
int routineid[2];
for (int i = 0; i < 2; i++)
{
routineid[i] = i;
co_create(&co[i], &attr, RoutineFunc, routineid + i);
co_resume(co[i]);
}
co_eventloop(co_get_epoll_ct(), NULL, NULL);
return 0;
}输出结果,可以看出,协程的栈地址一致:
[root@iZuf61kbf845xt6tz10abgZ libco]# ./example_copystack
from routineid 0 stack addr 0x12c6c10
from routineid 1 stack addr 0x12c6c10
from routineid 0 stack addr 0x12c6c10
from routineid 1 stack addr 0x12c6c10条件变量的使用
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include "co_routine.h"
using namespace std;
struct stTask_t
{
int id;
};
struct stEnv_t
{
stCoCond_t* cond;
queue<stTask_t*> task_queue;
};
void* Producer(void* args)
{
co_enable_hook_sys();
stEnv_t* env= (stEnv_t*)args;
int id = 0;
while (true)
{
stTask_t* task = (stTask_t*)calloc(1, sizeof(stTask_t));
task->id = id++;
env->task_queue.push(task);
printf("%s:%d produce task %d\n", __func__, __LINE__, task->id);
co_cond_signal(env->cond);
poll(NULL, 0, 1000);
}
return NULL;
}
void* Consumer(void* args)
{
co_enable_hook_sys();
stEnv_t* env = (stEnv_t*)args;
while (true)
{
if (env->task_queue.empty())
{
co_cond_timedwait(env->cond, -1);
continue;
}
stTask_t* task = env->task_queue.front();
env->task_queue.pop();
printf("%s:%d consume task %d\n", __func__, __LINE__, task->id);
free(task);
}
return NULL;
}
int main()
{
stEnv_t* env = new stEnv_t;
env->cond = co_cond_alloc();
stCoRoutine_t* consumer_routine;
co_create(&consumer_routine, NULL, Consumer, env);
co_resume(consumer_routine);
stCoRoutine_t* producer_routine;
co_create(&producer_routine, NULL, Producer, env);
co_resume(producer_routine);
co_eventloop(co_get_epoll_ct(), NULL, NULL);
return 0;
}输出结果,一个生产者和消费者交替执行:
[root@iZuf61kbf845xt6tz10abgZ libco]# ./example_cond
Producer:44 produce task 0
Consumer:63 consume task 0
Producer:44 produce task 1
Consumer:63 consume task 1