source: CIVL/examples/pthread/detached.cvl@ f6ce0eb

1.23 2.0 main test-branch
Last change on this file since f6ce0eb 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.0 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: detached.cvl
6* DESCRIPTION:
7* This example demonstrates how to explicitly create a thread in a
8* detached state. This might be done to conserve some system resources
9* if the thread never needs to join later. Compare with the join.cvl program
10* where the threads are created joinable.
11* Command line execution:
12* civl verify -inputNUM_THREADS=4 detached.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 long i,tid;
25 double result=0.0;
26 tid = (long)*t;
27 printf("Thread %d starting...\n",tid);
28 for (i=0; i<1000000; i++) {
29 result = result + sin(i) * tan(i);
30 }
31 printf("Thread %d done. Result = %e\n",tid, result);
32 pthread_exit(NULL, false, NULL, 0);
33}
34
35int main(void)
36{
37pthread_t thread[NUM_THREADS];
38pthread_attr_t attr;
39int rc;
40long t;
41long j[NUM_THREADS];
42
43/* Initialize and set thread detached attribute */
44pthread_attr_init(&attr);
45pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
46
47for(t=0;t<NUM_THREADS;t++) {
48 printf("Main: creating thread %d\n", t);
49 j[t] = (long)t;
50 rc = pthread_create(&thread[t], &attr, BusyWork, (void *)&j[t]);
51 if (rc) {
52 $assert($false, "ERROR; return code from pthread_create() is %d", rc);
53 return 0;
54 }
55 }
56
57/* We're done with the attribute object, so we can destroy it */
58pthread_attr_destroy(&attr);
59
60/* The main thread is done, so we need to call pthread_exit explicitly to
61* permit the working threads to continue even after main completes.
62*/
63printf("Main: program completed. Exiting.\n");
64pthread_exit(NULL, true, thread, NUM_THREADS);
65}
66
Note: See TracBrowser for help on using the repository browser.