有2个问题。1. 我可以找出当前处于活动状态的上下文吗?
2. 我能否以某种方式传入一个 ucontext,从一个函数到另一个函数作为参数。我想做这样的事情:
//Instead of this
#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <queue>
#define MEM 64000
#define MEMS 16000
#define MEL 32000
using namespace std;
ucontext_t N1,N2, Main;
void fn1()
{
for(int i=0;i<=3;i++){
cout<<i<<ndl;
swapcontext(&N1,&N2);
}
}
void fn2()
{
for(int i=4;i<=7;i++){
cout<<i<<endl;
if(i==7)
swapcontext(&N2,&Main);
else
swapcontext(&N2,&N1);
}
}
int main(int argc, char *argv[])
{
getcontext(&Main);
getcontext(&N1);
getcontext(&N2);
N1.uc_link=0;
N1.uc_stack.ss_sp=malloc(MEM);
N1.uc_stack.ss_size=MEM;
N2.uc_link=0;
N2.uc_stack.ss_sp=malloc(MEMS);
N2.uc_stack.ss_size=MEMS;
makecontext(&N1, fn1, 0);
makecontext(&N2, fn2, 0);
swapcontext(&Main,&N1);
printf("completed\n");
exit(0);
}
//Do something like this
void fn1()
{
for(int i=0;i<=3;i++){
cout<<i<<endl;
swapcontext(&N1,&Man);
}
}
void fn2()
{
for(int i=4;i<=7;i++){
cout<<i<<endl;
if(i==7)
swapcontext(&N2,&Main);
else
swapcontext(&N2,&Man);
}
void Manager()// void Manager(ucontext_t u)??? and makecontext(&Man,(void(*)())Manager,1,...)
{
//which ucontext transferred control ?
queue <ucontext> q;
push.active_context;
...
swapcontext(&Man,&another_context);
}
一般来说,你需要制作一个Manager,其中会有一个队列,你需要找出哪个上下文处于活动状态并将其放在队列的末尾,并将控制权传递给另一个上下文