thread program errors help

This is a discussion on thread program errors help within the C Programming forums, part of the General Programming Boards category; can some one tell me where im going wrong thanks Code: #include <pthread.h> #include <stdio.h> #include <time.h> #include <stdlib.h> #include ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    thread program errors help

    can some one tell me where im going wrong
    thanks

    Code:
    #include <pthread.h>  
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <unistd.h>
    #define BUFFER_SIZE 100;
    #define SIGMA 100.8
    #define MU 6.6
    
    double randno();
    double normal();
    double exponential();
    void *producer();
    void *consumer();
    
    time_t T;
    
    
    //buffer and related
    double buffer[BUFFER_SIZE];
    int in=0;
    int out=0;
    double mean;
    int i=0;
    
    //random no b/w 0~1
    double randno(){
    return (rand()/(double)RAND_MAX);
    }
    
    //normal distribution
    double normal(){
      double x1, x2, w;                                          
      x1 = randno();
      x2 = randno();
      w = sqrt(x1*x1+x2*x2);
      return (SIGMA * x1 * sqrt( abs(2 * log(w))/w ) + MU);
    }
    
    //exponential distribution
    double exponential(){
      double x;                                                                                                 
      x = randno();
      return ( -log(1-x)/MU );
    }
    
    //producer function
    void *producer(){
    
      while(1){
        while((in+1)% BUFFER_SIZE == out)
        //do nothing as buffer is full
        {;}    
    
        buffer[in++] = normal();
    
        in %= BUFFER_SIZE;
    
        usleep(exponential());
      }
      pthread_exit(0);
    }
    
    //consumer function
    void *consumer(){
     
      while(1){
        double sum=0;
    
          while( in == out ){//isempty
        usleep(exponential());
          }
       
       for(i=0; i<10; i++){
          sum+=buffer[out];
          
          out++;
          out %= BUFFER_SIZE;
    
        }
        mean = sum/10;
        
        printf("%d\t",in);
        T = time(NULL);
        printf(ctime(&T));
        printf("\n");
      }
      pthread_exit(0);
    }
    
    int main(){
     
      pthread_t prod,cons;
     
      pthread_create(&prod, NULL, producer, NULL);
      pthread_create(&cons, NULL, consumer, NULL);
    
      pthread_join(prod,NULL);
      pthread_join(cons,NULL);
     
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sure, you didnt include your error messages

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > #define BUFFER_SIZE 100;
    This will stop it compiling, remove the ;

    You need to fix all that shared access to global data. There is no control over when one thread stops and the other takes over.

    > for(i=0; i<10; i++)
    Who said producer managed to queue 10 items since last time?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    >> #define BUFFER_SIZE 100;
    >This will stop it compiling, remove the ;

    i removed the ;
    and so it compiles

    >> for(i=0; i<10; i++)
    >Who said producer managed to queue 10 items since last time?

    i put the while loop inside the for loop so the problem with atleast 10 in the buffer is resolved
    thats where it shd have been in the first place.

    about the global variables
    they just share the buffer and its pointers(in/out)
    there could be some conflicts but it should still give me some output

    the program compiles but the output is just a lot of gibberish
    even if i dont printf anything the same output comes
    im using sun solaris 10

    plus it gives one warning : no newline at end of file

    thanks alot

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    >> #define BUFFER_SIZE 100;
    >This will stop it compiling, remove the ;

    i removed the ;
    and so it compiles

    >> for(i=0; i<10; i++)
    >Who said producer managed to queue 10 items since last time?

    i put the while loop inside the for loop so the problem with atleast 10 in the buffer is resolved
    thats where it shd have been in the first place.

    about the global variables
    they just share the buffer and its pointers(in/out)
    there could be some conflicts but it should still give me some output

    the program compiles but the output is just a lot of gibberish
    even if i dont printf anything the same output comes
    im using sun solaris 10

    plus it gives one warning : no newline at end of file

    thanks alot

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Be aware that the compiler may store global variables in registers, so if you have multiple threads using the same global variable(s), then you may find that they don't see the updates the other thread does (this is more likely to happen on a Sparc or x86-64 processor than an x86-32 processor, due to higher number of available registers - but there's no guarantee that it doesn't happen on ANY machine).

    The solution is to use "volatile" for variables like in and out. This forces the compiler to read the variable from memory EVERY time it's used, and write back any result to the memory, without "holding it".

    I'm not guaranteeing that this is the right and the whole answer, but I strongly suspect it's part of the problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > there could be some conflicts but it should still give me some output
    M'kay...

    > the program compiles but the output is just a lot of gibberish
    OK, you've got some output, and some conflicts - what else do you want?


    First off, I would suggest you use only a single global variable.
    - the producer tries to increment it up to some limit value, and prints each change it makes
    - the consumer tries to decrement it down to zero, and prints each change it makes.

    Thread programming is about 1000&#37; harder than regular single threaded programs. If you create a race condition, then you can be sure this will show up when you're least able to investigate it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    Under POSIX, you don't use volatile in multi-threaded programming. Here are the only gaurentees your have with respect to memory synchronization under POSIX: http://www.opengroup.org/onlinepubs/...html#tag_04_10

    The MS compiler (since version 14) is the only compiler that gives multi-threading semantics to the volatile keyword. See Multiprocessor Considerations for Kernel-Mode Drivers (warning: word document)

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Few Simple Program Errors
    By pobri19 in forum C Programming
    Replies: 3
    Last Post: 05-22-2008, 06:12 PM
  3. Errors in Dev-C++ program
    By sinking2008 in forum C Programming
    Replies: 5
    Last Post: 05-15-2008, 09:00 AM
  4. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM
  5. Comment this Program
    By kishorepalle in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 06:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21