| 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 at thread creation. Compare with hello_arg1.c.
|
|---|
| 11 | * Command line execution:
|
|---|
| 12 | * civl verify -inputNUM_THREADS=8 bug3.cvl
|
|---|
| 13 | ******************************************************************************/
|
|---|
| 14 | #include "pthread.cvh"
|
|---|
| 15 | #include <civlc.h>
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 | #include <stdlib.h>
|
|---|
| 18 |
|
|---|
| 19 | $input int NUM_THREADS;
|
|---|
| 20 |
|
|---|
| 21 | void *PrintHello(void *threadid){
|
|---|
| 22 | long taskid = (long)*threadid;
|
|---|
| 23 | //sleep(1);
|
|---|
| 24 | printf("Hello from thread %d\n", taskid);
|
|---|
| 25 | pthread_exit(NULL, false, NULL, 0);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | int main(void){
|
|---|
| 29 | pthread_t threads[NUM_THREADS];
|
|---|
| 30 | int rc;
|
|---|
| 31 | long t;
|
|---|
| 32 |
|
|---|
| 33 | for(t=0;t<NUM_THREADS;t++) {
|
|---|
| 34 | printf("Creating thread %d\n", t);
|
|---|
| 35 | rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&t);
|
|---|
| 36 | if (rc) {
|
|---|
| 37 | $assert(false, "ERROR; return code from pthread_create() is %d", rc);
|
|---|
| 38 | return 0;
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | pthread_exit(NULL,true, threads, NUM_THREADS);
|
|---|
| 42 | return 0;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|