Checkpoint synchronization: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 187: | Line 187: | ||
D ends shift |
D ends shift |
||
</pre> |
</pre> |
||
=={{header|C}}== |
|||
Using OpenMP. Compiled with <code>gcc -Wall -fopenmp</code>. |
|||
<lang C>#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <unistd.h> |
|||
#include <omp.h> |
|||
int main() |
|||
{ |
|||
int jobs = 41, tid; |
|||
omp_set_num_threads(5); |
|||
#pragma omp parallel shared(jobs) private(tid) |
|||
{ |
|||
tid = omp_get_thread_num(); |
|||
while (jobs > 0) { |
|||
/* this is the checkpoint */ |
|||
#pragma omp barrier |
|||
if (!jobs) break; |
|||
printf("%d: taking job %d\n", tid, jobs--); |
|||
usleep(100000 + rand() / (double) RAND_MAX * 3000000); |
|||
printf("%d: done job\n", tid); |
|||
} |
|||
printf("[%d] leaving\n", tid); |
|||
/* this stops jobless thread from exiting early and killing workers */ |
|||
#pragma omp barrier |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 308: | Line 342: | ||
} |
} |
||
interp.waitAtTop(promiseAllFulfilled(waits))</lang> |
interp.waitAtTop(promiseAllFulfilled(waits))</lang> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
As of February 2011, Go has checkpoint synchronization in the standard library, with a type called WaitGroup in the package sync. Code below uses this feature and completes the task with the workshop scenario, including workers joining and leaving. |
As of February 2011, Go has checkpoint synchronization in the standard library, with a type called WaitGroup in the package sync. Code below uses this feature and completes the task with the workshop scenario, including workers joining and leaving. |