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

1.23 2.0 acw/focus-triggers 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
RevLine 
[38374b7]1/*****************************************************************************
[8090425]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
[38374b7]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
[8090425]11* Command line execution:
12* civl verify -inputNUM_THREADS=4 join.cvl
[38374b7]13******************************************************************************/
14#include "pthread.cvh"
15#include <civlc.h>
16#include <stdio.h>
17#include <stdlib.h>
[37bfb99]18#include "math.cvh"
[8090425]19
20$input int NUM_THREADS;
[38374b7]21
22void *BusyWork(void *t)
23{
24 int i;
25 long tid;
26 double result=0.0;
[bf81b8c]27 tid = (long)*t;
28 printf("Thread %d starting...\n",tid); // Removed l from %ld, unsupported
[37bfb99]29 for (i=0; i<100; i++)
[38374b7]30 {
31 result = result + sin(i) * tan(i);
32 }
[bf81b8c]33 printf("Thread %d done. Result = %e\n",tid, result); // Removed l from %ld, unsupported
[37bfb99]34 pthread_exit((void*) t, false, NULL, 0);
[38374b7]35}
36
[37bfb99]37int main (void)
[38374b7]38{
39 pthread_t thread[NUM_THREADS];
40 pthread_attr_t attr;
41 int rc;
[37bfb99]42 long j[NUM_THREADS];
[38374b7]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++) {
[ca52517]51 printf("Main: creating thread %d\n", t);
[37bfb99]52 j[t] = (long)t;
53 rc = pthread_create(&thread[t], &attr, BusyWork, (void *)&j[t]); //Added address as conversion from
[bf81b8c]54 if (rc) { // int directly to void * is unsupported
[068c228]55 $assert(false, "ERROR; return code from pthread_create() is %d", rc);
[38374b7]56 }
57 }
58
59 /* Free attribute and wait for the other threads */
[068c228]60 pthread_attr_destroy(&attr);
[38374b7]61 for(t=0; t<NUM_THREADS; t++) {
62 rc = pthread_join(thread[t], &status);
63 if (rc) {
[068c228]64 $assert(false, "ERROR; return code from pthread_create() is %d", rc);
[38374b7]65 }
[37bfb99]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
[38374b7]68 }
69
70printf("Main: program completed. Exiting.\n");
[37bfb99]71pthread_exit(NULL, false, NULL, 0);
[38374b7]72}
73
74
Note: See TracBrowser for help on using the repository browser.