| 1 | /*****************************************************************************
|
|---|
| 2 | * SOURCE: This is a translation of a Pthread program from the Lawrence Livermore
|
|---|
| 3 | * Computing Center POSIX Threads Programming Exercise at:
|
|---|
| 4 | * https://computing.llnl.gov/tutorials/pthreads/exercise.html
|
|---|
| 5 | * FILE: bug5.cvl
|
|---|
| 6 | * DESCRIPTION:
|
|---|
| 7 | * A simple pthreads program that dies before the threads can do their
|
|---|
| 8 | * work because pthread_exit() is not called.
|
|---|
| 9 | * Command line execution:
|
|---|
| 10 | * civl verify -inputNUM_THREADS=5 bug5.cvl
|
|---|
| 11 | ******************************************************************************/
|
|---|
| 12 |
|
|---|
| 13 | #include "pthread.cvh"
|
|---|
| 14 | #include <civlc.h>
|
|---|
| 15 | #include <stdio.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 | #include "math.cvh"
|
|---|
| 18 |
|
|---|
| 19 | $input int NUM_THREADS;
|
|---|
| 20 |
|
|---|
| 21 | void *PrintHello(void *threadid)
|
|---|
| 22 | {
|
|---|
| 23 | int i;
|
|---|
| 24 | double myresult=0.0;
|
|---|
| 25 | printf("thread=%d: starting...\n", threadid); // Removed l from %ld
|
|---|
| 26 | for (i=0; i<1000; i++)
|
|---|
| 27 | myresult += sin(i) * tan(i);
|
|---|
| 28 | printf("thread=%d result=%e. Done.\n",threadid,myresult); // Removed l from %ld
|
|---|
| 29 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | int main(void)
|
|---|
| 33 | {
|
|---|
| 34 | pthread_t threads[NUM_THREADS];
|
|---|
| 35 | int rc, i;
|
|---|
| 36 | long t[NUM_THREADS];
|
|---|
| 37 |
|
|---|
| 38 | for(i=0;i<NUM_THREADS;i++){
|
|---|
| 39 | printf("Main: creating thread %d\n", t);
|
|---|
| 40 | //Add values into array and use address so that each value is unique as int to void * conversion is not supported
|
|---|
| 41 | t[i] = (long)i;
|
|---|
| 42 | rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&t[i]);
|
|---|
| 43 | if (rc){
|
|---|
| 44 | printf("ERROR; return code from pthread_create() is %d\n", rc);
|
|---|
| 45 | exit(-1);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | //pthread_exit(NULL, true, threads, NUM_THREADS); // This is the bug, no pthread_exit causes error
|
|---|
| 49 | printf("Main: Done.\n");
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|