When we have code in c with 3,4 functions (for example for mathematical operations) how can we make these functions to be threads?
When we have code in c with 3,4 functions (for example for mathematical operations) how can we make these functions to be threads?
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?
i find some threads for example thread with consumer and producter or some multithreading with repeating messages but not with different functions.
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?
no , i am new in programming.
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?
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
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); }
You can't declare the thread function any way you want. It must be declared as taking a void* and returning a void*:
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".Code:void *your_thread_function(void *arg) { }
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; }
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; }
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.
intereresting this .you mean to have one random choice of some elements from the array?