File: pth_barrier.c

package info (click to toggle)
massivethreads 1.02-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,924 kB
  • sloc: ansic: 27,814; sh: 4,559; cpp: 3,334; javascript: 1,799; makefile: 1,745; python: 523; asm: 373; perl: 118; lisp: 9
file content (66 lines) | stat: -rw-r--r-- 1,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 
 * myth_barrier.c --- test barrier
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>

pthread_barrier_t b[1];

typedef struct {
  long id;
  long n_threads;
  long n;
  long * a;
} arg_t;

void * f(void * arg_) {
  arg_t * arg = (arg_t *)arg_;
  long id = arg->id;
  long n_threads = arg->n_threads;
  long n = arg->n;
  long * a = arg->a;
  long t;
  for (t = 0; t < n; t++) {
    a[id] = t + 1;
    pthread_barrier_wait(b);
    long j;
    for (j = 0; j < n_threads; j++) {
      assert(a[j] == t + 1);
    }
    pthread_barrier_wait(b);
  }
  return 0;
}

int main(int argc, char ** argv) {
  long n_threads = (argc > 1 ? atol(argv[1]) : 1000);
  long n         = (argc > 2 ? atol(argv[2]) : 1000);
  pthread_t tids[n_threads];
  long * a = (long *)calloc(sizeof(long), n_threads);
  arg_t * args = (arg_t *)calloc(sizeof(arg_t), n_threads);
  arg_t arg = { -1, n_threads, n, a };
  long i;
  pthread_barrier_init(b, 0, n_threads);
  for (i = 0; i < n_threads; i++) {
    args[i] = arg;
    args[i].id = i;
    int r = pthread_create(&tids[i], 0, f, &args[i]);
    assert(r == 0);
  }
  for (i = 0; i < n_threads; i++) {
    pthread_join(tids[i], 0);
  }
  for (i = 0; i < n_threads; i++) {
    if (a[i] != n) {
      printf("NG: a[%ld] = %ld != %ld\n", i, a[i], n);
      return 1;
    }
  }
  printf("OK\n");
  free(a);
  return 0;
}