2. Common Code, continued

Worker function compatible with POSIX threads.

In the simulated workload, f sleeps for size seconds, where size varies from 3 to 6.

In a real workload, f could return early from the work loop if it successfully completes the task, e.g. it finds the desired hash or password.

void *f( void *v) // worker function
{
  int i = *(int *) v;

  printf( "f %i start = %u, size = %u\n", i, D[i].start, D[i].size);

  while( D[i].size > 0) { sleep(1); --D[i].size; } // simulate work

  printf( "f %i done\n", i);

  return 0;
}

int main( void)
{
 // initialize data
 //
  for( int i = 0; i < N; ++i)
  {
    D[i].i = i; D[i].start = 10*i; D[i].size = 6-i; // simulate parameters
  }