c - 为什么这段代码会阻塞?

标签 c linux pthreads

我有一些代码需要很短的时间才能完成。我希望它在单独的线程上处理,因为它主要被 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/

相关文章:

c - 将 DLL 调用函数从 C 翻译成 Delphi

c - 如何修复 Code::Blocks 向导以查找 GTK+ 3.0 而不是 2.0?

c - pthread_exit() 和 exit() 之间的区别?

linux - 为什么我的 shell (sh) 变量在命令模式下没有导出?

Python 看门狗脚本 : load url asynchronously

c - 分配结构堆与堆栈 : Pthread_create

c - 在所有生产者都结束之后,如何正确终止多个消费者?

c - Fgets() 在一个操作系统上导致段错误,但在另一个操作系统上不会

c - C 中的预期标识符错误

node.js - 配置 monitrc 以监视 Node js 应用程序进程,无法找到与 screen ec2 linux 一起运行的 Node 应用程序 pidfile