【问题标题】:Xlib Muti-thread program only works under straceXlib多线程程序只能在strace下工作
【发布时间】:2013-10-07 12:00:12
【问题描述】:

我正在使用 xlib、pthread 和 cairo 编写一个多线程程序。 该程序创建一个线程,以便在单击事件后绘制十个点。

问题是:

程序画了三点后就崩溃了,xlib投诉

X Error of failed request:  BadRequest (invalid request code or no such operation)
  Major opcode of failed request:  0 ()
  Serial number of failed request:  67
  Current serial number in output stream:  97

但是,当我使用像“strace ./a.out”这样的 strace 时,它​​可以正常工作。

这是我的代码片段:

void *draw_point(void *arg) { //paint random-postion point
    int i = 0;
    int seed;
    double x ,y;
    srand(seed);
    cairo_set_source_rgba (cairo, 1, 0.2, 0.2, 0.6);
    for(i = 0; i< 10; i++) {
        x = rand() % 200;
        y = rand() % 200;
        if(candraw) {
            cairo_arc (cairo, x, y, 10.0, 0, 2*M_PI);
            cairo_fill (cairo);
        }
        hasdraw = true;
        sleep(1);
    }
    return NULL;
}

bool win_main(void)
{
    int clickx = 0, clicky = 0;
    unsigned long valuemask;
    XEvent event;

    valuemask = ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PointerMotionMask;

    XSelectInput(display, win, valuemask);

    pthread_t thread;

    while (1) {
        while (XPending(display) == 0) {
            candraw = true;
            if(hasdraw)
            XFlush(display);
            candraw = false;
        }
        XNextEvent(display, &event);
        candraw = false;
        switch (event.type) {
        case MotionNotify:
            //...
            break;
        case ButtonPress:
            clickx = event.xbutton.x;
            clicky = event.xbutton.y;
            if(clicky < 50)
                pthread_create(&thread, NULL, draw_point, NULL);
            break;
        case ButtonRelease:
            //...
            break;
        default:
            break;
        }
    }
    return 0;
}

有人知道这个奇怪的问题吗? 非常感谢!

【问题讨论】:

    标签: pthreads cairo xlib strace


    【解决方案1】:

    该问题是由于使用多线程和 2 个线程同时尝试访问显示而引起的。

    strace 会改变时间,所以线程在不同的时间访问显示。

    Xlib 确实具有防止冲突的功能。查找 XInitThreads,它启用线程支持以及 XLockDisplay 和 XUnlockDisplay,您需要在访问显示之前和之后从每个线程中调用它们。

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 2022-11-24
      • 2019-10-25
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      相关资源
      最近更新 更多