[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

POSIX Dead lock

Posted on 2007-03-23
2
Medium Priority
?
261 Views
Last Modified: 2011-09-20
Hey guys, im just trying to create 10 threads which then i want to print out in backwards order... so
"from thread 10", "from thread 9" ---> "from thread 1"

But im getting deadlocked after i get "from thread 10" to print.... Any ideas?

#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>

pthread_mutex_t mlock;
pthread_cond_t conditions[10];

int count = 10;


void * func(void * arg)
{
   int index = (int)arg - 1;

   pthread_mutex_lock(&mlock);

      while(count != (int)arg)
         pthread_cond_wait(&conditions[index], &mlock);

      printf("from thread %d\n", (int)arg);
      count--;
      index--;

      pthread_cond_signal(&conditions[index]);

    pthread_mutex_unlock(&mlock);

    return (NULL);
}


int main(int argc, char* argv[]) {
    int n,i;
    pthread_t threads[count];
    pthread_mutex_init(&mlock, NULL);

    for (i = 0; i < count; i++) {
      pthread_create(&threads[i], NULL, func, (void*)i+1);
      pthread_cond_init(&conditions[i], NULL);
    }

    for (i = 0; i < count; i++) {
      pthread_join(threads[i], NULL);
    }

    exit(1);
}
0
Comment
Question by:intazaar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 2000 total points
ID: 18787423
I don't know if this is the problem, but I can tell you one thing that looks fishy.

A thread might use a condition variable before it has been initialized.  Specifically, after a thread is started, it could easily get to the pthread_cond_wait call before the main thread has called pthread_cond_init.  This is not the order you want.

You could try moving the call to pthread_cond_init before the call to pthread_create and see if that helps.
0
 

Author Comment

by:intazaar
ID: 18787510
Thx mate, that was exactly it. Runs like a dream!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question