z3
unknown
c_cpp
4 years ago
988 B
16
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
// 1, 2, 3, 4, 6, 8, 10, 12, 14, 16 parz
// 1, 2, 3, 5, 7, 9, 11, 13, 15, 16 nieparz
struct Shared
{
int a;
int b;
};
void * thread1 (void * shared)
{
struct Shared * self = (struct Shared *) shared;
int * res = malloc(sizeof(int));
*res = self->a + self->b;
return (void *) res;
}
void * thread2 (void * shared)
{
struct Shared * self = (struct Shared *) shared;
int * res = malloc(sizeof(int));
*res = self->a - self->b;
return (void *) res;
}
int main (void)
{
pthread_t threads[2];
struct Shared shared;
shared.a = 10;
shared.b = 20;
void * rets[2];
pthread_create(&threads[0], NULL, thread1, &shared);
pthread_create(&threads[1], NULL, thread2, &shared);
int i;
for (i = 0; i < 2; i++)
pthread_join(threads[i], &(rets[i]));
for (i = 0; i < 2; i++)
printf("Watek %d zwrocil: %d\n", i + 1, *((int*)(rets[i])));
for (i = 0; i < 2; i++)
free(rets[i]);
return 0;
}
Editor is loading...