Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:04:14+00:00 2026-06-18T09:04:14+00:00

I am writting a program which computes the value of pi using the bailey-borwein-plouffe

  • 0

I am writting a program which computes the value of pi using the bailey-borwein-plouffe formula.My problem is i keep getting different results whenever i run this code using the mutex.I am not sure if the mutex lock in the pie function definition should be used for just one resource pi .

enter code here
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#define NUM_THREADS     1000

void *pie_function(void * p);//returns the value of pie
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; //creates a mutex variable
double pi=0,p16=1;
 main()
{
pthread_t threads[NUM_THREADS];  //creates the number of threads using NUM_THREADS
int iret1;   //used to ensure that threads are created properly
         //pthread_create(thread,attr,start_routine,arg)

int i;
   pthread_mutex_init(&mutex1, NULL);


for(i=0;i<NUM_THREADS;i++){
      iret1= pthread_create(&threads[i],NULL,pie_function,(void *) i);
   if(iret1){
        printf("ERROR; return code from pthread_create() is %d\n", iret1);
        exit(-1);
   }

}

for(i=0;i<NUM_THREADS;i++){
  iret1=pthread_join(threads[i],NULL);

 if(iret1){
        printf("ERROR; return code from pthread_create() is %d\n", iret1);
        exit(-1);
   }

}

pthread_mutex_destroy(&mutex1);
  printf("Main: program completed. Exiting.\n");
  printf("The value of pi is  : %f\n",pi);

  exit(0);

   }



 void *pie_function(void *s){
 int rc;
 int k=(int) s;
  pthread_mutex_lock( &mutex1 ); //locks the share variable pi and p16
   pi += 1.0/p16 * (4.0/(8*k + 1) - 2.0/(8*k + 4) - 1.0/(8*k + 5) - 1.0/(8*k+6));
 p16 *=16;

 rc=pthread_mutex_unlock( &mutex1 );
 if(rc){
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
  }



  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:04:15+00:00Added an answer on June 18, 2026 at 9:04 am

    You would be better off ditching threads totally in this case.

    Threading is handy when a group of threads can execute mostly in their own little world without affecting each other (occasional resource contention is okay).

    However, since your threads are very short-lived and pretty well lock the mutex for their entire lifetime, you are getting no advantage from threading. All you’re doing is adding extra overhead for little or no benefit.

    This particular task would be better handled a a series of sequential steps.


    However, if you feel you must do this thing, you can fix the problem by looking at the following formula:

    pi += 1.0/p16 * (4.0/(8*k + 1) - 2.0/(8*k + 4) - 1.0/(8*k + 5) - 1.0/(8*k+6));
    

    Obviously, what gets added to pi depends on both k and p16. Now your p16 is well-controlled (within the mutex) but your k is not since it’s passed in as a parameter.

    The problem you have is that, even though you may start the thread with k == 7 before the one with k == 8, there’s no guarantee that the former will get the mutex first. If the latter gets the mutex first, your k and p16 will not have “compatible” values. Such are the vagaries of threading.

    You can fix this by putting k under the control of the mutex.

    In other words, don’t pass it as a parameter, rather do the same thing as you do with p16. Initialise them all with:

    double pi=0, p16=1;
    int k = 0;
    

    Then have your thread function contain:

    // Should actually check all mutex calls.
    
    pthread_mutex_lock (&mutex1);
    pi += 1.0/p16 * (4.0/(8*k + 1) - 2.0/(8*k + 4) - 1.0/(8*k + 5) - 1.0/(8*k+6));
    p16 *=16;
    k++;
    pthread_mutex_unlock (&mutex1);
    

    This way, the values of k and p16 are kept in step regardless of the order of thread execution.

    However, I still maintain that threads are a bad idea in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple server-client program in which server and client are communicating using
I wrote a program in C to generate fractals using a formula, but it
I in the process of writting a simple java program which reads the contents
I am writing a program which is used to launch different command line applications.
I'm having the idea of writing a program using Python which shall find a
I am writing a program which needs to be run in Android 2.0. I
I am writing a program which needs to access data in a web server.
I m writing a program which involves converting java.util.Date to java.sql.Date... I've done it
I am writing a program which collects lots of individual pieces of data from
I'm writing a program which has both an ASP.NET configuration system and a Silverlight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.