| 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 | * SUPERSOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
|
|---|
| 6 | * et al. O'Reilly and Associates.
|
|---|
| 7 | * FILE: bug3.cvl
|
|---|
| 8 | * DESCRIPTION:
|
|---|
| 9 | * This "hello world" Pthreads program demonstrates an unsafe (incorrect)
|
|---|
| 10 | * way to pass thread arguments by address at thread creation resulting in
|
|---|
| 11 | * a non-unique value for each thread's argument.
|
|---|
| 12 | * Command line execution:
|
|---|
| 13 | * civl verify -inputNUM_THREADS=8 bug3.cvl
|
|---|
| 14 | ******************************************************************************/
|
|---|
| 15 |
|
|---|
| 16 | #include "pthread.cvh"
|
|---|
| 17 | #include <civlc.h>
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 |
|
|---|
| 21 | $input int NUM_THREADS;
|
|---|
| 22 |
|
|---|
| 23 | void *PrintHello(void *threadid){
|
|---|
| 24 | long taskid = (long)*threadid; // Dereference rather than direct conv
|
|---|
| 25 |
|
|---|
| 26 | //sleep(1);
|
|---|
| 27 | printf("Hello from thread %d\n", taskid);
|
|---|
| 28 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | int main(void){
|
|---|
| 32 | pthread_t threads[NUM_THREADS];
|
|---|
| 33 | int rc;
|
|---|
| 34 | long t;
|
|---|
| 35 |
|
|---|
| 36 | for(t=0;t<NUM_THREADS;t++) {
|
|---|
| 37 | printf("Creating thread %d\n", t);
|
|---|
| 38 | rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&t);
|
|---|
| 39 | if (rc) {
|
|---|
| 40 | $assert(false, "ERROR; return code from pthread_create() is %d", rc);
|
|---|
| 41 | return 0;
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | pthread_exit(NULL,true, threads, NUM_THREADS); //Different parameters
|
|---|
| 45 | return 0;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|