File: pth_mutex_initializer.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 (64 lines) | stat: -rw-r--r-- 1,486 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

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

#include <pthread.h>

typedef struct {
  long ninc_per_thread;
  long a;
  long b;
  long r;
  long * p;
  pthread_mutex_t * m;
} arg_t;

void * f(void * arg_) {
  arg_t * arg = (arg_t *)arg_;
  long a = arg->a, b = arg->b;
  long ninc_per_thread = arg->ninc_per_thread;
  if (b - a == 1) {
    int i;
    for (i = 0; i < ninc_per_thread; i++) {
      pthread_mutex_lock(arg->m);
      arg->p[0]++;
      pthread_mutex_unlock(arg->m);
    }
    arg->r = a;
  } else {
    long c = (a + b) / 2;
    arg_t cargs[2] = { { ninc_per_thread, a, c, 0, arg->p, arg->m }, 
		       { ninc_per_thread, c, b, 0, arg->p, arg->m } };
    pthread_t tid;
    pthread_create(&tid, 0, f, cargs);
    f(cargs + 1);
    pthread_join(tid, 0);
    arg->r = cargs[0].r + cargs[1].r;
  }
  return 0;
}

pthread_mutex_t m[1] = { PTHREAD_MUTEX_INITIALIZER };

int main(int argc, char ** argv) {
  long nthreads        = (argc > 1 ? atol(argv[1]) : 100);
  long ninc_per_thread = (argc > 2 ? atol(argv[2]) : 10000);

  long p[1] = { 0 };
  arg_t arg[1] = { { ninc_per_thread, 0, nthreads, 0, p, m } };
  pthread_t tid;
  pthread_create(&tid, 0, f, arg);
  pthread_join(tid, 0);

  if (arg->r == (nthreads - 1) * nthreads / 2
      && arg->p[0] == nthreads * ninc_per_thread) {
    printf("OK\n");
    return 0;
  } else {
    printf("NG: p = %ld != nthreads * ninc_per_thread = %ld\n",
	   arg->p[0], nthreads * ninc_per_thread);
    return 1;
  }
}