Thread: thread or mutex SIGTRAP in multiple threads program

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    thread or mutex SIGTRAP in multiple threads program

    Hello,
    I am trying a multithreaded code but I am recieving sigtrap. I am trying to test a round robin algorithm but somehow I am SIGTRAPing in the atomic_push function.

    Code:
    
    typedef struct _s
    {
        char system[32];
        struct _s* next;
    } node;
    
    
    static node* s_head = 0;
    static pthread_mutex_t mutex;
    
    
    static void atomic_push(node** pHeader, const char* data)
    {
    
    
        pthread_mutex_init(&mutex, NULL);
        pthread_mutex_lock(&mutex);
        critical_section:
        {
            node* pnode = (node*) malloc(sizeof(node));
            strcpy(pnode->system, data);
            pnode->next = (*pHeader);
            (*pHeader) = pnode;
        }
        pthread_mutex_unlock(&mutex);
    }
    
    
    static const char* systems[4] =
    {
        {"system1"},
        {"system2"},
        {"system3"},
        {"system4"}
    };
    
    
    
    
    static int fetch_and_add(int* pVar, int value)
    {
        asm volatile ("lock; xaddl %%eax, %2;"
                      :"=a"(value)
                      :"a" (value), "m" (*pVar)
                      :"memory");
        return value;
    }
    
    
    static int atomic_int = 1;
    
    
    static void run(void)
    {
        char* str = systems[fetch_and_add(&atomic_int, 1) % 4];
        atomic_push(&s_head, str);
    }
    
    
    
    
    int main(void)
    {
    
    
        int i = 0;
        pthread_t tids[500];
    
    
        for(i=0; i < 500; i++) {
            pthread_create(&tids[i],0, &run, 0);
            usleep(200);
        }
    
    
        node* it = &s_head;
        i =0;
        while ( it != NULL ) {
    
    
            printf("[%d]:[%s]\n", i++, it->system);
            it = it->next;
        }
    
    
    
    
        return 0;
    }
    A help will be appreciated.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You only call pthread_mutex_init once, and then use that initialized mutex for the remainder of its lifetime.

    Also add error checking.

    You need to call pthread_join on all your created threads before exiting main.

    gg

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    Quote Originally Posted by Codeplug View Post
    You only call pthread_mutex_init once, and then use that initialized mutex for the remainder of its lifetime.

    Also add error checking.

    You need to call pthread_join on all your created threads before exiting main.

    gg
    Hello,
    actually I had a wrong iterator for printing the list, but thanks for the reminder of the joining the threads.

    Code:
    //node* it = (&s_head);
    node* it = *(&s_head);

    I was iterating to the list of lists, not trough the nodes. . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-11-2012, 06:45 PM
  2. Mutex and Multiple Threads Problem
    By roweboats in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2010, 03:04 PM
  3. Thread/mutex difficulty
    By erasm in forum C Programming
    Replies: 30
    Last Post: 09-21-2009, 06:11 PM
  4. Replies: 2
    Last Post: 05-08-2008, 09:50 AM
  5. Producer-comsumer threads using semaphore mutex
    By Kaiya in forum C Programming
    Replies: 3
    Last Post: 04-29-2004, 08:17 AM

Tags for this Thread