Changes between Version 14 and Version 15 of Challenge


Ignore:
Timestamp:
03/12/19 11:23:41 (7 years ago)
Author:
siegel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Challenge

    v14 v15  
    146146concurrently in separate threads.  Used to avoid deadlocks that could
    147147result if all processes do `MPI_Send; MPI_Recv`.
     148
     149
     150== A two-thread barrier using semaphores in CIVL-C ==
     151
     152{{{
     153#include <stdio.h>
     154
     155int s1=0, s2=0;
     156#define sem_wait(s) $when (s>0) s--;
     157#define sem_post(s) s++;
     158const int N=10;
     159int i1=0, i2=0;
     160
     161void f1() {
     162  while (i1<N) {
     163    printf("thread 1 at iteration %d\n", i1);
     164    fflush(stdout);
     165    i1++;
     166    sem_post(s1);
     167    sem_wait(s2);
     168    $assert(i1==i2);
     169    sem_post(s1);
     170    sem_wait(s2);
     171  }
     172}
     173
     174void f2() {
     175  while (i2<N) {
     176    printf("thread 2 at iteration %d\n", i2);
     177    fflush(stdout);
     178    i2++;
     179    sem_post(s2);
     180    sem_wait(s1);
     181    $assert(i1==i2);
     182    sem_post(s2);
     183    sem_wait(s1);
     184  }
     185}
     186
     187int main() {
     188  $proc t1 = $spawn f1(), t2 = $spawn f2();
     189  $wait(t1);
     190  $wait(t2);
     191}
     192}}}