^ —————. —.— . . —.— . . .————— . .
——— | | | | | | || | | | |
—(o)— | | | | | | | | | |———— '————|
——————— | | | | | | | || | |
————————— —————' —'— ' —'— ' ' '————— '
home manual roadmap issues status papers download
| File main.c, 877 bytes (added by imartisko, 6 years ago) |
|
|
| Line | |
|---|
| 1 | #include<stdio.h> |
|---|
| 2 | #include<stdlib.h> |
|---|
| 3 | #include<pthread.h> |
|---|
| 4 | |
|---|
| 5 | void* mallocSomething (void* arg) |
|---|
| 6 | { |
|---|
| 7 | int** a = (int**) arg; |
|---|
| 8 | *a = malloc(sizeof(int)); |
|---|
| 9 | return NULL; |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | void* writeSomething (void* arg) |
|---|
| 13 | { |
|---|
| 14 | int* a = arg; |
|---|
| 15 | *a = 42; |
|---|
| 16 | return NULL; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | void* readSomething (void* arg) |
|---|
| 21 | { |
|---|
| 22 | int* a = arg; |
|---|
| 23 | printf ("The value is %d\n", *a); |
|---|
| 24 | return NULL; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | int main() |
|---|
| 28 | { |
|---|
| 29 | int* a = NULL; |
|---|
| 30 | pthread_t t1,t2,t3,t4; |
|---|
| 31 | int collectTids = 0; |
|---|
| 32 | if (collectTids == 0) |
|---|
| 33 | { |
|---|
| 34 | pthread_create(NULL, NULL, mallocSomething, &a); |
|---|
| 35 | pthread_create(NULL, NULL, writeSomething, a); |
|---|
| 36 | pthread_create(NULL, NULL, readSomething, a); |
|---|
| 37 | } |
|---|
| 38 | else |
|---|
| 39 | { |
|---|
| 40 | pthread_create(&t1, NULL, mallocSomething, &a); |
|---|
| 41 | pthread_create(&t2, NULL, writeSomething, a); |
|---|
| 42 | pthread_create(&t3, NULL, readSomething, a); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | return 0; |
|---|
| 46 | } |
|---|
| 47 | |
|---|