- Include Include Int Product This Data Is Shared By The Thread S Void Runner Void Param Threads Call T 1 (66.99 KiB) Viewed 17 times
#include #include int product;/ this data is shared by the thread (s) */ void *runner (void *param); /* threads call t
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
#include #include int product;/ this data is shared by the thread (s) */ void *runner (void *param); /* threads call t
#include #include int product;/ this data is shared by the thread (s) */ void *runner (void *param); /* threads call this function +/ typedef struct{ / data structure for passing data to threads */ int start; int end; ) parameters; int main(int argc, char *argv[]) { pthread_t workerl, worker2; product=1; parameters *data = (parameters *) malloc(sizeof (parameters)); data->start = 5; data->end = 10; pthread_create (&workerl, NULL, runner, data); data (parameters *) malloc (sizeof (parameters)); data->start = 11; data->end = 16; pthread_create (&worker2, NULL, runner, data); = pthread_join (workerl, NULL); pthread_join (worker2, NULL); printf("result- %d\n", product); return 0; } void *runner (void *params) { parameters* p = (parameters *)params; for (int i= p->start; i <= p->end; i++) product *= i; pthread_exit(0);