我正在做一个线程库(用uncontext.h更改上下文)。 我的函数是void类型,我无法返回。但即使我没有返回,编译时会出现此警告:
dccthread.c: In function ‘dccthread_init’:
dccthread.c:184:1: warning: ‘noreturn’ function does return [enabled by default]
}
这是函数的简化代码(没有一些细节):
void dccthread_init(void (*func), int param) {
int i=0;
if (gerente==NULL)
gerente = (dccthread_t *) malloc(sizeof(dccthread_t));
getcontext(&gerente->contexto);
gerente->contexto.uc_link = NULL;
gerente->contexto.uc_stack.ss_sp = malloc ( THREAD_STACK_SIZE );
gerente->contexto.uc_stack.ss_size = THREAD_STACK_SIZE;
gerente->contexto.uc_stack.ss_flags = 0;
gerente->tid=-1;
makecontext(&gerente->contexto, gerente_escalonador, 0);
if (principal==NULL)
principal = (dccthread_t *) malloc(sizeof(dccthread_t));
getcontext(&principal->contexto);
principal->contexto.uc_link = NULL;
principal->contexto.uc_stack.ss_sp = malloc ( THREAD_STACK_SIZE );
principal->contexto.uc_stack.ss_size = THREAD_STACK_SIZE;
principal->contexto.uc_stack.ss_flags = 0;
makecontext(&principal->contexto, func, 1, param);
swapcontext(&gerente->contexto, &principal->contexto);
}
请注意,我不会随时返回。但是gcc给了我这个警告。有谁知道这是什么问题?
答案 0 :(得分:3)
在代码C的末尾插入一个隐式返回。预期返回函数将在循环中运行或通过系统调用退出。 它与返回但不提供值的void函数不同。
答案 1 :(得分:2)
即使void
函数返回,它也不会返回值。 return;
表示它返回到上一个函数中调用的位置,有或没有新值。正如@Matthias先前所述,任何函数都会在C结尾处自动返回。如果编译器到达函数的结束括号,它将返回。我相信你需要让函数与另一个函数调用或类似的东西离开警告。
我希望这会有所帮助。
答案 2 :(得分:1)
仅仅因为你没有return
并不意味着你的日常生活无法回归。从结束落下(控制到达最后的近距离支撑)相当于回报。
因此
foo(x)
{
...
}
与
相同foo(x)
{
...
return;
}