|
|
1 program fork_join {
2 integer thread1 = 1;
3 integer thread2 = 2;
4 integer thread3 = 3;
5 printf("----------JOIN ALL---------\n");
6 fork
7 {
8 delay(400) ;
9 printf("[%0d] thread=%0d\n", get_time(LO),thread1);
10 }
11 {
12 delay(200) ;
13 printf("[%0d] thread=%0d\n", get_time(LO), thread2);
14 }
15 {
16 delay(100) ;
17 printf("[%0d] thread=%0d\n", get_time(LO), thread3);
18 }
19 join all
20 printf("[%0d] After join all\n", get_time(LO) );
21 printf("----------JOIN ANY---------\n");
22 fork
23 {
24 delay(400) ;
25 printf("[%0d] thread=%0d\n", get_time(LO),thread1);
26 }
27 {
28 delay(200) ;
29 printf("[%0d] thread=%0d\n", get_time(LO), thread2);
30 }
31 {
32 delay(100) ;
33 printf("[%0d] thread=%0d\n", get_time(LO), thread3);
34 }
35 join any
36 printf("[%0d] After join any\n", get_time(LO) );
37 delay (400);
38 printf("----------JOIN NONE---------\n");
39 fork
40 {
41 delay(400) ;
42 printf("[%0d] thread=%0d\n", get_time(LO),thread1);
43 }
44 {
45 delay(200) ;
46 printf("[%0d] thread=%0d\n", get_time(LO), thread2);
47 }
48 {
49 delay(100) ;
50 printf("[%0d] thread=%0d\n", get_time(LO), thread3);
51 }
52 join none
53 printf("[%0d] After join none\n", get_time(LO) );
54 delay (400);
55 printf("[%0d] End of program\n", get_time(LO) ) ;
56 }
You could download file fork_join.vr here
|
|
|
By default, all child processes have access to the parent´s variables. However, if multiple processes independently use the same variable, races can occur. To avoid races within fork/join blocks, shadow variables should be used. |