gpt4 book ai didi

c - 用户级线程

转载 作者:行者123 更新时间:2023-11-30 17:54:26 25 4
gpt4 key购买 nike

我正在尝试创建用户级线程。这是我的代码示例。任何人都可以帮助我这个程序有什么问题吗?

#include<stdio.h>
#include<ucontext.h>

int thread_counter = 0;
int thread1, thread2;
int who_am_i;

struct TCB {
ucontext_t context;
void (* fun_ptr)();
};
struct TCB tcb[3];
char stack[2][8192];


//----------------------
int thread_create(void (*fun)()) {
static volatile int s;
thread_counter++;
s = 0;
getcontext(&tcb[thread_counter].context);

if(!s) {
tcb[thread_counter].context.uc_stack.ss_sp = stack[thread_counter];
tcb[thread_counter].context.uc_stack.ss_size = sizeof(stack[thread_counter]);
tcb[thread_counter].context.uc_link = &tcb[0].context;
tcb[thread_counter].fun_ptr = fun;
s = 1;
}
else {
tcb[who_am_i].fun_ptr();
}

return thread_counter;
}


void thread_yield(int next_thread) {
static volatile int switched;
switched = 0;
getcontext(&tcb[who_am_i].context);

if(!switched) {
switched = 1;
who_am_i = next_thread;
setcontext(&tcb[next_thread].context);
}
}


//----------------------
void f1() {
printf("start f1\n");
thread_yield(thread2);
printf("finish f1:\n");
}

void f2() {
printf("start f2\n");
thread_yield(thread1);
printf("finish f2\n");
}


//----------------------
int main() {
thread1 = thread_create(f1);
thread2 = thread_create(f2);

who_am_i = 0;
thread_yield(thread1);
return 0;
}

线程未正确切换。当我运行它时,它会给出以下输出:

start f1
start f2
finish f2

谢谢

最佳答案

您遇到未定义的行为情况。

thread_create中,您要做的第一件事就是增加thread_counter。因此,当您创建第二个线程时,thread_counter 将为2。然后您访问 stack[2] ,这将给您带来未定义的行为。

<小时/>

您还将上下文的 uc_link 成员硬编码为 &tcb[0].context,由于 thread_counter 的“过早”增量,该成员永远不会初始化.

但主要问题是,您实际上并没有创建新的上下文,您只是获取当前线程的上下文。您应该使用 makecontext而是针对每个线程。

关于c - 用户级线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14952980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com