Thread: Run a program on multithreads and compare speed!

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    2

    Run a program on multithreads and compare speed!

    I need to run a program on different ammounts of threads so I can compare the times spend.
    My initial program is this one, a mount carlo simulation:


    Code:
    #include <stdlib.h>
        #include <stdio.h>
        #include <math.h>
        #include <string.h>
        #include <time.h>
        #define SEED 35791246
        
        main(int argc, char* argv)
        {
           int points=10000000;
           double x,y;
           int i,count=0;
           double z;
           double pi;
        
           clock_t begin = clock();
        
           srand(SEED);
           count=0;
           for ( i=0; i<points; i++) {
              x = (double)rand()/RAND_MAX;
              y = (double)rand()/RAND_MAX;
              z = x*x+y*y;
              if (z<=1) count++;
              }
           pi=(double)count/points*4;
           printf("The number o variables is %d , and the pi estimation is %g \n",points,pi);
        
           clock_t end = clock();
           double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
           printf("Time Spent: %f  \n", time_spent);
        }
    Everything works fine, but now I need to run it differents amounts of threads, let's say 2 and 4, I tried this but its not working:


    Code:
        #include <stdlib.h>
        #include <stdio.h>
        #include <math.h>
        #include <string.h>
        #define SEED 35791246
        #define NUM_THREADS1 2
        #define NUM_THREADS2 4
        
        main(int argc, char* argv)
        {
        
           pthread_t thread1, thread2;
           pthread_t thread1[NUM_THREADS1]
           pthread_t thread2[NUM_THREADS2]
        
        
           int points=10000000;
           double x,y;
           int i,count=0;
           double z;
           double pi;
        
           srand(SEED);
           count=0;
           for ( i=0; i< NUM_THREADS1; i++) {
              x = (double)rand()/RAND_MAX;
              y = (double)rand()/RAND_MAX;
              z = x*x+y*y;
              if (z<=1) count++;
              }
           pi=(double)count/points*4;
           printf("The number o variables is %d , and the pi estimation is %g \n",points,pi);
        
           srand(SEED);
           count=0;
           for ( i=0; i< NUM_THREADS2; i++) {
              x = (double)rand()/RAND_MAX;
              y = (double)rand()/RAND_MAX;
              z = x*x+y*y;
              if (z<=1) count++;
              }
           pi=(double)count/points*4;
           printf("The number o variables is %d , and the pi estimation is %g \n",points,pi);
        }
    I'm new to threads so I could use a little input on how to solve this. Thank you in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What's with the freaky topic?

    A short example of how to pass parameters to a thread, and get results back from a thread.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    typedef struct {
      int   num_points;
    } thread_param_t;
    
    typedef struct {
      int   num_points;
    } thread_result_t;
    
    void *worker(void *p) {
      thread_param_t *param = p;
      printf("Thread called with %d\n", param->num_points);
      free(param);
    
      // malloc here, main is responsible for free
      thread_result_t *result = malloc(sizeof(*result));
      result->num_points = 42;
      return result;
    }
    
    int main()
    {
      pthread_t tid;
    
      // malloc here, thread is responsible for free
      thread_param_t  *param = malloc(sizeof(*param));
      param->num_points = 1234;
    
      pthread_create(&tid,NULL,worker,param);
    
      void *p;
      pthread_join(tid,&p);
    
      thread_result_t *result = p;
      printf("Got result from thread=%d\n", result->num_points);
      free(result);
    
    }
    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
    Registered User
    Join Date
    Jun 2020
    Posts
    2
    This was supposed to be the title, sorry - Run a program on multithreads and compare speed!


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, done.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2020
    Posts
    1
    it that enough

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by Kenny844 View Post
    it that enough
    Is what enough?
    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. Multithreads for windows
    By Jablan in forum C Programming
    Replies: 3
    Last Post: 03-09-2014, 05:38 AM
  2. how to speed the program with bcb?
    By orcher in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2004, 09:50 PM
  3. Completion Port and Multithreads :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 11-06-2002, 11:37 PM
  4. multithreads
    By JagWire in forum C Programming
    Replies: 1
    Last Post: 06-28-2002, 11:22 AM
  5. Multithreads & Pointer to bool :: MFC
    By kuphryn in forum Windows Programming
    Replies: 7
    Last Post: 06-26-2002, 10:50 AM

Tags for this Thread