Changeset b002261


Ignore:
Timestamp:
Mar 1, 2018, 9:28:48 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
dcbb03b
Parents:
807d8c3
Message:

Busy program now supports variable number of threads

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/busy.c

    r807d8c3 rb002261  
    11#include <stdbool.h>
     2#include <stdio.h>
    23#include <stdlib.h>
    34#include <pthread.h>
     
    3132}
    3233
    33 int main() {
    34     while(true) {
    35             pthread_t t[15];
    36             for(int i = 0; i < 15; i++) {
    37                     pthread_create( &t[i], NULL, CallingMalloc, NULL );
    38             }
     34int 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                }
    3957
    40             for(int i = 0; i < 15; i++) {
    41                     pthread_join( t[i], NULL );
    42             }
    43     }
     58                for(int i = 0; i < nThreads; i++) {
     59                        pthread_join( t[i], NULL );
     60                }
     61        }
     62        return 0;
    4463}
Note: See TracChangeset for help on using the changeset viewer.