source: CIVL/examples/pthread/join.cvl@ 8090425

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

Updated headers for examples

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

  • Property mode set to 100644
File size: 2.2 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
19$input int NUM_THREADS;
20
21void *BusyWork(void *t)
22{
23 int i;
24 long tid;
25 double result=0.0;
26 tid = (long)*t;
27 printf("Thread %d starting...\n",tid); // Removed l from %ld, unsupported
28 for (i=0; i<1000000; i++)
29 {
30 result = result + sin(i) * tan(i);
31 }
32 printf("Thread %d done. Result = %e\n",tid, result); // Removed l from %ld, unsupported
33 pthread_exit((void*) t);
34}
35
36int main (int argc, char *argv[])
37{
38 pthread_t thread[NUM_THREADS];
39 pthread_attr_t attr;
40 int rc;
41 long t;
42 void *status;
43
44 /* Initialize and set thread detached attribute */
45 pthread_attr_init(&attr);
46 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
47
48 for(t=0; t<NUM_THREADS; t++) {
49 printf("Main: creating thread %ld\n", t);
50 rc = pthread_create(&thread[t], &attr, BusyWork, (void *)&t); //Added address as conversion from
51 if (rc) { // int directly to void * is unsupported
52 $assert(false, "ERROR; return code from pthread_create() is %d", rc);
53 }
54 }
55
56 /* Free attribute and wait for the other threads */
57 pthread_attr_destroy(&attr);
58 for(t=0; t<NUM_THREADS; t++) {
59 rc = pthread_join(thread[t], &status);
60 if (rc) {
61 $assert(false, "ERROR; return code from pthread_create() is %d", rc);
62 }
63 printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
64 }
65
66printf("Main: program completed. Exiting.\n");
67pthread_exit(NULL);
68}
69
70
Note: See TracBrowser for help on using the repository browser.