Thread: Trying to implement a thread barrier and running into some hanging problems.

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    5

    Post Trying to implement a thread barrier and running into some hanging problems.

    note: I'm currently using mac os X so I couldn't use the pthread_barrier primitive.

    I'm trying to hold a 'n' number of threads to do a matrix computation on two matrices. Only once every thread has been created or is 'ready' the first thread will start computing the appropriate subset of the matrix and then the second thread and so on. (I'm trying to simulate a relay race, so this would be one team) I'm just trying to get it working with one team.

    Anyways here it seems where my problem is rooted:

    Code:
    //creating start method right now just a prototype
    //this method will be implemented as a "barrier"
    void* start(void* input){
    
        long id = (long)input;
    
        //maybe I will need to create a buffer here to hold the threads
    
        if(id == k -1){
            pthread_cond_signal(&testCond);
        }
        //create a mutex lock here
        pthread_mutex_lock(&theMutex);
    
        //set boolean predicate for condition variable
        //while the last thread has not come in let's wait for others to arrive.
        while(id < k) pthread_cond_wait(&testCond,&theMutex);
    
        compute(id); // call compute function
    
        //unlock the mutex
        pthread_mutex_unlock(&theMutex);
    
        pthread_exit(0);
    }
    Also, not that the function compute() it's just a function to perform the correct subset multiplication of the two matrices

    and here is most of my main:

    Code:
    int main(int argc, char** args) {
        // Read the number of threads that need to be created for each team from the command line.
        k = atoi(args[1]);
    
        //test the input
        printf("Number of threads is: %d\n",k);
    
        // Populate the A matrix data.
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                A[i][j] = i;
            }
        }
        // Populate the B matrix data.
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < P; j++) {
                B[i][j] = i;
            }
        }
    
    
        //create an array of threads for each team
        pthread_t* redThreads = malloc(k*sizeof(pthread_t));
        pthread_t* blueThreads = malloc(k*sizeof(pthread_t));
    
        for(long i = 0; i < k;i++){
            //create one of each team
            pthread_create(&redThreads[i],0,start,(void*)(long)i);
            //pthread_create(&blueThreads[i],0,start,(void*)(long)i);
        }
        
        //wait for the threads
        for(int i =0; i < k; i++) {
            pthread_join(redThreads[i],0);
            //pthread_join(blueThreads[i],0);
        }
    
        // Print out the matricies.
    Now when I run this code the program just keeps on running and no computation is done, so there seems that I'm not using the pthread condition variables correctly? I'm just trying to figure out why my program is getting "hanged up".

    Any help is greatly appreciated.
    Last edited by hamsasimon; 06-02-2019 at 04:02 PM. Reason: grammar

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > and here is most of my main:
    It wouldn't have taken much more effort to strip out the rest of the fluff and post something we can just copy/paste/test.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    long int k = -1;
    pthread_cond_t  testCond = PTHREAD_COND_INITIALIZER;
    pthread_mutex_t theMutex = PTHREAD_MUTEX_INITIALIZER;
    
    //creating start method right now just a prototype
    //this method will be implemented as a "barrier"
    void* start(void* input){
    
        long id = (long)input;
    
        //maybe I will need to create a buffer here to hold the threads
    
        if(id == k -1){
            pthread_cond_signal(&testCond);
        }
        //create a mutex lock here
        pthread_mutex_lock(&theMutex);
    
        //set boolean predicate for condition variable
        //while the last thread has not come in let's wait for others to arrive.
        while(id < k) pthread_cond_wait(&testCond,&theMutex);
    
        //compute(id); // call compute function
    
        //unlock the mutex
        pthread_mutex_unlock(&theMutex);
    
        pthread_exit(0);
    }
    
    int main(int argc, char** args) {
        // Read the number of threads that need to be created for each team from the command line.
        k = 3; // atoi(args[1]);
    
        //test the input
        printf("Number of threads is: %ld\n",k);
    
        //create an array of threads for each team
        pthread_t* redThreads = malloc(k*sizeof(pthread_t));
    
        for(long i = 0; i < k;i++){
            //create one of each team
            pthread_create(&redThreads[i],0,start,(void*)(long)i);
        }
    
        //wait for the threads
        for(int i =0; i < k; i++) {
            pthread_join(redThreads[i],0);
        }
    }
    All your threads are stuck.
    Code:
    $ gcc -g -Wall -pthread foo.c
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) run
    Starting program: ./a.out 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    Number of threads is: 3
    [New Thread 0x7ffff77ef700 (LWP 3971)]
    [New Thread 0x7ffff6fee700 (LWP 3972)]
    [New Thread 0x7ffff67ed700 (LWP 3973)]
    ^C
    Thread 1 "a.out" received signal SIGINT, Interrupt.
    0x00007ffff7bc298d in pthread_join (threadid=140737345681152, thread_return=0x0) at pthread_join.c:90
    90	pthread_join.c: No such file or directory.
    (gdb) thread apply all bt
    
    Thread 4 (Thread 0x7ffff67ed700 (LWP 3973)):
    #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
    #1  0x00000000004008e0 in start (input=0x2) at foo.c:25
    #2  0x00007ffff7bc16ba in start_thread (arg=0x7ffff67ed700) at pthread_create.c:333
    #3  0x00007ffff78f741d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
    
    Thread 3 (Thread 0x7ffff6fee700 (LWP 3972)):
    #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
    #1  0x00000000004008e0 in start (input=0x1) at foo.c:25
    #2  0x00007ffff7bc16ba in start_thread (arg=0x7ffff6fee700) at pthread_create.c:333
    #3  0x00007ffff78f741d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
    
    Thread 2 (Thread 0x7ffff77ef700 (LWP 3971)):
    #0  pthread_cond_wait@@GLIBC_2.3.2 () at ../sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
    #1  0x00000000004008e0 in start (input=0x0) at foo.c:25
    #2  0x00007ffff7bc16ba in start_thread (arg=0x7ffff77ef700) at pthread_create.c:333
    #3  0x00007ffff78f741d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
    
    Thread 1 (Thread 0x7ffff7fc1700 (LWP 3967)):
    #0  0x00007ffff7bc298d in pthread_join (threadid=140737345681152, thread_return=0x0) at pthread_join.c:90
    #1  0x00000000004009be in main (argc=1, args=0x7fffffffdf48) at foo.c:53
    Your pthread_cond_signal(&testCond); is in the wrong place. Signals have no effect if no-one is waiting.

    So perhaps
    - the first signal to the first thread should come from main
    - the n'th signal should come from the n'th thread after compute() has done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    5
    Yes moving pthread_cond_signal(&testCond) to after the compute() function worked. Also, I created an int variable to keep to make sure the threads go in order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-14-2014, 03:15 PM
  2. Playing a wav file while a thread is running
    By etzarfat in forum C Programming
    Replies: 0
    Last Post: 08-25-2009, 04:05 AM
  3. Replies: 4
    Last Post: 12-20-2007, 07:55 PM
  4. How to check if a thread is running or has come out ?
    By gopi_tony in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2006, 12:40 PM
  5. pthread's - check for running thread
    By xErath in forum Linux Programming
    Replies: 2
    Last Post: 05-29-2005, 03:32 PM

Tags for this Thread