Mercurial > mxe-octave
annotate src/pthreads-test.c @ 982:4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
author | Volker Grabsch <vog@notjusthosting.com> |
---|---|
date | Sun, 16 May 2010 18:06:17 +0200 |
parents | |
children | e55bb62b2970 |
rev | line source |
---|---|
982
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
1 /* This file is part of mingw-cross-env. */ |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
2 /* See doc/index.html for further information. */ |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
3 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
4 #include <stdio.h> |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
5 #include <pthread.h> |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
6 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
7 #define N 10 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
8 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
9 void *thread_fn(void *p) |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
10 { |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
11 const int *arg = p; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
12 fprintf(stderr, "Hello from thread %d\n", *arg); |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
13 return NULL; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
14 } |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
15 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
16 int main(int argc, char *argv[]) |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
17 { |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
18 pthread_t threads[N]; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
19 int args[N]; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
20 int i; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
21 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
22 (void)argc; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
23 (void)argv; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
24 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
25 for (i = 0; i < N; i++) { |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
26 args[i] = i; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
27 if (pthread_create(threads + i, NULL, thread_fn, args + i) != 0) { |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
28 return 1; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
29 } |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
30 } |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
31 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
32 for (i = 0; i < N; i++) { |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
33 if (pthread_join(threads[i], NULL) != 0) { |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
34 return 2; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
35 } |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
36 } |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
37 |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
38 return 0; |
4d2e6c7c2cc2
test program for package pthreads (by Martin Lambers)
Volker Grabsch <vog@notjusthosting.com>
parents:
diff
changeset
|
39 } |