source: CIVL/examples/pthread/join.cvl@ 325d439

1.23 2.0 main test-branch
Last change on this file since 325d439 was 6fe2cd9, checked in by John Edenhofner <johneden@…>, 12 years ago

Almost done

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1040 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.4 KB
Line 
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
23void *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
38int 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}
Note: See TracBrowser for help on using the repository browser.