多线程应用中的pthread_kill导致分段错误

时间:2019-05-31 03:27:47

标签: linux pthreads

我设法找到了对该问题的一些引用,这表明pthread_kill正在取消引用pthread_t结构,这导致了一些问题,但是其他文章说,只要通过pthread_create创建了pthread_t架构,这不是问题。

然后我找到了如何正确执行此操作的多线程示例:

How to send a signal to a process in C?

但是我仍然遇到段错误,因此这是我的代码示例:

static pthread_t GPUthread;

static void GPUsigHandler(int signo)
{
    fprintf(stderr, "Queue waking\n");
}

void StartGPUQueue()
{
    sigset_t sigmask;                 
    pthread_attr_t attr_obj;             /* a thread attribute variable */
    struct sigaction action;

    /* set up signal mask to block all in main thread */
    sigfillset(&sigmask);                /* to turn on all bits */
    pthread_sigmask(SIG_BLOCK, &sigmask, (sigset_t *)0);

    /* set up signal handlers for SIGINT & SIGUSR1 */
    action.sa_flags = 0;
    action.sa_handler = GPUsigHandler;
    sigaction(SIGUSR1, &action, (struct sigaction *)0);

    pthread_attr_init(&attr_obj);        /* init it to default */
    pthread_attr_setdetachstate(&attr_obj, PTHREAD_CREATE_DETACHED);

    GPUthread = pthread_create(&GPUthread, &attr_obj, ProcessGPUqueue, NULL);

    if (GPUthread != 0) 
    {
        fprintf(stderr, "Cannot start GPU thread\n");
    }
}

void ProcessGPUqueue(void *ptr)
{
    int sigdummy;
    sigset_t sigmask;

    sigfillset(&sigmask);                /* will unblock all signals */  
    pthread_sigmask(SIG_UNBLOCK, &sigmask, (sigset_t *)0);

    fprintf(stderr, "GPU queue alive\n");

    while(queueActive)
    {
        fprintf(stderr, "Processing GPU queue\n");
        while(GPUqueue != NULL)
        {
          // process stuff
        }

        sigwait(&sigmask, &sigdummy);
    }
}


void QueueGPUrequest(unsigned char cmd, unsigned short p1, unsigned short p2, unsigned short p3, unsigned short p4)
{
    // Add request to queue logic ...

    fprintf(stderr, "About to Wake GPU queue\n");
    pthread_kill(GPUthread, SIGUSR1);// Earth shattering KA-BOOM!!!
}

0 个答案:

没有答案