开发者

Why is this code blocking?

I have some code which takes a short while to complete. I'd like it to be handled on a separate thread as it is mostly blocked by IO. To do this I implemented the following, but when the calling thread runs background_picture_save(), it seems to block. Why?

I'm trying to get the save_picture() function to work as a background process.

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 *) o开发者_JAVA技巧ptarg);
} /* background_picture_save() */


Take the ambiguity out of your observations.

Run the program with gdb, when the main thread blocks print a backtrace with "where".

Use strace to show the system calls being made when blocking.

Use systemtap http://sourceware.org/systemtap/ to show kernel backtraces for the blocked process (although I see that pthreads support only recently went in http://www.cygwin.com/ml/libc-alpha/2011-01/threads.html#00010).


I think you have to do it in this order:

pthread_detach(pthread_self());
save_picture(optarg);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜