Solved

POSIX Dead lock

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 24
Problem to show menu 5 107
Can case within switch statement specify range of values ? 3 103
Reactjs with .NET 3 126
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

734 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