• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ pthread_barrier_init函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中pthread_barrier_init函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_barrier_init函数的具体用法?C++ pthread_barrier_init怎么用?C++ pthread_barrier_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了pthread_barrier_init函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: do_test

static int
do_test (void)
{
  stack_t ss;
  ss.ss_sp = malloc (2 * SIGSTKSZ);
  if (ss.ss_sp == NULL)
    {
      puts ("failed to allocate alternate stack");
      return 1;
    }
  ss.ss_flags = 0;
  ss.ss_size = 2 * SIGSTKSZ;
  if (sigaltstack (&ss, NULL) < 0)
    {
      printf ("sigaltstack failed %m\n");
      return 1;
    }

  if (pthread_barrier_init (&b, NULL, 2) != 0)
    {
      puts ("barrier_init failed");
      return 1;
    }

  struct sigaction sa;
  sa.sa_handler = sh;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = 0;

  if (sigaction (SIGHUP, &sa, NULL) != 0)
    {
      puts ("sigaction failed");
      return 1;
    }

  puts ("sa_flags = 0 test");
  if (do_one_test ())
    return 1;

  sa.sa_handler = sh;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = SA_ONSTACK;

  if (sigaction (SIGHUP, &sa, NULL) != 0)
    {
      puts ("sigaction failed");
      return 1;
    }

  puts ("sa_flags = SA_ONSTACK test");
  if (do_one_test ())
    return 1;

  sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = SA_SIGINFO;

  if (sigaction (SIGHUP, &sa, NULL) != 0)
    {
      puts ("sigaction failed");
      return 1;
    }

  puts ("sa_flags = SA_SIGINFO test");
  if (do_one_test ())
    return 1;

  sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = SA_SIGINFO | SA_ONSTACK;

  if (sigaction (SIGHUP, &sa, NULL) != 0)
    {
      puts ("sigaction failed");
      return 1;
    }

  puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
  if (do_one_test ())
    return 1;

  return 0;
}
开发者ID:AdvancedC,项目名称:glibc,代码行数:83,代码来源:tst-cancel20.c


示例2: TEST


//.........这里部分代码省略.........
#endif

    cout << "_POSIX_THREAD_ATTR_STACKSIZE/_SC_THREAD_ATTR_STACKSIZE ----------------------------- " <<endl;
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
    cout << "POSIX_THREAD_ATTR_STACKSIZE " << _POSIX_THREAD_ATTR_STACKSIZE << endl;
#endif
#ifdef _SC_THREAD_ATTR_STACKSIZE
        m = sysconf(_SC_THREAD_ATTR_STACKSIZE);
    cout << "sysconf: _SC_THREAD_ATTR_STACKSIZE " << 
                        _SC_THREAD_ATTR_STACKSIZE << endl;
#endif

    cout << "_POSIX_THREAD_ATTR_STACKADDR/_SC_THREAD_ATTR_STACKADDR ----------------------------- " <<endl;
#ifdef _POSIX_THREAD_ATTR_STACKADDR
    cout << "POSIX_THREAD_ATTR_STACKADDR " << _POSIX_THREAD_ATTR_STACKADDR << endl;
#endif
#ifdef _SC_THREAD_ATTR_STACKADDR
        m = sysconf(_SC_THREAD_ATTR_STACKADDR);
    cout << "sysconf: _SC_THREAD_ATTR_STACKADDR " << 
                        _SC_THREAD_ATTR_STACKADDR << endl;
#endif

#ifdef _POSIX_BARRIERS
    cout << "POSIX_BARRIERS " << _POSIX_BARRIERS << endl;
#endif
#ifdef _POSIX_READER_WRITER_LOCKS
    cout << "POSIX_READER_WRITER_LOCKS " << _POSIX_READER_WRITER_LOCKS << endl;
#endif

#ifdef _POSIX_THREAD_THREADS_MAX
    cout << "POSIX_THREAD_THREADS_MAX " << _POSIX_THREAD_THREADS_MAX << endl;
#endif

#ifdef PTHREAD_THREADS_MAX
        threads = PTHREADS_MAX;
    cout << "limits.h: maximum pthreads per process is " << 
                        threads << endl;
#else
    cout 
        << "limits.h: maximum pthreads per process is not defined." 
        << endl;
#endif
#ifdef _SC_THREAD_THREADS_MAX
        m = sysconf(_SC_THREAD_THREADS_MAX);
    cout << "limits.h: maximum pthreads per process is " << 
                        m << endl;
        threads = int(m);
#else
    cout 
        << "NOT COMPLIIANT: sysconf: maximum _SC_PTHREAD_THREADS_MAX is not defined." 
        << endl;
#endif

        // create and fork as many threads as the system will allow,

#ifdef TEST_FIND_PTHREAD_RUNTIMEMAX
        threads=0;
    while(true) {
                threads++;
                // pthread_attr_t attr;
                pthread_t thr;
                int e= pthread_create(&thr, NULL, dummy, NULL);
                if(e!=0) {
                        if(e==EAGAIN) {
                                cout << "runtime: maximum pthreads " << threads << endl;
                                break;
                        }
                        perror("perror ");
                        cout << "ERROR " << e << endl;
                        cout << "threads " << threads << endl;
                        break;
                }
    }
        // On RHEL 5 I got 32748 with the runtime test despite having
        // the stated limit of POSIX_THREAD_THREADS_MAX of 64.
#endif

        threads=2;
        // They all wait on this barrier.
    pthread_barrier_init(&b, NULL, threads+1);
    sthread_t **t = new sthread_t *[threads];

    for (int i = 0; i < threads; i++)
        t[i] = new simple_thread_t();

    for (int i = 0; i < threads; i++)
        EXPECT_TRUE(t[i] != NULL);

    for (int i = 0; i < threads; i++)
        EXPECT_FALSE(t[i]->fork().is_error());

    pthread_barrier_wait(&b);

    for (int i = 0; i < threads; i++)
        EXPECT_FALSE(t[i]->join().is_error());

    pthread_barrier_destroy(&b);
    
    delete[] t;
}
开发者ID:PickXu,项目名称:zero-xum,代码行数:101,代码来源:test_pthread.cpp


示例3: do_test

static int
do_test (void)
{
  if (pthread_barrier_init (&bar, NULL, 3) != 0)
    {
      puts ("barrier_init failed");
      exit (1);
    }

  pthread_attr_t a;

  if (pthread_attr_init (&a) != 0)
    {
      puts ("attr_init failed");
      exit (1);
    }

  if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
    {
      puts ("attr_setstacksize failed");
      return 1;
    }

  pthread_t th[2];

  if (pthread_create (&th[0], &a, tf, NULL) != 0)
    {
      puts ("1st create failed");
      exit (1);
    }

  if (pthread_attr_setdetachstate (&a, PTHREAD_CREATE_DETACHED) != 0)
    {
      puts ("attr_setdetachstate failed");
      exit (1);
    }

  if (pthread_create (&th[1], &a, tf, NULL) != 0)
    {
      puts ("1st create failed");
      exit (1);
    }

  if (pthread_attr_destroy (&a) != 0)
    {
      puts ("attr_destroy failed");
      exit (1);
    }

  if (pthread_detach (th[0]) != 0)
    {
      puts ("could not detach 1st thread");
      exit (1);
    }

  int err = pthread_detach (th[0]);
  if (err == 0)
    {
      puts ("second detach of 1st thread succeeded");
      exit (1);
    }
  if (err != EINVAL)
    {
      printf ("second detach of 1st thread returned %d, not EINVAL\n", err);
      exit (1);
    }

  err = pthread_detach (th[1]);
  if (err == 0)
    {
      puts ("detach of 2nd thread succeeded");
      exit (1);
    }
  if (err != EINVAL)
    {
      printf ("detach of 2nd thread returned %d, not EINVAL\n", err);
      exit (1);
    }

  exit (0);
}
开发者ID:riscv,项目名称:riscv-glibc,代码行数:81,代码来源:tst-join4.c


示例4: main

int
main()
{
  int i;
  int fail = 0;
  pthread_t thread[NUM_THREADS];

  assert(pthread_barrier_init(&startBarrier, NULL, NUM_THREADS/2) == 0);

  for (i = 1; i < NUM_THREADS/2; i++)
    {
      accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
      assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
    }

  /*
   * Here we test that existing threads will get a key created
   * for them.
   */
  assert(pthread_key_create(&key, destroy_key) == 0);

  (void) pthread_barrier_wait(&startBarrier);

  /*
   * Test main thread key.
   */
  accesscount[0] = 0;
  setkey((void *) &accesscount[0]);

  /*
   * Here we test that new threads will get a key created
   * for them.
   */
  for (i = NUM_THREADS/2; i < NUM_THREADS; i++)
    {
      accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
      assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
    }

  /*
   * Wait for all threads to complete.
   */
  for (i = 1; i < NUM_THREADS; i++)
    {
	assert(pthread_join(thread[i], NULL) == 0);
    }

  assert(pthread_key_delete(key) == 0);

  assert(pthread_barrier_destroy(&startBarrier) == 0);

  for (i = 1; i < NUM_THREADS; i++)
    {
	/*
	 * The counter is incremented once when the key is set to
	 * a value, and again when the key is destroyed. If the key
	 * doesn't get set for some reason then it will still be
	 * NULL and the destroy function will not be called, and
	 * hence accesscount will not equal 2.
	 */
	if (accesscount[i] != 2)
	  {
	    fail++;
	    fprintf(stderr, "Thread %d key, set = %d, destroyed = %d\n",
			i, thread_set[i], thread_destroyed[i]);
	  }
    }

  fflush(stderr);

  return (fail);
}
开发者ID:0xack13,项目名称:nodejs-appliance-web-mgmt,代码行数:72,代码来源:tsd1.c


示例5: main

/* The main test function. */
int main( int argc, char *argv[] )
{
	int ret = 0;
	pthread_t tcontrol, tchange;
	pthread_barrier_t bar;

	/* Initialize output routine */
	output_init();

	ret = pthread_barrier_init( &bar, NULL, 2 );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to init barrier" );
	}

	/* Create the controler thread */
	ret = pthread_create( &tcontrol, NULL, controler, &bar );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "thread creation failed" );
	}

	/* Now create the changer thread */
	ret = pthread_create( &tchange, NULL, changer, &tcontrol );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "thread creation failed" );
	}

	/* wait until the changer finishes */
	ret = pthread_join( tchange, NULL );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to join the thread" );
	}


	/* let the controler control */
	ret = pthread_barrier_wait( &bar );

	if ( ( ret != 0 ) && ( ret != PTHREAD_BARRIER_SERIAL_THREAD ) )
	{
		UNRESOLVED( ret, "barrier wait failed" );
	}


	ret = pthread_join( tcontrol, NULL );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to join the thread" );
	}

	ret = pthread_barrier_destroy( &bar );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "barrier destroy failed" );
	}

	PASSED;
}
开发者ID:8l,项目名称:rose,代码行数:67,代码来源:pthread_setschedparam.1-2.c


示例6: main

int main(int argc, char *argv[]) {
    // CLI argument parsing
    if (argc != 4) {
        fprintf(stderr, "Expected 3 arguments, got %d\nExiting...\n", argc - 1);
        exit(1);
    }
    else {
        char *end;
        uint32_t thread_count = (uint32_t) strtol(argv[1], &end, 10);
        if (*end) {
            fprintf(stderr, "Error, expects first argument to be a power of two,"
                    " was '%s'.\n", argv[1]);
            exit(1);
        }
        if (!(thread_count && !(thread_count & (thread_count - 1)))) {
            fprintf(stderr, "Error, first argument must be a power of two, not %d.\n",
                    thread_count);
            exit(1);
        }
        fprintf(stderr, "Looking for '%s' in file %s using %d threads...\n",
                argv[2], argv[3], thread_count);

        char *search_term = argv[2];

        FILE *fp;
        long file_size;
        char *buffer;
        fp = fopen(argv[3], "rb");
        if (!fp) {
            perror(argv[1]);
            exit(1);
        }
        fseek(fp, 0, SEEK_END);
        file_size = ftell(fp);
        rewind(fp);

        buffer = malloc(file_size + 1);
        if (!buffer) {
            fclose(fp);
            fprintf(stderr, "Cannot allocate buffer for file (probably too large).\n");
            exit(1);
        }
        buffer[file_size] = 0;

        if (1 != fread(buffer, file_size, 1, fp)) {
            fclose(fp);
            free(buffer);
            fprintf(stderr, "Entire read failed.\n");
            exit(1);
        }
        else {
            fclose(fp);
        }


        // Find all lines
        pthread_t *tid = malloc(thread_count * sizeof(pthread_t));
        LineSplitArgs *lsa = malloc(thread_count * sizeof(LineSplitArgs));
        uint32_t *intervals = malloc(thread_count * sizeof(uint32_t));
        struct timespec time_start, time_stop;
        long long nanoseconds_elapsed;

        for (uint32_t i = 0; i < thread_count; i++) {
            LineSplitArgs arguments =
            {   .buffer = buffer, .file_size = (uint32_t) file_size,
                .own_tid = i, .thread_count = thread_count,
                .intervals = intervals
            };
            lsa[i] = arguments;
        }

        clock_gettime(CLOCK_MONOTONIC, &time_start);
        for (uint32_t i = 0; i < thread_count; i++) {
            pthread_create(&tid[i], NULL, &split_to_lines, (void *) &lsa[i]);
        }

        for (uint32_t i = 0; i < thread_count; i++) {
            pthread_join(tid[i], NULL);
        }
        clock_gettime(CLOCK_MONOTONIC, &time_stop);
        nanoseconds_elapsed = (time_stop.tv_sec - time_start.tv_sec) * 1000000000LL;
        nanoseconds_elapsed += (time_stop.tv_nsec - time_start.tv_nsec);

        long long line_split = nanoseconds_elapsed;

        free(lsa);

        // Concatenate work
        ConcatArgs *ca = malloc(thread_count * sizeof(ConcatArgs));
        pthread_barrier_t *barriers = malloc(thread_count * sizeof(pthread_barrier_t));

        for (uint32_t i = 0; i < thread_count; i++) {
            pthread_barrier_init(&barriers[i], NULL, 2);
            ConcatArgs args = {.own_tid = i, .thread_count = thread_count,
                               .intervals = intervals, .barriers = barriers
                              };
            ca[i] = args;
        }

        clock_gettime(CLOCK_MONOTONIC, &time_start);
//.........这里部分代码省略.........
开发者ID:sizur,项目名称:c-rrb,代码行数:101,代码来源:pgrep_dummy.c


示例7: main

int main(int argc, char *argv[])
{
	if (argc != 5) {
		printf("Usage: %s nx ny steps threads.\n", argv[0]);
		return 1;
	}
	int i, ti;
	le_point2 n = {atoi(argv[1]), atoi(argv[2])};
	int steps = atoi(argv[3]);
	int max_threads = atoi(argv[4]);
	le_task task;
	le_material mat;
	le_vec2 h = {1.0, 1.0};
	real dt = 0.3;
	le_vec2 center = {n.x / 2, n.y / 2};
	char name[1000];

	double t;
	unsigned long cc;
	
	/* pthread variables */
	pthread_t threads[NUM_THREADS];
	st_pthread data[NUM_THREADS];
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	/* end of pthread variables */

	le_init_material(2.0, 1.0, 1.5, &mat);
	task.max_threads = max_threads;
	task.steps = steps;
	le_init_task(&task, dt, h, mat, n);
	le_set_ball(&task, center, 10.0, 1.0);

	pthread_barrier_init(&b, NULL, max_threads);
	
	cc = getCC();
	t = timer();
	
	for(ti = 0; ti < max_threads; ti++) {		
		data[ti].task = &task;
		data[ti].thread_num = ti;
		int rc = pthread_create(&(threads[ti]), &attr, le_step, (void *)(&(data[ti])));
		assert(!rc);
	}
	for(ti = 0; ti < max_threads; ti++) {
		void *st;
		int rc = pthread_join(threads[ti], &st);
		assert(!rc);;
	}
	
	t = timer() - t;
	cc = getCC() - cc;
	
	pthread_barrier_destroy(&b);
	
	printf("%d %d %d %f ", n.x, n.y, steps, t);
	
	le_save_task(&task, "result.vtk");

	le_free_task(&task);
	pthread_exit(NULL);
}
开发者ID:am-ivanov,项目名称:le2d,代码行数:63,代码来源:main.c


示例8: do_test

static int
do_test (void)
{
  if (pthread_barrier_init (&b, NULL, N + 1) != 0)
    {
      puts ("barrier_init failed");
      return 1;
    }

  pthread_mutex_lock (&mut);

  int i, j, err;
  pthread_t th[N];
  for (i = 0; i < N; ++i)
    if ((err = pthread_create (&th[i], NULL, tf, NULL)) != 0)
      {
	printf ("cannot create thread %d: %s\n", i, strerror (err));
	return 1;
      }

  for (i = 0; i < ROUNDS; ++i)
    {
      pthread_cond_wait (&cond2, &mut);

      if (i & 1)
        pthread_mutex_unlock (&mut);

      if (i & 2)
	pthread_cond_broadcast (&cond);
      else if (i & 4)
	for (j = 0; j < N; ++j)
	  pthread_cond_signal (&cond);
      else
	{
	  for (j = 0; j < (i / 8) % N; ++j)
	    pthread_cond_signal (&cond);
	  pthread_cond_broadcast (&cond);
	}

      if ((i & 1) == 0)
        pthread_mutex_unlock (&mut);

      err = pthread_cond_destroy (&cond);
      if (err)
	{
	  printf ("pthread_cond_destroy failed: %s\n", strerror (err));
	  return 1;
	}

      /* Now clobber the cond variable which has been successfully
         destroyed above.  */
      memset (&cond, (char) i, sizeof (cond));

      err = pthread_barrier_wait (&b);
      if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  puts ("parent: barrier_wait failed");
	  return 1;
	}

      pthread_mutex_lock (&mut);

      err = pthread_barrier_wait (&b);
      if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
	{
	  puts ("parent: barrier_wait failed");
	  return 1;
	}

      count = 0;
      err = pthread_cond_init (&cond, NULL);
      if (err)
	{
	  printf ("pthread_cond_init failed: %s\n", strerror (err));
	  return 1;
	}
    }

  for (i = 0; i < N; ++i)
    if ((err = pthread_join (th[i], NULL)) != 0)
      {
	printf ("failed to join thread %d: %s\n", i, strerror (err));
	return 1;
      }

  puts ("done");

  return 0;
}
开发者ID:Xilinx,项目名称:eglibc,代码行数:89,代码来源:tst-cond20.c


示例9: main

int main(int argc, char **argv) { 
   struct timeval start, end;

   //Read partition file
   char *dot = strrchr(argv[2], '.');
   if (!dot || dot == argv[2]) dot = "";
   else dot = dot + 1;
   
   int partitionCount = 0;
   //determine the number of partitions
   if (isdigit(dot[0])) {
      partitionCount = atoi(dot);
   } else {
      printf("INVALID PARTITION FILE\n");
      exit(1);
   }

   max_threads = partitionCount;
   pthread_t p_threads[max_threads];
   pthread_attr_t attr;
   pthread_attr_init(&attr);
   pthread_mutex_init(&sumLock, NULL);

   pthread_barrier_init(&barr, NULL, max_threads);

   FILE *pFile = fopen(argv[1], "r");
   if (pFile==NULL) {
      printf("File error");
      fputs ("File error", stderr); 
      exit(1);
   }

   int size;
   fseek (pFile, 0, SEEK_END);
   size = ftell(pFile);
   rewind (pFile);

   int i = 0;
   char st[20];
   int readRowsPtrs = 0;
   long int* rownum = (long int *) malloc(size * sizeof(long int));

   printf("READING LAST LINE\n");
   while (fscanf(pFile, "%20s", st) != EOF) {
      if (readRowsPtrs == 1) {
         long int v;
         v = strtol(st, NULL, 0);
         rownum[i] = v;
         i++;
      }

      if (strcmp(st, "row_ptrs:") == 0) {
         readRowsPtrs = 1;
      }
   }
   rewind(pFile);
   printf("LAST LINE READ\n");

   struct row matrixdata;
   matrixdata.rowary = malloc(i * sizeof(rowData));
   matrixdata.rcount = i-1;
   
   int *outGoingEdgeCount = (int *) malloc(matrixdata.rcount * sizeof(int));

   int c;
   for (c=0; c < matrixdata.rcount; c++) { //i is the total amount of rows in the matrix
      long int rowSize;
      rowSize = rownum[c+1] - rownum[c];

      matrixdata.rowary[c].values = malloc(rowSize * sizeof(double));
      matrixdata.rowary[c].col = malloc(rowSize * sizeof(long int));
      matrixdata.rowary[c].vcount = rowSize;

      outGoingEdgeCount[c] = 0;
   }
   free(rownum);

   i = 0;
   c = 0;      //Matrix row incrementor
   char str[20];
   int newline = 0;
   char ch;
   rewind(pFile);

   printf("READING col_inds LINE\n");
   readRowsPtrs = 0;
   while (fscanf(pFile, "%20s", st) != EOF) {
      if (strcmp(st, "row_ptrs:") == 0) {
         break;
      }

      if (readRowsPtrs == 1) {
         int v;
         v = strtol(st, NULL, 0);
         outGoingEdgeCount[v] = outGoingEdgeCount[v] + 1;
      }

      if (strcmp(st, "col_inds:") == 0) {
         readRowsPtrs = 1;
      }
//.........这里部分代码省略.........
开发者ID:wiszow238,项目名称:Pthread-PageRank,代码行数:101,代码来源:pagerank_thread.c


示例10: uv_barrier_init

int uv_barrier_init(uv_barrier_t* barrier, unsigned int count) {
  return UV__ERR(pthread_barrier_init(barrier, NULL, count));
}
开发者ID:AlexanderPankiv,项目名称:node,代码行数:3,代码来源:thread.c


示例11: mdlInitialize

int mdlInitialize(MDL *pmdl,char **argv,void (*fcnChild)(MDL))
{
    MDL mdl,tmdl;
    int i,nThreads,bThreads,bDiag;
    char *p,ach[256],achDiag[256];
    
    *pmdl = NULL;
    /*
     ** Do some low level argument parsing for number of threads, and
     ** diagnostic flag!
     */
    bDiag = 0;
    bThreads = 0;
    i = 1;
    while (argv[i]) {
		if (!strcmp(argv[i],"-sz") && !bThreads) {
			++i;
			if (argv[i]) {
				nThreads = atoi(argv[i]);
				bThreads = 1;
				}
			}
		if (!strcmp(argv[i],"+d") && !bDiag) {
			p = getenv("MDL_DIAGNOSTIC");
			if (!p) p = getenv("HOME");
			if (!p) sprintf(ach,"/tmp");
			else sprintf(ach,"%s",p);
			bDiag = 1;
			}
		++i;
		}
    if (!bThreads) {
		nThreads = pthread_procsetinfo(NULL,0);
		}
    mdl = malloc(sizeof(struct mdlContext));
    assert(mdl != NULL);
    mdl->pmdl = malloc(nThreads*sizeof(MDL));
    assert(mdl->pmdl != NULL);
    mdl->pmdl[0] = mdl;			/* that's me! */
    mdl->pt = (pthread_t *)malloc(nThreads*sizeof(pthread_t));
    assert(mdl->pt != NULL);
    /*
     ** Initialize caching barrier.
     */
    pthread_barrier_init(&mdl->bar,pthread_barrierattr_default,nThreads);
    *pmdl = mdl;
    if (nThreads > 1) {
		for (i=1;i<nThreads;++i) {
			/*
			 ** Allocate the children mdl data structures.
			 */
			tmdl = malloc(sizeof(struct mdlContext));
			assert(tmdl != NULL);
			mdl->pmdl[i] = tmdl;
			tmdl->pt = NULL;
			}
		for (i=0;i<nThreads;++i) {
			/*
			 ** Set up all the mdl data structures.
			 */
			tmdl = mdl->pmdl[i];
            BasicInit(tmdl);
			tmdl->pmdl = mdl->pmdl;
			tmdl->idSelf = i;
			tmdl->bDiag = bDiag;
			tmdl->nThreads = nThreads;
			if (tmdl->bDiag) {
				sprintf(achDiag,"%s.%d",ach,tmdl->idSelf);
				tmdl->fpDiag = fopen(achDiag,"w");
				assert(tmdl->fpDiag != NULL);
				}
			}
		for (i=1;i<nThreads;++i) {
			/*
			 ** Start all the children.
			 */
			pthread_create(&mdl->pt[i],pthread_attr_default,fcnChild,
						   mdl->pmdl[i]);
			}
		return(nThreads);
		}
    else {
		/*
		 ** A unik!
		 */
        BasicInit(mdl);
		mdl->bDiag = bDiag;
		mdl->nThreads = 1;
		mdl->idSelf = 0;
		if (mdl->bDiag) {
			sprintf(achDiag,"%s.%d",ach,mdl->idSelf);
			mdl->fpDiag = fopen(achDiag,"w");
			assert(mdl->fpDiag != NULL);
			}
		return(nThreads);
		}
    }
开发者ID:N-BodyShop,项目名称:mdl,代码行数:97,代码来源:mdl.c


示例12: main

int main(int argc, char *argv[])
{
	int node_id = 0;
	int arrival_lambda = 10;
	int thread_cpu_map[N_THREADS];
	int i,j,k;
	int n_threads;
	int n_left;
	int n_right;
	int next_index_left = 3;
	int next_index_right = 7;
	float local_square = 0.0, remote_square = 0.0;


	/***************** make sure #args is correct and get the n_threads, n_left and n_right */
	if(argc < 4)
	{
		printf("Usage: ./test_numa_comb n_of_threads n_of_threads_on_node0 n_of_threads_on_node1\n");
		exit(-1);
	}
	n_threads = atoi(argv[1]);
	n_left = atoi(argv[2]);
	n_right = atoi(argv[3]);
	/******************* Set the thread_cpu_map according to the n_left and n_right */
	printf("n_threads: %d, n_left: %d, n_right: %d\n",n_threads,n_left,n_right);
	for(i = 0; i < n_left; i++)
	{
		thread_cpu_map[i] = next_index_left;
		next_index_left--;
	}
	for(i = n_left; i < n_threads; i++)
	{
		thread_cpu_map[i] = next_index_right;
		next_index_right--;
	}
	for(i = 0; i < n_threads; i++)
	{
		printf("Thread %d is on cpu %d\n",i,thread_cpu_map[i]);
	}



	thread_params para[n_threads]; //The parameters to pass to the threads

	//printf("The return value of numa_get_run_node_mask(void) is %d\n",numa_get_run_node_mask());
	//printf("The return value of numa_max_node(void) is %d\n",numa_max_node());
	//numa_tonode_memory((void *)spinlock_ptr,sizeof(pthread_spinlock_t),node_id); //This doesn't work

	//initilize the spinlock pointer and put it on a specific node
	pthread_spinlock_t *spinlock_ptr = numa_alloc_onnode(sizeof(pthread_spinlock_t),node_id);

	if(spinlock_ptr == NULL) //error handling of the allocating of a spinlock pointer on a specific node
	{
		printf("alloc of spinlock on a node failed.\n");
		exit(-1);
	}

	/* initialise  syncs */
	pthread_barrier_init(&fin_barrier, NULL, n_threads);
	pthread_spin_init(spinlock_ptr,0);
	int rc;
	//create the threads
	for(i = 0; i < n_threads; i++)
	{

		para[i].thread_id = i;
		para[i].arrival_lambda = arrival_lambda;
		para[i].spinlock_ptr = spinlock_ptr;
		CPU_ZERO(&cpuset[i]);
		CPU_SET(thread_cpu_map[i],&cpuset[i]);
		rc = pthread_create(&threads[i],NULL,work,(void*)&para[i]);
		E (rc);


	}
	start_work_flag = 1; 

	/* wait here */
	for(i = 0; i < n_threads; i++)
	    pthread_join(threads[i],NULL);


	pthread_barrier_destroy(&fin_barrier);

	/*
	for(i = 0; i < n_threads; i++)
	{
		printf("The time to get one lock for thread %d is : %.9f\n",i,time_in_cs[i]/num_access_each_thread[i]);
		printf("The number of lock accesses for thread %d is : %d\n",i,num_access_each_thread[i]);
	}
	*/

	qsort((void*)g_tss,(size_t)access_count,(size_t)sizeof(timestamp),cmp_timestamp);
	/*
	for (i = 0; i < access_count; i++)
		printf("%lu with id %d\n",g_tss[i].ts,g_tss[i].id);
	*/

	/* for (i = 0; i < access_count; i++)
	 * {
//.........这里部分代码省略.........
开发者ID:SLAP-,项目名称:locklocklock,代码行数:101,代码来源:test_numa_comb_jl.c


示例13: main

int main(int argc, char **argv)
{
  cpu_set_t dp;
  int retval[2];

  if (argc > 1)
    niter = atoi(argv[1]);

  signal(SIGALRM, sighand_alrm);
  
  ERRHAND(tmc_cpus_get_dataplane_cpus(&dp));  
  ERRHAND(cpu_snd = tmc_cpus_find_first_cpu(&dp));
  ERRHAND(cpu_rcv = tmc_cpus_find_last_cpu(&dp));
  
  ERRHAND_NZ(pthread_barrier_init(&computation_end, NULL, 2));

  for (cur_suite=0; cur_suite<niter; cur_suite++) {
    PRINT(fprintf(stderr, "[INFO] start test suite %d\n", cur_suite));
    /* --> init shared objects */
    ERRHAND_NN(ch = ch_sym_ref_sm_create(M, cpu_snd, cpu_rcv, NULL));
    /* set deadlock alarm */
    alarm(deadlock_timeout);

    /* start */
    ERRHAND(gettimeofday(&start, NULL));
    ERRHAND_NZ
      (pthread_create(&thread_producer, NULL, task_producer, (void *)cpu_snd));
    ERRHAND_NZ
      (pthread_create(&thread_consumer, NULL, task_consumer, (void *)cpu_rcv));
    /* wait end */
    ERRHAND_NZ(pthread_join(thread_producer, (void *)retval));
    ERRHAND_NZ(pthread_join(thread_consumer, (void *)(retval+1)));
    /* end */
    ERRHAND(gettimeofday(&end, NULL)); 

    timersub(&end, &start, &start);
    prepareStatistics(time_suite, start.tv_sec*1000+start.tv_usec/(double)1000);
    prepareStatistics(result_suite, retval[0] + retval[1]);
    PRINT(fprintf(stderr, "[INFO] end test suite %d: producer %d consumer %d\n",
		  cur_suite, retval[0], retval[1]));
    /* --> destroy shared object */
    ch_sym_ref_sm_destroy(&ch);
    deadlock_continue = 0;
  }

  calcStatistics(avg[0], sdev[0], result_suite, niter);
  calcStatistics(avg[1], sdev[1], time_suite, niter);  

  printf(printStatistics_format("return values", PRIu64)
	 printStatistics_format("elapsed time", "f")
	 printStatistics_format("Tcall-send", PRIu64)
	 printStatistics_format("Tcall-recv", PRIu64)
	 printStatistics_format("Tsend", PRIu64),
	 printStatistics_values(avg[0], sdev[0], result_suite),
	 printStatistics_values(avg[1], sdev[1], time_suite),
	 printStatistics_values(avg[2], sdev[2], call_send),
	 printStatistics_values(avg[3], sdev[3], call_recv),
	 printStatistics_values(avg[4], sdev[4], send)
	 );

  return result_suite[1];
}
开发者ID:federicomariti,项目名称:tileraCom,代码行数:62,代码来源:test_ch_sym_ref_sm.c


示例14: do_test

static int
do_test (void)
{
  char tmp[] = "/tmp/tst-signal1-XXXXXX";

  int fd = mkstemp (tmp);
  if (fd == -1)
    {
      puts ("mkstemp failed");
      exit (1);
    }

  unlink (tmp);

  int i;
  for (i = 0; i < 20; ++i)
    write (fd, "foobar xyzzy", 12);

  b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
	    MAP_SHARED, fd, 0);
  if (b == MAP_FAILED)
    {
      puts ("mmap failed");
      exit (1);
    }

  pthread_barrierattr_t ba;
  if (pthread_barrierattr_init (&ba) != 0)
    {
      puts ("barrierattr_init failed");
      exit (1);
    }

  if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
    {
      puts ("barrierattr_setpshared failed");
      exit (1);
    }

  if (pthread_barrier_init (b, &ba, 2) != 0)
    {
      puts ("barrier_init failed");
      exit (1);
    }

  if (pthread_barrierattr_destroy (&ba) != 0)
    {
      puts ("barrierattr_destroy failed");
      exit (1);
    }

  pid_t pid = fork ();
  if (pid == -1)
    {
      puts ("fork failed");
      exit (1);
    }

  if (pid == 0)
    receiver ();

  pthread_barrier_wait (b);

  /* Wait a bit more.  */
  struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
  nanosleep (&ts, NULL);

  /* Send the signal.  */
  puts ("sending the signal now");
  kill (pid, SIGINT);

  /* Wait for the process to terminate.  */
  int status;
  if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
    {
      puts ("wrong child reported terminated");
      exit (1);
    }

  if (!WIFSIGNALED (status))
    {
      puts ("child wasn't signalled");
      exit (1);
    }

  if (WTERMSIG (status) != SIGINT)
    {
      puts ("child not terminated with SIGINT");
      exit (1);
    }

  return 0;
}
开发者ID:JamesLinus,项目名称:glibc-mips,代码行数:93,代码来源:tst-signal1.c


示例15: smlt_platform_barrier_init

/**
 * @brief initializes a platform independent barrier for init synchro
 *
 * @param bar     pointer to the barrier memory area
 * @param attr    attributes for the barrier if any
 * @param count   number of threads
 *
 * @returns SMELT_SUCCESS or error value
 */
errval_t smlt_platform_barrier_init(smlt_platform_barrier_t *bar,
                                    smlt_platform_barrierattr_t *attr,
                                    uint32_t count)
{
    return pthread_barrier_init(bar, attr, count);
}
开发者ID:libsmelt,项目名称:libsmelt,代码行数:15,代码来源:barrier.c


示例16: MavlinkInit

int MavlinkInit(MavlinkStruct *Mavlink, AttitudeData *AttitudeDesire, AttitudeData *AttitudeMesure, char *TargetIP) {
	pthread_attr_t		attr;
	struct sched_param	param;
	int					minprio, maxprio;
	int retval = 0;

	Mavlink->AttitudeDesire = AttitudeDesire;
	Mavlink->AttitudeMesure = AttitudeMesure;

	strcpy(Mavlink->target_ip, TargetIP);
	Mavlink->sock 		= socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	memset((void *) &(Mavlink->locAddr), 0, sizeof(struct sockaddr_in));
	Mavlink->locAddr.sin_family 	 = AF_INET;
	Mavlink->locAddr.sin_addr.s_addr = INADDR_ANY;
	Mavlink->locAddr.sin_port 		 = htons(14551);

	/* Bind the socket to port 14551 - necessary to receive packets from qgroundcontrol */ 
	retval = bind(Mavlink->sock,(struct sockaddr *)&(Mavlink->locAddr), sizeof(struct sockaddr));
	if (retval == -1) {
		printf("%s : erreur bind échoué", __FUNCTION__);
		close(Mavlink->sock);
		return retval;
	} 

	/* Attempt to make it non blocking */
	if ((retval = fcntl(Mavlink->sock, F_SETFL, fcntl(Mavlink->sock, F_GETFL) | O_ASYNC | O_NONBLOCK)) < 0) {
		printf("%s : erreur ouverture non-bloquante", __FUNCTION__);
		close(Mavlink->sock);
		return retval;
	}

	memset(&Mavlink->gcAddr, 0, sizeof(struct sockaddr_in));
	Mavlink->gcAddr.sin_family 		= AF_INET;
	Mavlink->gcAddr.sin_addr.s_addr = inet_addr(Mavlink->target_ip);
	Mavlink->gcAddr.sin_port 		= htons(14550);

	pthread_attr_init(&attr);
	pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
	minprio = sched_get_priority_min(POLICY);
	maxprio = sched_get_priority_max(POLICY);
	pthread_attr_setschedpolicy(&attr, POLICY);
	param.sched_priority = minprio + (maxprio - minprio)/4;
	pthread_attr_setstacksize(&attr, THREADSTACK);
	pthread_attr_setschedparam(&attr, &param);

	printf("Creating Mavlink thread\n");
	pthread_barrier_init(&MavlinkStartBarrier, NULL, 3);

	sem_init(&MavlinkReceiveTimerSem, 0, 0);
	sem_init(&MavlinkStatusTimerSem, 0, 0);

	retval = pthread_create(&Mavlink->MavlinkReceiveTask, &attr, MavlinkReceiveTask, Mavlink);
	if (retval) {
		printf("pthread_create : Impossible de créer le thread Mavlink_reception_thread\n");
		return retval;
	}

	retval = pthread_create(&Mavlink->MavlinkStatusTask, &attr, MavlinkStatusTask, Mavlink);
	if (retval) {
		printf("pthread_create : Impossible de créer le thread MavlinkStatusTask\n");
		return retval;
	}

	pthread_attr_destroy(&attr);

	return retval;
}
开发者ID:limoges,项目名称:drone,代码行数:69,代码来源:Mavlink.c


示例17: main

int main(int argc, char **argv)
{
    char name[100];
    num_threads = sysconf(_SC_NPROCESSORS_ONLN);
    errval_t err;

    err = smlt_init(num_threads, true);
    if (smlt_err_is_fail(err)) {
	    printf("FAILED TO INITIALIZE !\n");
	    return 1;
    }

    for (int i = 4; i < num_threads+1; i++) {

        pthread_barrier_init(&bar, NULL, i);
        printf("Running %d cores\n", i);

        active_threads = i;
        uint32_t* cores = placement(i, false);
        for (int j = 0; j < i; j++) {
            printf("Cores[%d]=%d\n",j,cores[j]);
        }

        active_cores = cores;

        printf("Generating model\n");
        struct smlt_generated_model* model = NULL;
        err = smlt_generate_model(cores, i, NULL, &model);
        if (smlt_err_is_fail(err)) {
            exit(0);
        }

        printf("Generating topology\n");
        struct smlt_topology *topo = NULL;
        sprintf(name, "adaptivetree_rr%d",i);
        smlt_topology_create(model, name, &topo);
        active_topo = topo;

        printf("Generating context\n");
        err = smlt_context_create(topo, &context);
        if (smlt_err_is_fail(err)) {
            printf("FAILED TO INITIALIZE CONTEXT !\n");
            return 1;
        }
       
        printf("----------------------------------------\n");
        printf("Executing Barrier with %d cores rr\n", i);
        printf("----------------------------------------\n");
        
        struct smlt_node *node;
        for (uint64_t j = 0; j < i; j++) {
            node = smlt_get_node_by_id(cores[j]);
            err = smlt_node_start(node, barrier, (void*) j);
            if (smlt_err_is_fail(err)) {
                printf("Staring node failed \n");
            }   
        }

        for (int j=0; j < i; j++) {
            node = smlt_get_node_by_id(cores[j]);
            smlt_node_join(node);
        }
    }

    for (int i = 4; i < num_threads+1; i++) {

        pthread_barrier_init(&bar, NULL, i);

        active_threads = i;
        uint32_t* cores = placement(i, true);
        for (int j = 0; j < i; j++) {
            printf("Cores[%d]=%d\n",j,cores[j]);
        }

        active_cores = cores;

        struct smlt_generated_model* model = NULL;
        err = smlt_generate_model(cores, i, NULL, &model);
        if (smlt_err_is_fail(err)) {
            exit(0);
        }

        struct smlt_topology *topo = NULL;
        sprintf(name, "adaptivetree_fill%d",i);
        smlt_topology_create(model, name, &topo);
        active_topo = topo;

        err = smlt_context_create(topo, &context);
        if (smlt_err_is_fail(err)) {
            printf("FAILED TO INITIALIZE CONTEXT !\n");
            return 1;
        }
       
        printf("----------------------------------------\n");
        printf("Executing Barrier with %d cores fill\n", i);
        printf("----------------------------------------\n");
        
        struct smlt_node *node;
        for (uint64_t j = 0; j < i; j++) {
            node = smlt_get_node_by_id(cores[j]);
//.........这里部分代码省略.........
开发者ID:libsmelt,项目名称:libsmelt,代码行数:101,代码来源:bar-bench.c


示例18: main

/* The main test function. */
int main( int argc, char *argv[] )
{
	int ret = 0;
	pthread_t child;
	pthread_attr_t ta;
	pthread_barrier_t bar;

	struct sched_param sp;

	/* Initialize output routine */
	output_init();

	ret = pthread_barrier_init( &bar, NULL, 2 );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to init barrier" );
	}

	/* Create the attribute object with a known scheduling policy */
	ret = pthread_attr_init( &ta );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to initialize thread attribute" );
	}

	ret = pthread_attr_setinheritsched( &ta, PTHREAD_EXPLICIT_SCHED );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to set inherit sched" );
	}

	sp.sched_priority = sched_get_priority_min( SCHED_RR );

	if ( sp.sched_priority == -1 )
	{
		UNRESOLVED( errno, "Failed to get min priority" );
	}

	ret = pthread_attr_setschedparam( &ta, &sp );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to set attribute param" );
	}

	ret = pthread_attr_setschedpolicy( &ta, SCHED_RR );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to set attribute policy" );
	}

	/* Create the thread with this attribute */
	ret = pthread_create( &child, &ta, threaded, &bar );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "thread creation failed (you may need more priviledges)" );
	}

	/* Wait while the thread checks its policy
	  (we only check what is reported, not the real behavior) 
	 */
	check_param( child, SCHED_RR, sched_get_priority_min( SCHED_RR ) );


	ret = pthread_barrier_wait( &bar );

	if ( ( ret != 0 ) && ( ret != PTHREAD_BARRIER_SERIAL_THREAD ) )
	{
		UNRESOLVED( ret, "barrier wait failed" );
	}

	/* Change the threads policy */
	sp.sched_priority = sched_get_priority_min( SCHED_FIFO );

	if ( sp.sched_priority == -1 )
	{
		UNRESOLVED( errno, "Failed to get min priority" );
	}

	ret = pthread_setschedparam( child, SCHED_FIFO, &sp );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to change running thread's policy" );
	}

	ret = pthread_barrier_wait( &bar );

	if ( ( ret != 0 ) && ( ret != PTHREAD_BARRIER_SERIAL_THREAD ) )
	{
		UNRESOLVED( ret, "barrier wait failed" );
	}

	/* Wait while the thread checks its policy
//.........这里部分代码省略.........
开发者ID:SummerSnail2014,项目名称:haiku,代码行数:101,代码来源:1-3.c


示例19: do_test


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ pthread_barrier_wait函数代码示例发布时间:2022-05-30
下一篇:
C++ pthread_barrier_destroy函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap