Link to home
Start Free TrialLog in
Avatar of CBMLude
CBMLude

asked on

Can't get my threads running properly

#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!!!
Avatar of BabuPrasanna
BabuPrasanna

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!
ASKER CERTIFIED SOLUTION
Avatar of rooster_0429
rooster_0429

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial