Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Can't get my threads running properly

Posted on 2003-03-01
2
Medium Priority
?
181 Views
Last Modified: 2010-08-05
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include "circ_buffer.h"

static char *message = "Threading";

static const unsigned int SIZE = 5;
static circ_buffer_t cb;

static void *producer(void *arg);
static void *consumer(void *arg);

pthread_mutex_t mutex;     //defines the mutex
pthread_cond_t threshold;  //defines the condition
pthread_cond_t full;       //cond if the buffer is full

int main(void)
{
  pthread_t PID, CID;    //define the producer and consumer threads
  pthread_attr_t attr;   //defines the attributes for the threads

  pthread_mutex_init(&mutex, NULL);     //initialize the mutex object
  pthread_cond_init(&threshold, NULL);  //initialize the condition object
  pthread_cond_init(&full, NULL);       //initialize the condition object
 
  printf("heeree\n");
  circ_buffer_init(&cb, SIZE);  //initialize a circular buffer to SIZE
 
  printf("Before the threads\n");
  pthread_create(&PID, &attr, producer, NULL/*SIZE*/); //PROBLEM HERE
  printf("In betwen the threads\n");
  pthread_create(&CID, &attr, consumer, NULL/*SIZE*/); //PROBLEM HERE TOO
  printf("After the threads\n");
 

  /*****joins threads together*****/
  //pthread_join(PID, NULL);
  //pthread_join(CID, NULL);

  /****clean up****/
  pthread_attr_destroy(&attr);
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&threshold);
  pthread_cond_destroy(&full);
 
  circ_buffer_destroy(&cb);
  pthread_exit(NULL);
  //return 0;
}

Well basically I when I run the program, All I get is the fprint statements(used for debugging purposes only).  It doesn't seem to call my producer or consumer function already defined below my main (not included because it's not really neccessary).  I've seem to been able to do the same thing with semaphores, but I can't get the threads to run properly with semaphores.  I was just wondering if it's possible, and can anyone suggest a solution to my problem(s)?  

Thanks in advance!!!
0
Comment
Question by:CBMLude
2 Comments
 

Expert Comment

by:BabuPrasanna
ID: 8051648
Just try to get the returned error number and print the sys_error_list[error_no](forgot the exact extern variable name!) This could help you!

Also check the return values of the pthread_create() function before proceeding further! otherwise the error tracking will become a menace!

pthread_create works fine in any machine!
Looks like you have commented the pthread_join function! Hope you know that if the main thread exits then all the threads in the process space are killed without option! uncomment and try! still if it doesnt workn just print the system error string and infer the posssible error!

good luck!
0
 

Accepted Solution

by:
rooster_0429 earned 400 total points
ID: 8053521
pthread_create(&PID,&attr,producer,NULL /*SIZE*/ );

the problem is the second parameter of your pthread_create statements you are passing an uninitialized pthread_attr_t don't do this just pass NULL. I tested this in a simple hello world prog and it seemed to fix the problem. I think what is actually happening is that you're passing an address that you haven't initialized so you have no idea whats in that memory location. The pthread_create method sees that junk that is in there and returns an error instead of running your method.
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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 how to use strings and some functions related to them 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.

580 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