0

I managed to find several references to this problem which suggested pthread_kill was de-referencing the pthread_t structure which was causing some problem however other articles said this is not a problem as long as the pthread_t staructure is created via pthread_create.

Then I found a multi thread example of how to do this correctly :

How to send a signal to a process in C?

However I am still getting seg faults so here is my code example:

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!!!
}
Walter
  • 173
  • 1
  • 13
  • 1
    For starters, it's not safe to call stdio functions in a signal handler. See [man signal-safety](http://www.man7.org/linux/man-pages/man7/signal-safety.7.html) for details. – Shawn May 31 '19 at 03:34
  • The printfs are not expected to remain in the working code. Consider them removed! – Walter May 31 '19 at 06:55
  • 1
    The error is this line : GPUthread = pthread_create(&GPUthread, &attr_obj, ProcessGPUqueue, NULL); It should just be : pthread_create(&GPUthread, &attr_obj, ProcessGPUqueue, NULL); It took three hours of debugging to spot this!!!! – Walter May 31 '19 at 09:00

0 Answers0