ADT
        aaron-thesis
        arm-eh
        ast-experimental
        cleanup-dtors
        deferred_resn
        demangler
        enum
        forall-pointer-decay
        jacob/cs343-translation
        jenkins-sandbox
        new-ast
        new-ast-unique-expr
        no_list
        persistent-indexer
        pthread-emulation
        qualifiedEnum
      
      
        
          | Last change
 on this file since 303c61d was             b002261, checked in by Thierry Delisle <tdelisle@…>, 8 years ago | 
        
          | 
Busy program now supports variable number of threads
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.3 KB | 
      
      
| Rev | Line |  | 
|---|
| [2c39855] | 1 | #include <stdbool.h> | 
|---|
| [b002261] | 2 | #include <stdio.h> | 
|---|
| [2c39855] | 3 | #include <stdlib.h> | 
|---|
|  | 4 | #include <pthread.h> | 
|---|
|  | 5 |  | 
|---|
|  | 6 | struct S { | 
|---|
|  | 7 | int i, j, k, l, m, n; | 
|---|
|  | 8 | }; | 
|---|
|  | 9 |  | 
|---|
|  | 10 | void * CallingMalloc( void * arg __attribute__((unused)) ) { | 
|---|
|  | 11 | for ( volatile int i = 0; i < 10000; i += 1 ) { | 
|---|
|  | 12 | volatile int      * ip  = (volatile int      *)malloc( sizeof(   int  ) *   1 ); | 
|---|
|  | 13 | volatile int      * aip = (volatile int      *)malloc( sizeof(   int  ) * 100 ); | 
|---|
|  | 14 | volatile struct S * sp  = (volatile struct S *)malloc( sizeof(struct S) * 200 ); | 
|---|
|  | 15 |  | 
|---|
|  | 16 | sp->i = 0; | 
|---|
|  | 17 | sp->j = 1; | 
|---|
|  | 18 | sp->k = 2; | 
|---|
|  | 19 | sp->l = 3; | 
|---|
|  | 20 | sp->m = 4; | 
|---|
|  | 21 | sp->n = 5; | 
|---|
|  | 22 | for(int j = 0; j < 100; j++) { | 
|---|
|  | 23 | aip[j] = j; | 
|---|
|  | 24 | } | 
|---|
|  | 25 | *ip = i; | 
|---|
|  | 26 |  | 
|---|
|  | 27 | free( (void*)sp ); | 
|---|
|  | 28 | free( (void*)aip ); | 
|---|
|  | 29 | free( (void*)ip ); | 
|---|
|  | 30 | } | 
|---|
|  | 31 | return NULL; | 
|---|
|  | 32 | } | 
|---|
|  | 33 |  | 
|---|
| [b002261] | 34 | int main(int argc, const char * const argv[]) { | 
|---|
|  | 35 | char * endptr; | 
|---|
|  | 36 | int nThreads = 15; | 
|---|
|  | 37 | switch(argc) { | 
|---|
|  | 38 | case 1: | 
|---|
|  | 39 | break; | 
|---|
|  | 40 | case 2: | 
|---|
|  | 41 | nThreads = strtol(argv[1], &endptr, 10); | 
|---|
|  | 42 | if( *endptr != 0 || nThreads <= 0 ) { | 
|---|
|  | 43 | fprintf(stderr, "Invalid number of threads %s\n", argv[1]); | 
|---|
|  | 44 | return 1; | 
|---|
|  | 45 | } | 
|---|
|  | 46 | break; | 
|---|
|  | 47 | default: | 
|---|
|  | 48 | fprintf(stderr, "Usage: %s [no of threads]\n", argv[0]); | 
|---|
|  | 49 | return 1; | 
|---|
|  | 50 | } | 
|---|
|  | 51 | printf("Running %d threads\n", nThreads); | 
|---|
|  | 52 | while(true) { | 
|---|
|  | 53 | pthread_t t[nThreads]; | 
|---|
|  | 54 | for(int i = 0; i < nThreads; i++) { | 
|---|
|  | 55 | pthread_create( &t[i], NULL, CallingMalloc, NULL ); | 
|---|
|  | 56 | } | 
|---|
| [2c39855] | 57 |  | 
|---|
| [b002261] | 58 | for(int i = 0; i < nThreads; i++) { | 
|---|
|  | 59 | pthread_join( t[i], NULL ); | 
|---|
|  | 60 | } | 
|---|
|  | 61 | } | 
|---|
|  | 62 | return 0; | 
|---|
| [2c39855] | 63 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.