Its me again. I wouldn't normally trouble people on a forum with all this but last time I posted here I got really good feedback and everyone was really helpful. So, I have created a program that uses the Monte Carlo method to find the value of pie and for the most part it works but I have a few questions.
1) I don't really understand how to use the pthread_join() function because when I implement it and try to create more than some odd threads I seg fault but if I don't put it in at all I don't seg fault.
2) I wrote the program and my professor said that it should output the value of pie and lets say I want 20 threads each thread does a process a 1000 times my value for pie should be similar to at least 8 or maybe even 10 decimal places each time I fun it but mine is not. I thought that might be to my mutex locks or my data types that I used. I am using doubles right now but Im not sure if I should use long ints instead or if it matters.
I am not asking for answers to all my problems but I am so new to POSIX threads that I am not sure what is causing me my major faults and what areas I need to check to fix my issues. I know my pthread_join isn't 100% right but I am not sure how to fix it or where to start.
So enough of me complaining below is my source code, if you would give me feed back on parts that are going to cause me issue pertaining to my 1 and 2 issues I would be very thankful. And forgive me for my messy code I have yet to clean it up.
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <ctime>
#include<iomanip>
using namespace std;
//Declaring Global Variables
double inside_val = 0;
double tries_total = 0;
double pi_val;
//Global Variables
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct params //Structure to pass number of iterations to threads
{
int num;
};
void * ranPtsThread(void * p) //Thread generates random points
{
params * data = (params*)p;
//Stores the value of iterations passed by the command-line
//in a local variable named iter
int iter = data->num;
int tries = 0;
double x, y, inside; //x and y values and local number of hits inside the circle
//Loop Generates number random points and test whether
//the points are inside or outside the circle
for(int i = 0; i < iter; i++)
{
pthread_mutex_lock(&mymutex); //Mutex lock
x = static_cast<double>(rand( ))/RAND_MAX; //Generates a random X value
y = static_cast<double>(rand( ))/RAND_MAX; //Generates a random y value
if((x*x + y*y) <= 1) //This would be inside the circle
{
tries++;
inside++;
}
else //Else it was a miss and not inside
{
tries++;
}
if(tries == 100 || i == (iter - 1)) //Updates the grand total inside hits
{ //and stores that to the global variable inside_val
inside_val = inside_val + inside; //
tries_total = tries_total + tries; //Updates the grand total of tries
inside = 0; //Sets the local variables inside and tries
tries = 0; //back to zero
}
pthread_mutex_unlock(&mymutex); //Mutex unlock
}
// return 0;
}
//Main Function
int main(int argc, char *argv[]) //Main function accepts two arguments, used to read
{ //in number of threads and iterations from user
srand(time(0)); //Seeds the random function
int num_threads = atoi(argv[1]); //Converting command line arguments to
int iterations = atoi(argv[2]); //integers and storing in variables
params num_iter; //Declaring num_iter as parameter
num_iter.num = iterations; //to pass iterations to threads
pthread_t TID[num_threads]; //Defining thread TID
for(int t = 0; t < num_threads; t++) //Generating number of threads defined by
{ //User
pthread_create(&TID[t], NULL, ranPtsThread, &num_iter); //Creating threads
}
//This loop joins the array of threads
/* for(int t = 0; t < num_threads; t++)
{
pthread_join(TID[t], NULL);
}
*/ pi_val = ((4*inside_val)/tries_total);
cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(14) << pi_val << endl;
}
1) I don't really understand how to use the pthread_join() function because when I implement it and try to create more than some odd threads I seg fault but if I don't put it in at all I don't seg fault.
2) I wrote the program and my professor said that it should output the value of pie and lets say I want 20 threads each thread does a process a 1000 times my value for pie should be similar to at least 8 or maybe even 10 decimal places each time I fun it but mine is not. I thought that might be to my mutex locks or my data types that I used. I am using doubles right now but Im not sure if I should use long ints instead or if it matters.
I am not asking for answers to all my problems but I am so new to POSIX threads that I am not sure what is causing me my major faults and what areas I need to check to fix my issues. I know my pthread_join isn't 100% right but I am not sure how to fix it or where to start.
So enough of me complaining below is my source code, if you would give me feed back on parts that are going to cause me issue pertaining to my 1 and 2 issues I would be very thankful. And forgive me for my messy code I have yet to clean it up.
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <ctime>
#include<iomanip>
using namespace std;
//Declaring Global Variables
double inside_val = 0;
double tries_total = 0;
double pi_val;
//Global Variables
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct params //Structure to pass number of iterations to threads
{
int num;
};
void * ranPtsThread(void * p) //Thread generates random points
{
params * data = (params*)p;
//Stores the value of iterations passed by the command-line
//in a local variable named iter
int iter = data->num;
int tries = 0;
double x, y, inside; //x and y values and local number of hits inside the circle
//Loop Generates number random points and test whether
//the points are inside or outside the circle
for(int i = 0; i < iter; i++)
{
pthread_mutex_lock(&mymutex); //Mutex lock
x = static_cast<double>(rand( ))/RAND_MAX; //Generates a random X value
y = static_cast<double>(rand( ))/RAND_MAX; //Generates a random y value
if((x*x + y*y) <= 1) //This would be inside the circle
{
tries++;
inside++;
}
else //Else it was a miss and not inside
{
tries++;
}
if(tries == 100 || i == (iter - 1)) //Updates the grand total inside hits
{ //and stores that to the global variable inside_val
inside_val = inside_val + inside; //
tries_total = tries_total + tries; //Updates the grand total of tries
inside = 0; //Sets the local variables inside and tries
tries = 0; //back to zero
}
pthread_mutex_unlock(&mymutex); //Mutex unlock
}
// return 0;
}
//Main Function
int main(int argc, char *argv[]) //Main function accepts two arguments, used to read
{ //in number of threads and iterations from user
srand(time(0)); //Seeds the random function
int num_threads = atoi(argv[1]); //Converting command line arguments to
int iterations = atoi(argv[2]); //integers and storing in variables
params num_iter; //Declaring num_iter as parameter
num_iter.num = iterations; //to pass iterations to threads
pthread_t TID[num_threads]; //Defining thread TID
for(int t = 0; t < num_threads; t++) //Generating number of threads defined by
{ //User
pthread_create(&TID[t], NULL, ranPtsThread, &num_iter); //Creating threads
}
//This loop joins the array of threads
/* for(int t = 0; t < num_threads; t++)
{
pthread_join(TID[t], NULL);
}
*/ pi_val = ((4*inside_val)/tries_total);
cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(14) << pi_val << endl;
}