source: CIVL/examples/pthread/detached.cvl@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b 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
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: detached.cvl
[38374b7]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
[8090425]9* if the thread never needs to join later. Compare with the join.cvl program
[38374b7]10* where the threads are created joinable.
[8090425]11* Command line execution:
12* civl verify -inputNUM_THREADS=4 detached.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{
[37bfb99]24 long i,tid;
[38374b7]25 double result=0.0;
[ca52517]26 tid = (long)*t;
27 printf("Thread %d starting...\n",tid);
[38374b7]28 for (i=0; i<1000000; i++) {
29 result = result + sin(i) * tan(i);
30 }
[ca52517]31 printf("Thread %d done. Result = %e\n",tid, result);
[37bfb99]32 pthread_exit(NULL, false, NULL, 0);
[38374b7]33}
34
[37bfb99]35int main(void)
[38374b7]36{
37pthread_t thread[NUM_THREADS];
38pthread_attr_t attr;
[37bfb99]39int rc;
40long t;
41long j[NUM_THREADS];
[38374b7]42
43/* Initialize and set thread detached attribute */
44pthread_attr_init(&attr);
45pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
46
[37bfb99]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]);
[38374b7]51 if (rc) {
[37bfb99]52 $assert($false, "ERROR; return code from pthread_create() is %d", rc);
[ca52517]53 return 0;
[38374b7]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");
[37bfb99]64pthread_exit(NULL, true, thread, NUM_THREADS);
[38374b7]65}
66
Note: See TracBrowser for help on using the repository browser.