示例#1
0
static void
wq_init(workqueue_t *wq, int nfiles)
{
	int throttle, nslots, i;

	if (getenv("CTFMERGE_MAX_SLOTS"))
		nslots = atoi(getenv("CTFMERGE_MAX_SLOTS"));
	else
		nslots = MERGE_PHASE1_MAX_SLOTS;

	if (getenv("CTFMERGE_PHASE1_BATCH_SIZE"))
		wq->wq_maxbatchsz = atoi(getenv("CTFMERGE_PHASE1_BATCH_SIZE"));
	else
		wq->wq_maxbatchsz = MERGE_PHASE1_BATCH_SIZE;

	nslots = MIN(nslots, (nfiles + wq->wq_maxbatchsz - 1) /
	    wq->wq_maxbatchsz);

	wq->wq_wip = xcalloc(sizeof (wip_t) * nslots);
	wq->wq_nwipslots = nslots;
	wq->wq_nthreads = MIN(sysconf(_SC_NPROCESSORS_ONLN) * 3 / 2, nslots);
	wq->wq_thread = xmalloc(sizeof (pthread_t) * wq->wq_nthreads);

	if (getenv("CTFMERGE_INPUT_THROTTLE"))
		throttle = atoi(getenv("CTFMERGE_INPUT_THROTTLE"));
	else
		throttle = MERGE_INPUT_THROTTLE_LEN;
	wq->wq_ithrottle = throttle * wq->wq_nthreads;

	debug(1, "Using %d slots, %d threads\n", wq->wq_nwipslots,
	    wq->wq_nthreads);

	wq->wq_next_batchid = 0;

	for (i = 0; i < nslots; i++) {
		pthread_mutex_init(&wq->wq_wip[i].wip_lock, NULL);
		wq->wq_wip[i].wip_batchid = wq->wq_next_batchid++;
	}

	pthread_mutex_init(&wq->wq_queue_lock, NULL);
	wq->wq_queue = fifo_new();
	pthread_cond_init(&wq->wq_work_avail, NULL);
	pthread_cond_init(&wq->wq_work_removed, NULL);
	wq->wq_ninqueue = nfiles;
	wq->wq_nextpownum = 0;

	pthread_mutex_init(&wq->wq_donequeue_lock, NULL);
	wq->wq_donequeue = fifo_new();
	wq->wq_lastdonebatch = -1;

	pthread_cond_init(&wq->wq_done_cv, NULL);

	pthread_cond_init(&wq->wq_alldone_cv, NULL);
	wq->wq_alldone = 0;

	barrier_init(&wq->wq_bar1, wq->wq_nthreads);
	barrier_init(&wq->wq_bar2, wq->wq_nthreads);

	wq->wq_nomorefiles = 0;
}
示例#2
0
文件: tas.c 项目: parmerasa-uau/tas
/**
 * Initialize the TAS thread pool -> create threads
 * @param workers Number of workers
 * */
void tas_init(int workers) {
	int i;

	logStart("tas_init");

	worker_total_count = workers;

#if TAS_POSIX == 0
	barrier_init(&worker_init_barrier, workers + 1);
	ticket_init(&worker_available_lock);
#else
	pthread_barrier_init(&worker_init_barrier, NULL, workers + 1);
#endif

#if TAS_DEBUG == 1
	PFL
	printf("MAIN - START tas_init with %i workers\n", workers);
	PFU
#endif

	// Initialize structures for threads
	for (i = 0; i < worker_total_count; i++) {
		worker_threads_data[i].do_shutdown = 0;
		worker_threads_data[i].work_assigned = 0;
		worker_threads_data[i].pattern_assigned = 0;
		worker_threads_data[i].thread_id = i;

		// Initialize barriers (n + 1)
#if TAS_POSIX == 1
		pthread_barrier_init(&(worker_threads_data[i].pattern_assigned_barrier_idle),
				NULL, 2);
		pthread_barrier_init(&(worker_threads_data[i].pattern_assigned_barrier),
				NULL, 2);
#else
		barrier_init(&(worker_threads_data[i].pattern_assigned_barrier), 2);
		barrier_init(&(worker_threads_data[i].pattern_assigned_barrier_idle), 2);
#endif
	}

	// Start worker threads
#if TAS_POSIX == 1
	for (i = 0; i < worker_total_count; i++) {
		pthread_create(&(thread_tasks[i]), NULL, tas_thread,
				&(worker_threads_data[i]));
	}
#else
	// Threads are started automatically
#endif

#if TAS_DEBUG == 1
	PFL
	printf("MAIN - END tas_init\n");
	PFU
#endif

	logEnd("tas_init");
}
/*------------------------------------------------------------------|
|               Functions' implementation (BEGIN)                   |
|------------------------------------------------------------------*/
my_bool oph_sum_array_r_init(UDF_INIT * initid, UDF_ARGS * args, char *message)
{
	/* oph_sum_array_r(measureA, measureB, OPH_TYPEA, OPH_TYPEB) */
	int i = 0;
	if (args->arg_count < 2 || args->arg_count > 4) {
		strcpy(message, "ERROR: oph_sum_array_r(measure_a, measure_b, [OPH_TYPE_a], [OPH_TYPE_b])");
		return 1;
	}

	if (args->arg_type[0] != STRING_RESULT || args->arg_type[1] != STRING_RESULT) {
		strcpy(message, "ERROR: Wrong arguments to oph_sum_array_r function");
		return 1;
	}

	if (args->arg_count > 2) {
		if (args->arg_type[2] != STRING_RESULT) {
			strcpy(message, "ERROR: Wrong arguments to oph_sum_array_r function");
			return 1;
		}
	}

	if (args->arg_count > 3) {
		if (args->arg_type[3] != STRING_RESULT) {
			strcpy(message, "ERROR: Wrong arguments to oph_sum_array_r function");
			return 1;
		}
	}

	initid->ptr = NULL;
	initid->extension = NULL;

	/* Plugin specific initializations */
	th_data *data_r = malloc(sizeof(th_data));
	data_r->curr_args = (void *) args;	// Used in thread function
	data_r->exit_flag = 0;
	if (barrier_init(&(data_r->barr_start), NTHREAD + 1)) {
		strcpy(message, "Could not create a barrier");
		return 1;
	}
	if (barrier_init(&(data_r->barr_end), NTHREAD + 1)) {
		strcpy(message, "Could not create a barrier");
		return 1;
	}
	initid->extension = (char *) data_r;
	for (i = 0; i < NTHREAD; i++) {
		pthread_create(&(data_r->thread[i]), NULL, sum_array_r, (void *) (initid));
	}


	return 0;
}
示例#4
0
文件: pcu_thread.c 项目: GKosiba/core
void pcu_run_threads(int count, pcu_thread* function)
{
  if (count < 1) pcu_fail("thread count must be positive");
  global_nthreads = count;
  PCU_MALLOC(global_threads,(size_t)count);
  *global_threads = pthread_self();
  barrier_init(&global_barrier, count);
  pthread_mutex_init(&global_lock, NULL);

  int err;
  err = pthread_key_create(&global_key,NULL);
  if (err) pcu_fail("pthread_key_create failed");
  pthread_setspecific(global_key,0);

  for (int i=1; i < count; ++i)
  {
    err = pthread_create(global_threads+i,NULL,function,(void*)(ptrdiff_t)i);
    if (err) pcu_fail("pthread_create failed");
  }

  function(NULL);
  for (int i=1; i < count; ++i)
  {
    err = pthread_join(global_threads[i],NULL);
    if (err) pcu_fail("pthread_join failed");
  }
  pthread_mutex_destroy(&global_lock);
  barrier_destroy(&global_barrier);

  err = pthread_key_delete(global_key);
  if (err) pcu_fail("pthread_key_delete failed");
  pcu_free(global_threads);
}
示例#5
0
/**
 * Package init and quit functions
 */
void
sylvan_init_package(size_t tablesize, size_t maxsize, size_t cachesize, size_t max_cachesize)
{
    if (tablesize > maxsize) tablesize = maxsize;
    if (cachesize > max_cachesize) cachesize = max_cachesize;

    if (maxsize > 0x000003ffffffffff) {
        fprintf(stderr, "sylvan_init_package error: tablesize must be <= 42 bits!\n");
        exit(1);
    }

    nodes = llmsset_create(tablesize, maxsize);
    cache_create(cachesize, max_cachesize);

    gc = 0;
    barrier_init(&gcbar, lace_workers());
#if SYLVAN_AGGRESSIVE_RESIZE
    gc_hook = TASK(sylvan_gc_aggressive_resize);
#else
    gc_hook = TASK(sylvan_gc_default_hook);
#endif
    sylvan_gc_add_mark(10, TASK(sylvan_gc_mark_cache));
    sylvan_gc_add_mark(19, TASK(sylvan_gc_destroy_unmarked));
    sylvan_gc_add_mark(20, TASK(sylvan_gc_call_hook));
    sylvan_gc_add_mark(30, TASK(sylvan_gc_rehash));

    LACE_ME;
    sylvan_stats_init();
}
示例#6
0
static void barrier_thread(int id)
{
    int i, j;

    if (0 == id) {
        barrier_init(&barrier, NUM_THREADS);
        barrier_initialized = TRUE;
    } else {
        while (!barrier_initialized) {
            do_yield();
        }
    }

    for (i = 0; i < NUM_ITERATIONS; ++i) {
        ++value[id];
        barrier_wait(&barrier);
        do_sleep(rand() % MAX_SLEEP);
        for (j = 0; j < NUM_THREADS; ++j) {
            ASSERT(value[j] == value[id]);
        }
        printf(LINE + id, COL, "Barrier thread %d: %d", do_getpid(),
               value[id]);
        barrier_wait(&barrier);
    }
    ASSERT(NUM_ITERATIONS == value[id]);
    printf(LINE + id, COL, "Barrier thread %d: Passed\t", do_getpid());
    do_exit();
}
示例#7
0
void initialise_cpus(void)
{
	int i;

	act_cpus = 0;

	if (maxcpus > 1) {
		smp_find_cpus();
		/* The total number of CPUs may be limited */
		if (num_cpus > maxcpus) {
			num_cpus = maxcpus;
		}
		/* Determine how many cpus have been selected */
		for(i = 0; i < num_cpus; i++) {
			if (cpu_mask[i]) {
				act_cpus++;
			}
		}
	} else {
		act_cpus = found_cpus = num_cpus = 1;
	}

	/* Initialize the barrier before starting AP's */
	barrier_init(act_cpus);

	/* let the BSP initialise the APs. */
	for(i = 1; i < num_cpus; i++) {
	    /* Only start this CPU if it is selected by the mask */
	    if (cpu_mask[i]) {
	        smp_boot_ap(i);
	    }
	}

}
示例#8
0
int main(int argc, char *argv[])
{
	gmactime_t s, t;
	setParam<unsigned>(&width, widthStr, widthDefault);
	setParam<unsigned>(&height, heightStr, heightDefault);
	setParam<unsigned>(&frames, framesStr, framesDefault);

	assert(eclCompileSource(kernel_code) == eclSuccess);

	gmac_sem_init(&s_quant.free, 0);
	gmac_sem_init(&s_idct.free, 0);

	srand(unsigned(time(NULL)));

	getTime(&s);

	barrier_init(&barrierInit, 3);

	s_dct.id = thread_create(dct_thread, NULL);
	s_quant.id = thread_create(quant_thread, NULL);
	s_idct.id = thread_create(idct_thread, NULL);

	thread_wait(s_dct.id);
	thread_wait(s_quant.id);
	thread_wait(s_idct.id);

	getTime(&t);

	printTime(&s, &t, "Total: ", "\n");

	return 0;
}
示例#9
0
int main()
{
    DCMF_Messager_initialize();

    init();

    barrier_init(DCMF_DEFAULT_GLOBALBARRIER_PROTOCOL);

    allreduce_init(DCMF_DEFAULT_GLOBALALLREDUCE_PROTOCOL);

    control_init(DCMF_DEFAULT_CONTROL_PROTOCOL, DCMF_DEFAULT_NETWORK);

    memregion_init(MAX_MSG_SIZE * ITERATIONS * 2);

    get_init(DCMF_DEFAULT_PUT_PROTOCOL, DCMF_TORUS_NETWORK);

    if (myrank == 0)
    {
        printf("Get Bandwidth - All processes communication in pairs \n");
        fflush(stdout);
    }
    get_contention();

    if (myrank == 0)
    {
        printf("Benchmark Complete \n");
        fflush(stdout);
    }

    memregion_finalize();

    DCMF_Messager_finalize();

    return 0;
}
示例#10
0
int main()
{
    DCMF_Messager_initialize();

    init();

    barrier_init(DCMF_DEFAULT_GLOBALBARRIER_PROTOCOL);

    posix_memalign((void **) &source, 16, MAX_MSG_SIZE_LOCAL);
    posix_memalign((void **) &target, 16, MAX_MSG_SIZE_LOCAL);

    send_init(DCMF_EAGER_SEND_PROTOCOL, DCMF_TORUS_NETWORK);

    barrier();

    send_remoteadvance();

    barrier();

    if (myrank == 0)
    {
        printf("[%d] Benchmark Complete \n", myrank);
        fflush(stdout);
    }

    memregion_finalize();

    DCMF_Messager_finalize();

    return 0;
}
示例#11
0
文件: main.c 项目: bthies/streamit
// main() Function Here
int main(int argc, char** argv) {

  // Initialize barrier
  barrier_init(&barrier, 1);

  // Spawn threads
  int rc;
  pthread_attr_t attr;
  void *status;

  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  pthread_t thread_n0;
  if ((rc = pthread_create(&thread_n0, NULL, __main____4659, (void *)NULL)) < 0)
    printf("Error creating thread for core 0: %d\n", rc);

  pthread_attr_destroy(&attr);

  if ((rc = pthread_join(thread_n0, &status)) < 0) {
    printf("Error joining thread for core 0: %d\n", rc);
    exit(-1);
  }

  // Exit
  pthread_exit(NULL);
}
示例#12
0
int main(int argc, char **argv) {

	int my_id, num_processes;

	MPI_Init(&argc, &argv);

  	MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
  	barrier_init(num_processes);
  	MPI_Comm_rank(MPI_COMM_WORLD, &my_id);

  	fprintf(stderr, "Before barrier 1 : Process %d of %d\n", my_id+1, num_processes);
	//double time1 = MPI_Wtime();

	// Barrier
	barrier(my_id);

	fprintf(stderr, "Before Barrier 2 : Process %d of %d\n", my_id+1, num_processes);
	//double time2 = MPI_Wtime();
	
	// Barrier
	barrier(my_id);

	fprintf(stderr, "After barrier 2 : Process %d of %d\n", my_id+1, num_processes);
	//double time3 = MPI_Wtime();
	
  	MPI_Finalize();
  	return 0;
}
示例#13
0
int
main(int argc, char *argv[])
{
  pthread_t *tha;
  void *value;
  long i;
  double t1, t0;

  if (argc < 2) {
    fprintf(stderr, "%s: %s nthread\n", argv[0], argv[0]);
    exit(-1);
  }
  nthread = atoi(argv[1]);
  tha = malloc(sizeof(pthread_t) * nthread);
  srandom(0);

  barrier_init();

  for(i = 0; i < nthread; i++) {
    assert(pthread_create(&tha[i], NULL, thread, (void *) i) == 0);
  }
  for(i = 0; i < nthread; i++) {
    assert(pthread_join(tha[i], &value) == 0);
  }
  printf("OK; passed\n");
}
示例#14
0
int main()
{
    DCMF_Messager_initialize();

    init();

    barrier_init(DCMF_DEFAULT_GLOBALBARRIER_PROTOCOL);

    control_init(DCMF_DEFAULT_CONTROL_PROTOCOL, DCMF_DEFAULT_NETWORK);

    memregion_init(MAX_BUF_SIZE * nranks);

    put_init(DCMF_DEFAULT_PUT_PROTOCOL, DCMF_TORUS_NETWORK);

    barrier();

    if (myrank == 0)
    {
        printf("Put Latency (usec) post vs restart\n");
        fflush(stdout);
    }
    put_restart();

    barrier();

    printf("[%d] Benchmark complete\n", myrank);
    fflush(stdout);

    memregion_finalize();

    DCMF_Messager_finalize();

    return 0;
}
示例#15
0
/**
 * @brief Constructor
 */
IpmiSEL::IpmiSEL(void)
    :iv_msgQ(msg_q_create())
{
    IPMI_TRAC(ENTER_MRK "IpmiSEL ctor");
    barrier_init(&iv_sync_start, 2);
    task_create(&IpmiSEL::start, this);
    barrier_wait(&iv_sync_start);
}
示例#16
0
int main() {
  barrier_init(&barrier, 2);
  pthread_t t[2];
  StartThread(&t[0], Thread1);
  StartThread(&t[1], Thread2);
  pthread_join(t[0], NULL);
  pthread_join(t[1], NULL);
  return 0;
}
示例#17
0
int main() {
  barrier_init(&barrier, 2);
  pthread_t t[2];
  pthread_create(&t[0], NULL, Thread1, NULL);
  pthread_create(&t[1], NULL, Thread2, NULL);
  pthread_join(t[0], NULL);
  pthread_join(t[1], NULL);
  return 0;
}
示例#18
0
int main() {
  barrier_init(&barrier, 2);
  int *a = new int(0);
  pthread_t t;
  pthread_create(&t, 0, Thread, a);
  delete a;
  barrier_wait(&barrier);
  pthread_join(t, 0);
}
示例#19
0
文件: tiny_race.c 项目: 0day-ci/gcc
int main() {
  barrier_init(&barrier, 2);
  pthread_t t;
  pthread_create(&t, 0, Thread1, 0);
  Global = 43;
  barrier_wait(&barrier);
  pthread_join(t, 0);
  return Global;
}
示例#20
0
文件: main.c 项目: rwmarejka/barrier
int
main( int argc, char *argv[] ) {
	int		c;
	int		i;
	int		niter	= NITER;
	int		nthr	= NTHR;
	int		bound	= 0;
	pthread_attr_t	attr;

	while ( ( c = getopt( argc, argv, "i:t:b" ) ) != EOF )
		switch ( c ) {
		  case 'i' :
			niter	= atoi( optarg );
			break;

		  case 't' :
			nthr	= atoi( optarg );
			break;

		  case 'b' :
			bound	= 1;
			break;

		  default  :
			fprintf( stderr, "usage: barrier -i N -t N -b\n" );
			exit( 1 );
		}

	if ( i = barrier_init( &ba, nthr + 1 ) ) {
        fprintf( stderr, "barrer_init: %d\n", i );
        exit( 1 );
    }

	pthread_attr_init( &attr );

	if ( bound )
		pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );

	for ( i=0; i < nthr; ++i ) {
		int		n;
		pthread_t	tid;

		if ( n = pthread_create( &tid, &attr, (void *(*)( void *)) bthread, &ba ) ) {
			errno	= n;
			perror( "pthread_create" );
			exit( 1 );
		}
	}

	pthread_attr_destroy( &attr );

	for ( i=0; i < niter; ++i )
		barrier_wait( &ba );

	return( 0 );
}
示例#21
0
文件: mutexset1.c 项目: ds2dev/gcc
int main() {
  barrier_init(&barrier, 2);
  pthread_mutex_init(&mtx, 0);
  pthread_t t[2];
  pthread_create(&t[0], NULL, Thread1, NULL);
  pthread_create(&t[1], NULL, Thread2, NULL);
  pthread_join(t[0], NULL);
  pthread_join(t[1], NULL);
  pthread_mutex_destroy(&mtx);
  return 0;
}
示例#22
0
int main() {
  volatile int N = 5;  // prevent loop unrolling
  barrier_init(&barrier, N + 1);
  for (int i = 0; i < N; i++) {
    pthread_t t;
    pthread_create(&t, 0, Thread, 0);
  }
  barrier_wait(&barrier);
  sleep(1);  // wait for the threads to finish and exit
  return 0;
}
示例#23
0
int main() {
  barrier_init(&barrier, 2);
  pthread_mutex_t Mtx;
  pthread_mutex_init(&Mtx, 0);
  pthread_t t;
  pthread_create(&t, 0, Thread, &Mtx);
  barrier_wait(&barrier);
  pthread_mutex_destroy(&Mtx);
  pthread_join(t, 0);
  return 0;
}
示例#24
0
文件: montepi.c 项目: cdkersey/iqyax
int main() {
  /* Initialize the RNG */
  unsigned rng, i, trials = TRIALS / get_num_cores(), x, y, active;
  rng = 0;
  init_rng(&rng);

  barrier(&b0);

  /* Do the simulation */
  for (i = 0; i < trials; ++i) {
    x = rand_mc(&rng) / ((1u<<31)/10000);
    y = rand_mc(&rng) / ((1u<<31)/10000);
    if (x * x + y * y <= 100000000) count[get_core_id()]++;
  }

  printf("core %u: %u\n", get_core_id(), count[get_core_id()]);

  barrier(&b1); if (get_core_id() == 0) barrier_init(&b0);
  barrier(&b2); if (get_core_id() == 0) barrier_init(&b1);
  /* Do the final reduction */

  for (active = get_num_cores()/2; active > 0; active /= 2) {
    if (get_core_id() < active) {
      unsigned idx0 = get_core_id(), idx1 = get_core_id() + active;
      count[idx0] = count[idx0] + count[idx1];
      printf("%u active cores, sum = %u\n", active, count[idx0]);
    }

    barrier(&b0); if (get_core_id() == 0) barrier_init(&b2);
    barrier(&b1); if (get_core_id() == 0) barrier_init(&b0);
    barrier(&b2); if (get_core_id() == 0) barrier_init(&b1);
  }

  if (get_core_id() == 0) {
    unsigned pi_whole = count[0]*4/TRIALS,
             pi_frac = count[0]*4%TRIALS/(TRIALS/100);
    printf("pi is approximately %u.%02u\n", pi_whole, pi_frac);
  }

  return 0;
}
示例#25
0
文件: pr64265.C 项目: 0day-ci/gcc
int
main ()
{
  pthread_t th;
  barrier_init (&barrier, 2);
  if (pthread_create (&th, NULL, tf, NULL))
    return 0;
  v++;
  barrier_wait (&barrier);
  pthread_join (th, NULL);
  return 0;
}
示例#26
0
文件: eclBarr.cpp 项目: GMAC-lib/gmac
int main(int argc, char *argv[])
{
	thread_t *nThread;
	unsigned n = 0;
	gmactime_t st, en;

	setParam<unsigned>(&nIter, nIterStr, nIterDefault);
	setParam<unsigned>(&vecSize, vecSizeStr, vecSizeDefault);

	barrier_init(&barr, nIter + 1);

	assert(eclCompileSource(kernel) == eclSuccess);

	nThread = (thread_t *)malloc(nIter * sizeof(thread_t));
	unsigned *ids = (unsigned *)malloc(nIter * sizeof(unsigned));

	getTime(&st);
	for(n = 0; n < nIter; n++) {
		ids[n] = n;
		nThread[n] = thread_create(check, &ids[n]);
	}

	// Alloc & init input data
	ecl_error ret = eclMalloc((void **)&a, vecSize * sizeof(float));
	assert(ret == eclSuccess);

	for(n = 0; n < 32; n++) {
		// Call the kernel
		size_t globalSize = vecSize;
		ecl_kernel kernel;
		assert(eclGetKernel("accum", &kernel) == eclSuccess);
		assert(eclSetKernelArgPtr(kernel, 0, a) == eclSuccess);
		assert(eclCallNDRange(kernel, 1, NULL, &globalSize, NULL) == eclSuccess);
		barrier_wait(&barr);

		// Wait for the threads to do stuff
		barrier_wait(&barr);
	}

	for(n = 0; n < nIter; n++) {
		thread_wait(nThread[n]);
	}

	getTime(&en);
	printTime(&st, &en, "Total: ", "\n");

	free(ids);
	free(nThread);

	return 0;
}
示例#27
0
int main() {
  fprintf(stderr, "Hello world.\n");
  dispatch_semaphore_t done = dispatch_semaphore_create(0);
  barrier_init(&barrier, 2);

  dispatch_queue_t q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
  dispatch_queue_t bgq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

  dispatch_async(bgq, ^{
    dispatch_sync(q, ^{
      global = 42;
    });
    barrier_wait(&barrier);
  });
示例#28
0
int main() {
  barrier_init(&barrier, 2);
  pthread_t t;
  pthread_create(&t, 0, Thread, 0);
  struct timespec ts;
  clock_gettime(CLOCK_REALTIME, &ts);
  check(pthread_timedjoin_np(t, 0, &ts), ETIMEDOUT);
  barrier_wait(&barrier);
  clock_gettime(CLOCK_REALTIME, &ts);
  ts.tv_sec += 10000;
  check(pthread_timedjoin_np(t, 0, &ts), 0);
  var = 2;
  fprintf(stderr, "PASS\n");
  return 0;
}
int main (int arg, char *argv[])
{
    int thread_count, array_count;
    int status;

    barrier_init (&barrier, THREADS);

    /*
     * Create a set of threads that will use the barrier.
     */
    for (thread_count = 0; thread_count < THREADS; thread_count++) {
        thread[thread_count].increment = thread_count;
        thread[thread_count].number = thread_count;

        for (array_count = 0; array_count < ARRAY; array_count++)
            thread[thread_count].array[array_count] = array_count + 1;
        // for (array_count = 0; array_count < ARRAY; array_count++)
        //     printf ("%010u ", thread[thread_count].array[array_count]);
        // printf ("\n");

        status = pthread_create (&thread[thread_count].thread_id,
            NULL, thread_routine, (void*)&thread[thread_count]);
        if (status != 0)
            err_abort (status, "Create thread");
    }

    /*
     * Now join with each of the threads.
     */
    for (thread_count = 0; thread_count < THREADS; thread_count++) {
        status = pthread_join (thread[thread_count].thread_id, NULL);
        if (status != 0)
            err_abort (status, "Join thread");

        printf ("%02d: (%d) ", thread_count, thread[thread_count].increment);

        for (array_count = 0; array_count < ARRAY; array_count++)
            printf ("%010u ", thread[thread_count].array[array_count]);
        printf ("\n");
    }

    /*
     * To be thorough, destroy the barrier.
     */
    barrier_destroy (&barrier);
    return 0;
}
示例#30
0
int main() {
    barrier_init(&barrier, 2);

    pthread_t th;

    pthread_mutex_init(&m, 0);
    pthread_cond_init(&c, 0);

    pthread_create(&th, 0, thr1, 0);
    barrier_wait(&barrier);
    sleep(1);  // let it block on cond var
    pthread_cancel(th);

    pthread_join(th, 0);
    pthread_mutex_lock(&m);
    pthread_mutex_unlock(&m);
    fprintf(stderr, "OK\n");
}