Solved

POSIX Dead lock

Posted on 2007-03-23
2
256 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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

717 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