/mobile Handheld Friendly website

 performance measurements

Each table row shows performance measurements for this C gcc program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
500,0003.502.664,620575  2% 48% 48% 4%
5,000,00034.0726.544,624575  39% 9% 9% 39%
50,000,000339.11264.294,620575  13% 33% 33% 12%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read thread-ring benchmark to see what this program should do.

 notes

gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8)

 thread-ring C gcc #2 program source code

/* The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Ricardo Garcia
 */

#include <assert.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define NUM_THREADS (503)

pthread_t threads[NUM_THREADS];   /* Threads. */
int names[NUM_THREADS];      /* Thread "names". */
long mailboxes[NUM_THREADS];   /* Read tokens from here. */
sem_t greenlights[NUM_THREADS];   /* Semaphores to signal token presence. */

void *ring_routine(void *myname)
{
   int name = *(int *)(myname);   /* My name (1..NUM_THREADS). */
   int index = name - 1;      /* My index for the arrays. */
   int next = (index + 1) % NUM_THREADS;
   long token;

   for (;;) {
      sem_wait(greenlights + index);   /* Wait for a new token. */
      token = mailboxes[index];   /* Get token. */
      if (token == 0) {      /* Game over. */
         printf("%d\n", name);
         exit(EXIT_SUCCESS);
      }
      mailboxes[next] = token - 1;   /* Decrement and pass. */
      sem_post(greenlights + next);   /* Green ligh for next thr. */
   }
   return NULL;
}

int main(int argc, char *argv[])
{
   long first_token;
   int ii;
   pthread_attr_t attributes;
   sem_t permanent_red;
   
   /* Read first token. */
   first_token = strtol(argv[1], NULL, 10);

   /* Initialize attributes. */
   pthread_attr_init(&attributes);
   pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
   pthread_attr_setstacksize(&attributes, PTHREAD_STACK_MIN);

   /* Initialize arrays and create threads. */
   for (ii = 0; ii < NUM_THREADS; ++ii) {
      names[ii] = ii + 1;
      sem_init(greenlights + ii, 0, 0);
      pthread_create(threads + ii, &attributes, ring_routine, names + ii);
   }

   /* Pass first token. */
   mailboxes[0] = first_token;
   sem_post(greenlights + 0);

   /* Wait forever. */
   sem_init(&permanent_red, 0, 0);
   sem_wait(&permanent_red);
   return 0;
}

 make, command-line, and program output logs

Sun, 20 Oct 2013 02:57:03 GMT

MAKE:
/usr/bin/gcc -pipe -Wall -O3 -fomit-frame-pointer -march=native -pthread threadring.gcc-2.c -o threadring.gcc-2.gcc_run 
rm threadring.gcc-2.c
0.09s to complete and log all make actions

COMMAND LINE:
./threadring.gcc-2.gcc_run 50000000

PROGRAM OUTPUT:
292

Revised BSD license

  Home   Conclusions   License   Play