如何退出主线程?



cur_rent
2011-01-31 19:07:43

第一次写PSP程序,想在在屏幕上打印出按键和遥杆坐标,主要代码如下:
……
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
……
int main()
{……
while(true) {
……
if (!ipad.Buttons ……) {
printf(……)
……
}
else if (……) {
switch (ipad.Buttons) {
case PSP_CTRL_SELECT:
printf("You have pressed SELECT button.
");
break;
……
}
}
}
return 0;
}
测试后发现无法正常退出(home键),在“请稍等……”时死了。
看了sample后发现其中定义一全局变量done,研究其作用后改原代码如下:

……
int done = 1;
int exit_callback(int arg1, int arg2, void *common) {
done = 0;
return 0;
}
……
int main()
{……
while(done){
……
if (!ipad.Buttons ……) {
printf(……)
……
}
else if (……) {
switch (ipad.Buttons) {
case PSP_CTRL_SELECT:
printf("You have pressed SELECT button.
");
break;
……
}
}
}
sceKernelExitGame();
return 0;
}
现猜测其作用机理如下:
按下home键后调用exit_callback函数终止循环,再由sceKernelExitGame退出主线程,之前因未终止循环导致主线程无法终止,故停在“请稍等……”处。
其中精确流程未明还请哪位高手指点迷津。