我有一些代码需要很短的时间才能完成。我希望它在单独的线程上处理,因为它主要被 IO 阻塞。为此,我实现了以下内容,但是当调用线程运行 background_picture_save()
时,它似乎会阻塞。为什么?
我正在尝试让 save_picture()
函数作为后台进程工作。
static void * threaded_save_picture(void * p);
static void * threaded_save_picture(void * p)
{
char optarg[512];
strncpy(optarg, p, sizeof optarg); optarg[sizeof optarg - 1] = '\0';
fprintf(stderr,"%s()::%s\n",__FUNCTION__,optarg);
save_picture(optarg);
pthread_detach(pthread_self());
return(p);
} /* threaded_save_picture() */
extern void background_picture_save(const char * const optarg);
void background_picture_save(const char * const optarg)
{
pthread_t thrd;
(void)pthread_create(& thrd, NULL, threaded_save_picture, (void *) optarg);
} /* background_picture_save() */
最佳答案
消除观察中的歧义。
使用 gdb 运行程序,当主线程 block 打印带有“where”的回溯时。
使用 strace 显示阻塞时进行的系统调用。
使用 systemtap http://sourceware.org/systemtap/显示阻塞进程的内核回溯(尽管我看到 pthreads 支持最近才进入 http://www.cygwin.com/ml/libc-alpha/2011-01/threads.html#00010 )。
关于c - 为什么这段代码会阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043006/