source: CIVL/examples/pthread/join.cvl@ 37bfb99

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

Updated examples and pthread.cvh

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1039 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#include "pthread.cvh"
15#include <civlc.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include "math.cvh"
19
20$input int NUM_THREADS;
21
22void *BusyWork(void *t)
23{
24 int i;
25 long tid;
26 double result=0.0;
27 tid = (long)*t;
28 printf("Thread %d starting...\n",tid); // Removed l from %ld, unsupported
29 for (i=0; i<100; i++)
30 {
31 result = result + sin(i) * tan(i);
32 }
33 printf("Thread %d done. Result = %e\n",tid, result); // Removed l from %ld, unsupported
34 pthread_exit((void*) t, false, NULL, 0);
35}
36
37int main (void)
38{
39 pthread_t thread[NUM_THREADS];
40 pthread_attr_t attr;
41 int rc;
42 long j[NUM_THREADS];
43 long t;
44 void *status;
45
46 /* Initialize and set thread detached attribute */
47 pthread_attr_init(&attr);
48 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
49
50 for(t=0; t<NUM_THREADS; t++) {
51 printf("Main: creating thread %d\n", t);
52 j[t] = (long)t;
53 rc = pthread_create(&thread[t], &attr, BusyWork, (void *)&j[t]); //Added address as conversion from
54 if (rc) { // int directly to void * is unsupported
55 $assert(false, "ERROR; return code from pthread_create() is %d", rc);
56 }
57 }
58
59 /* Free attribute and wait for the other threads */
60 pthread_attr_destroy(&attr);
61 for(t=0; t<NUM_THREADS; t++) {
62 rc = pthread_join(thread[t], &status);
63 if (rc) {
64 $assert(false, "ERROR; return code from pthread_create() is %d", rc);
65 }
66 //printf("Main: completed join with thread %d having a status of %d\n",t,(long)*status);
67 //Will emulate passing value of status later
68 }
69
70printf("Main: program completed. Exiting.\n");
71pthread_exit(NULL, false, NULL, 0);
72}
73
74
Note: See TracBrowser for help on using the repository browser.