| 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: join.cvl
|
|---|
| 6 | * DESCRIPTION:
|
|---|
| 7 | * This example demonstrates how to "wait" for thread completions by using
|
|---|
| 8 | * the Pthread join routine. Threads are explicitly created in a joinable
|
|---|
| 9 | * state for portability reasons. Use of the pthread_exit status argument is
|
|---|
| 10 | * also shown. Compare to detached.c
|
|---|
| 11 | * Command line execution:
|
|---|
| 12 | * civl verify -inputNUM_THREADS=4 join.cvl
|
|---|
| 13 | ******************************************************************************/
|
|---|
| 14 |
|
|---|
| 15 | #include "pthread.cvh"
|
|---|
| 16 | #include <civlc.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <stdlib.h>
|
|---|
| 19 | #include "math.cvh"
|
|---|
| 20 |
|
|---|
| 21 | $input int NUM_THREADS;
|
|---|
| 22 |
|
|---|
| 23 | void *BusyWork(void *t)
|
|---|
| 24 | {
|
|---|
| 25 | int i;
|
|---|
| 26 | long tid;
|
|---|
| 27 | double result=0.0;
|
|---|
| 28 | tid = (long)*t; // Dereference rather than direct conv
|
|---|
| 29 |
|
|---|
| 30 | printf("Thread %d starting...\n",tid); // Removed l from %ld, unsupported
|
|---|
| 31 | for (i=0; i<100; i++){
|
|---|
| 32 | result = result + sin(i) * tan(i);
|
|---|
| 33 | }
|
|---|
| 34 | printf("Thread %d done. Result = %e\n",tid, result); // Removed l from %ld, unsupported
|
|---|
| 35 | pthread_exit((void*) t, false, NULL, 0); //Different parameters
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | int main (void)
|
|---|
| 39 | {
|
|---|
| 40 | pthread_t thread[NUM_THREADS];
|
|---|
| 41 | pthread_attr_t attr;
|
|---|
| 42 | int rc;
|
|---|
| 43 | long j[NUM_THREADS];
|
|---|
| 44 | long t;
|
|---|
| 45 | void *status;
|
|---|
| 46 |
|
|---|
| 47 | /* Initialize and set thread detached attribute */
|
|---|
| 48 | pthread_attr_init(&attr);
|
|---|
| 49 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 50 |
|
|---|
| 51 | for(t=0; t<NUM_THREADS; t++) {
|
|---|
| 52 | printf("Main: creating thread %d\n", t);
|
|---|
| 53 | j[t] = (long)t;
|
|---|
| 54 | rc = pthread_create(&thread[t], &attr, BusyWork, (void *)&j[t]); //Added address as conversion from
|
|---|
| 55 | if (rc) { // int directly to void * is unsupported
|
|---|
| 56 | $assert(false, "ERROR; return code from pthread_create() is %d", rc);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /* Free attribute and wait for the other threads */
|
|---|
| 61 | pthread_attr_destroy(&attr);
|
|---|
| 62 | for(t=0; t<NUM_THREADS; t++) {
|
|---|
| 63 | rc = pthread_join(thread[t], &status);
|
|---|
| 64 | if (rc) {
|
|---|
| 65 | $assert(false, "ERROR; return code from pthread_create() is %d", rc);
|
|---|
| 66 | }
|
|---|
| 67 | //printf("Main: completed join with thread %d having a status of %d\n",t,(long)*status);
|
|---|
| 68 | //Will emulate passing value of status later
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | printf("Main: program completed. Exiting.\n");
|
|---|
| 72 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 73 | }
|
|---|