Thread: Multi-threading calculating pi with user input

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    1

    Multi-threading calculating pi with user input

    Code:
    #include<pthread.h>
    #include<math.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    #define NUM_THREADS 8
    int threads = 0;
    
    typedef struct {
        int rank;
        double sum;
    } thread_data;
    
    thread_data td[NUM_THREADS];
    
    void* help_pi(void *rank) {
        int thread_rank = *(int *)rank;
        double incr = 0;
        int sign;
            int k = thread_rank;
            
    
        if (k % 2) {
            sign = -1;
        } else {
            sign = 1;
        }
    
        td[thread_rank].sum = 4*((double)sign / (2 * thread_rank + 1));
    
        do {
            k += NUM_THREADS;
    
            if (k % 2) {
                sign = -1;
            } else {
                sign = 1;
            }
    
            incr = (double)sign / (2 * k + 1);
            td[thread_rank].sum += 4 * incr;
        }
      while ( fabs(incr) > 1e-6);
    
        return NULL;
    }
    
    int main(){
        int rank = 0;
        int err;
        double pi = 0;
        
        
        
        //Introduction
        printf("\nThe Leibniz formula is an infinite series method of calculating PI.\n");
        
        //user input
        printf("\nEnter the number of iterations: ");
        scanf("%d",&threads);
        
        pthread_t thread_ids[NUM_THREADS];
    
        while(rank < threads) {
            td[rank].rank = rank;
    
            err = pthread_create(&(thread_ids[rank]), NULL, help_pi, (void*)&td[rank].rank);
            if (err != 0) {
                printf("Can't create thread error =%d\n", err);
                return 1;
            }
            rank++;
        }
    
        rank = 0;
        while(rank < threads) {
            pthread_join(thread_ids[rank], NULL);
            rank++;
        }
    
        rank = 0;
        while(rank < threads) {
            pi += td[rank].sum;
            rank++;
        }
        //output
        printf("\nThe value of Pi = %f\n", threads, pi);
    
        return 0;
        }

    I'm having trouble printing because it keeps telling me that the f expects argument of double but argument has two type int, not sure what to do now
    thankyou

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("\nThe value of Pi = %f\n", threads, pi);
    You have two parameters, but only one % conversion in the format string.

    Maybe
    Code:
    printf("\nThe value of Pi = %f\n", pi);
    or
    Code:
    printf("\n%d threads computed Pi = %f\n", threads, pi);
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Figures - I was wondering how someone could write multiple threaded code and not have a clue how to fix a simple printf.
    Calculate Pi with Leibniz formula using pthread library (as part of C course) * GitHub
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Multi-threading and/or Multi-process
    By lehe in forum C++ Programming
    Replies: 5
    Last Post: 07-14-2009, 11:43 AM
  2. Is Multi-threading necessary?
    By keira in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-14-2007, 05:13 AM
  3. Multi Threading
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-04-2006, 09:21 PM
  4. Replies: 3
    Last Post: 07-07-2006, 08:53 PM
  5. Multi-threading
    By chandhru in forum Windows Programming
    Replies: 2
    Last Post: 04-29-2004, 09:04 PM

Tags for this Thread