Thread: convert simple c code to multithreading.

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    13

    convert simple c code to multithreading.

    When we have code in c with 3,4 functions (for example for mathematical operations) how can we make these functions to be threads?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    What have you tried so far? What results have you received from attempts to search the web for information about multithreaded programs?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    i find some threads for example thread with consumer and producter or some multithreading with repeating messages but not with different functions.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Do you understand the basic concepts of creating, joining, blocking, and waking threads?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    no , i am new in programming.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    New to programming in general, or just new to threads?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    Look at POSIX threads (pthread).

    https://computing.llnl.gov/tutorials/pthreads/

    You can start by creating your own example prototypes following these examples:

    Multithreading in C

  8. #8
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    i try to write a code but i make mistakes.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    
    
    
    int sum_array(int LengthOfArray,int*TheArray){
    int sum=0;
    int i;
    for(i=0;i<LengthOfArray;i++)
        {
    sum=sum+TheArray[i];
    
    
    }
    return (sum);
    }
    
    
    int prod_array(int LengthOfArray,int*TheArray){
    int prod=1;
    int i;
    for(i=0;i<LengthOfArray;i++)
        {
    prod=prod*TheArray[i];
    
    
    }
    return (prod);
    }
    
    main()
    {
        int i,N,a1,a2,a3,*array;
            printf("Enter number of elements:");
            scanf("%d",&N);
            array=(int*)malloc(N*sizeof(int));
             if(array==NULL)
        {
    
    
            exit(0);
        }
    
    
    
    
           srand(time(NULL));
    
    
            for(i=0;i<N;i++){
                    array[i]=(rand()%201+ (-100));
                    printf("%d\n", array[i]);}
    pthread_t thread1, thread2;
    
    
    pthread_create( &thread1, NULL, &sum_array, NULL);
    pthread_create( &thread2, NULL, &prod_array, NULL);
    
    pthread_join( thread1, NULL);
    pthread_join( thread2, NULL);
    
    
    
    
    
    a1=sum_array(N,array);
    a2=prod_array(N,array);
    printf("the sum is :%d",a1);
    printf("\nthe product is :%d",a2);
    
    
    
    
    free(array);
    }

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You can't declare the thread function any way you want. It must be declared as taking a void* and returning a void*:
    Code:
    void *your_thread_function(void *arg) {
    }
    So you can only pass it a single pointer. Passing in a pointer to a struct allows you to pass in anything you want, including a place to put a "return value".

    An alternative way to pass back a return value is to have the thread function malloc up whatever you want (including a single integer, I guess) and return the poniter through the return statement. It is then up to the caller to free the memory. (It could also potentially return the address of a static local variable.)

    Here's an example of the former technique (return value in the input struct) :
    Code:
    // gcc -Wall thread01.c -pthread
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    typedef struct ThreadData {
      pthread_t th;
      int *array;
      int length;
      int result;
    } ThreadData;
    
    void *sum_array(void *arg) {
      ThreadData *ptd = (ThreadData *)arg;
      int *ar = ptd->array;
      int i, sum = 0;
      for (i = 0; i < ptd->length; i++) sum += ar[i];
      ptd->result = sum;
      return NULL; // not using this
    }
    
    int main() {
      int i, length = 100;
      int *array = malloc(length * sizeof *array);
      if (!array) return EXIT_FAILURE;
      for (i = 0; i < length; i++) array[i] = i;
    
      ThreadData td[2];
      td[0].array = array;  td[0].length = length;
      td[1].array = array;  td[1].length = length;
    
      pthread_create(&td[0].th, NULL, sum_array, &td[0]);
      pthread_create(&td[1].th, NULL, sum_array, &td[1]);
      pthread_join(td[0].th, NULL);
      pthread_join(td[1].th, NULL);
    
      printf("%d\n", td[0].result);
      printf("%d\n", td[1].result);
    
      free(array);
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    but for more functions is more complicated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    
    typedef struct ThreadData {
      pthread_t th;
      int *array;
      int length;
      int result;
    } ThreadData;
    
    
    void *sum_array(void *arg) {
      ThreadData *ptd = (ThreadData *)arg;
      int *ar = ptd->array;
      int i, sum = 0;
      for (i = 0; i < ptd->length; i++) sum += ar[i];
      ptd->result = sum;
      return NULL; // not using this
    }
    
    
    void *prod_array(void*arg)
    {
        ThreadData *ptd= (ThreadData *)arg;
        int *ar=ptd->array;
      int i, prod = 0;
      for (i = 0; i < ptd->length; i++) sum *= ar[i];
      ptd->result = prod;
    }
    
    
    void *prod_array(void*arg)
    {
        ThreadData *ptd= (ThreadData *)arg;
        int *ar=ptd->array;
      int i, prod = 0;
      for (i = 0; i < ptd->length; i++) sum *= ar[i];
      ptd->result = prod;
    }
    
    
    void *rand_number(void *arg){
     ThreadData *ptd= (ThreadData *)arg;   
    ptd->result=(rand()%(2001))+(-1000);
    }
    
    
    int main() {
      int i, length;
      printf("Enter number of elements:");
        scanf("%d",&length);
      int *array = malloc(length * sizeof *array);
      if (!array) return EXIT_FAILURE;
      for (i = 0; i < length; i++) array[i] = i;
    
    
      ThreadData td[2];
      td[0].array = array;  td[0].length = length;
      td[1].array = array;  td[1].length = length;
    
    
      pthread_create(&td[0].th, NULL, sum_array, &td[0]);
      pthread_create(&td[1].th, NULL, sum_array, &td[1]);
      pthread_join(td[0].th, NULL);
      pthread_join(td[1].th, NULL);
    
    
      printf("%d\n", td[0].result);
      printf("%d\n", td[1].result);
      
      ThreadData td[2];
      td[0].array = array;  td[0].length = length;
      td[1].array = array;  td[1].length = length;
    
    
      pthread_create(&td[0].th, NULL, prod_array, &td[0]);
      pthread_create(&td[1].th, NULL, prod_array, &td[1]);
      pthread_join(td[0].th, NULL);
      pthread_join(td[1].th, NULL);
    
    
      printf("%d\n", td[0].result);
      printf("%d\n", td[1].result);
    
    
      free(array);
      return 0;
    }

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by rot90 View Post
    but for more functions is more complicated.
    I assume that's your mindless way of saying "thank you".

    Anyway, I was just showing you the idea since you were completely clueless. It shouldn't be that much more "complicated" with more functions. You've definitely overcomplicated it, though (and still don't seem to understand it).

    My example was kind of dumb since it did exactly the same thing in each of the two threads. It would make more sense to pass half the array to one instantiation of sum_array and half to the other.

  12. #12
    Registered User
    Join Date
    Jan 2016
    Posts
    13
    intereresting this .you mean to have one random choice of some elements from the array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Simple multithreading problem
    By praised in forum C Programming
    Replies: 11
    Last Post: 08-14-2011, 01:03 PM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Help with simple multithreading.
    By Blackroot in forum C++ Programming
    Replies: 15
    Last Post: 02-03-2006, 08:02 AM
  5. Replies: 14
    Last Post: 11-23-2005, 08:53 AM