1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: bug2.cvl
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * A "hello world" Pthreads program that dumps core. Figure out why and
|
|---|
| 5 | * then fix it - or else see the solution bug2fix.c.
|
|---|
| 6 | * AUTHOR: 9/98 Blaise Barney
|
|---|
| 7 | * LAST REVISED: 01/29/09
|
|---|
| 8 | ******************************************************************************/
|
|---|
| 9 | #include "pthread.cvh"
|
|---|
| 10 | #include <civlc.h>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdlib.h>
|
|---|
| 13 | #define NTHREADS 8
|
|---|
| 14 | #define ARRAY_SIZE 500000
|
|---|
| 15 |
|
|---|
| 16 | void *Hello(void *threadid)
|
|---|
| 17 | {
|
|---|
| 18 | double A[ARRAY_SIZE];
|
|---|
| 19 | int i;
|
|---|
| 20 | long tid = (long)*threadid;
|
|---|
| 21 | //sleep(3);
|
|---|
| 22 | for (i=0; i<ARRAY_SIZE; i++)
|
|---|
| 23 | {
|
|---|
| 24 | A[i] = i * 1.0;
|
|---|
| 25 | }
|
|---|
| 26 | printf("%d: Hello World! %f\n", tid, A[ARRAY_SIZE-1]);
|
|---|
| 27 | pthread_exit(NULL);
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | int main(int argc, char *argv[])
|
|---|
| 31 | {
|
|---|
| 32 | pthread_t threads[NTHREADS];
|
|---|
| 33 | size_t stacksize;
|
|---|
| 34 | pthread_attr_t attr;
|
|---|
| 35 | int rc;
|
|---|
| 36 | long t;
|
|---|
| 37 | pthread_attr_init(&attr);
|
|---|
| 38 | pthread_attr_getstacksize (&attr, &stacksize);
|
|---|
| 39 | printf("Thread stack size = %d bytes (hint, hint)\n",stacksize);
|
|---|
| 40 | for(t=0;t<NTHREADS;t++){
|
|---|
| 41 | rc = pthread_create(&threads[t], NULL, Hello, (void *)&t);
|
|---|
| 42 | if (rc){
|
|---|
| 43 | printf("ERROR; return code from pthread_create() is %d\n", rc);
|
|---|
| 44 | exit(-1);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | printf("Created %d threads.\n", t);
|
|---|
| 48 | pthread_exit(NULL);
|
|---|
| 49 | return 0;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.