Solved

POSIX Dead lock

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 58
Replication of Nagios surveilance data 2 90
Problem to save 10 122
improve Shell script 5 138
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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now