내 스레드 라이브러리를 만들었습니다 ... 물론 비 - 선제 라이브러리는 호출 순서대로 스레드가 실행됩니다. 프로그램의 간단한 아웃 라인은 다음과 같습니다 :사용자 정의 스레드 라이브러리 생성자가 한 번만 호출됩니다.
class Thread
{
static Thread sched;
/*called only once*/
static void init()
{
*create a context for sched thread*
static void schedule()
{
....
setcontext(current thread from the queue);
return if empty
}
Thread(function)
{
intializes the context via getcontext and sets the uc_link to the context of sched
and pushed into the queue and calls schedule function
}
모든 것이 단일 스레드로 좋을 것 같습니다. 하지만 두 스레드 개체를 초기화 할 때 그들 중 하나만 실행됩니다. 첫 번째 스레드가 작업을 완료 할 때 일정 기능으로 돌아가고 일정 기능이 대기열이 비어있는 것을 확인할 때 ... 또한 반환됩니다.
그러나 생성자는 첫 번째 스레드에 대해 한 번만 호출됩니다. 왜 이렇게이다? 내가 constructor에의 일정 함수를 호출하지 않으면
, 대신 내가
void start()
{
schedule();
}
같은 함수를 정의하고 모든 스레드가 제대로 실행 된 초기화 후를 호출합니다. 하지만이 방법을 사용하고 싶지 않습니다.
위의 문제에 대한 실행 경로와 해결책을 알려주세요.
가 여기
class thread
{
static queue<thread> Q;
ucontext_t con;
int u_p;
int c_p;
static void init()
{
flag=0;
getcontext(&schd.con);
//cout<<"fgh\n";
schd.con.uc_link=0;
schd.con.uc_stack.ss_sp=malloc(ST_S);
schd.con.uc_stack.ss_size=ST_S;
makecontext(&schd.con, schd.schedule, 0);
}
static thread main, schd;
static int flag;
public:
thread()
{ }
thread(int i){ init(); }
static void schedule()
{
//cout<<"ii\n";
if(Q.empty()){
flag=0;
return;
}
main=Q.front();
Q.pop();
setcontext(&main.con);
init();
//cout<<"yea\n";
}
thread(void (*fn)(), int u_p=15)
{
getcontext(&con);
con.uc_link=&schd.con;
con.uc_stack.ss_sp=malloc(ST_S);
con.uc_stack.ss_flags=0;
con.uc_stack.ss_size=ST_S;
makecontext(&con, fn, 0);
//cout<<"hjkl\n";
Q.push(*this);
if(flag==0){
flag=1;
schedule();
}
}
static void start(){ schedule(); }
};
queue<thread> thread::Q;
thread thread::main;
thread thread::schd;
int thread::flag;
5 단계 후에 스레드가 일정으로 돌아갑니다. 왜냐하면 나는 ** UC- 링크 **를 스케줄 함수를 가리키는 생성 된 스레드로 유지했기 때문에 (나는 내 질문에 언급했다) ... 한번 보았을 때 ... – nitish712
@ nitish712 나는 그것을 볼 수 있지만 그렇지 않다. 돌아 가지. 그'setcontext()'행 다음에 인쇄를 추가하거나 디버깅을 시도 했습니까? 그래서 우리는 그 점에 도달했는지 확신 할 수 있습니다. – xaizek
네, 맞습니다! 해당 스레드가 호출하는 함수로 돌아 오지 않았습니다. sched의 컨텍스트가 이미 실행 된 (일정 기능) 것이므로 반환하지 않는다고 생각합니다. 그러나 적어도이 후 ryt를 인쇄해야합니다. 왜 그런 일이 일어나지 않습니까 ?? – nitish712