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

C++ pthread_attr_init函数代码示例

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

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



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

示例1: uorb_receive_start

pthread_t
uorb_receive_start(void)
{
	/* --- SENSORS RAW VALUE --- */
	mavlink_subs.sensor_sub = orb_subscribe(ORB_ID(sensor_combined));
	/* rate limit set externally based on interface speed, set a basic default here */
	orb_set_interval(mavlink_subs.sensor_sub, 200);	/* 5Hz updates */

	/* --- ATTITUDE VALUE --- */
	mavlink_subs.att_sub = orb_subscribe(ORB_ID(vehicle_attitude));
	/* rate limit set externally based on interface speed, set a basic default here */
	orb_set_interval(mavlink_subs.att_sub, 200);	/* 5Hz updates */

	/* --- GPS VALUE --- */
	mavlink_subs.gps_sub = orb_subscribe(ORB_ID(vehicle_gps_position));
	orb_set_interval(mavlink_subs.gps_sub, 200);	/* 5Hz updates */

	/* --- HOME POSITION --- */
	mavlink_subs.home_sub = orb_subscribe(ORB_ID(home_position));
	orb_set_interval(mavlink_subs.home_sub, 1000);	/* 1Hz updates */

	/* --- SYSTEM STATE --- */
	status_sub = orb_subscribe(ORB_ID(vehicle_status));
	orb_set_interval(status_sub, 300);		/* max 3.33 Hz updates */

	/* --- RC CHANNELS VALUE --- */
	rc_sub = orb_subscribe(ORB_ID(rc_channels));
	orb_set_interval(rc_sub, 100);			/* 10Hz updates */

	/* --- RC RAW VALUE --- */
	mavlink_subs.input_rc_sub = orb_subscribe(ORB_ID(input_rc));
	orb_set_interval(mavlink_subs.input_rc_sub, 100);

	/* --- GLOBAL POS VALUE --- */
	mavlink_subs.global_pos_sub = orb_subscribe(ORB_ID(vehicle_global_position));
	orb_set_interval(mavlink_subs.global_pos_sub, 100);	/* 10 Hz active updates */

	/* --- LOCAL POS VALUE --- */
	mavlink_subs.local_pos_sub = orb_subscribe(ORB_ID(vehicle_local_position));
	orb_set_interval(mavlink_subs.local_pos_sub, 1000);	/* 1Hz active updates */

	/* --- GLOBAL SETPOINT VALUE --- */
	mavlink_subs.spg_sub = orb_subscribe(ORB_ID(vehicle_global_position_setpoint));
	orb_set_interval(mavlink_subs.spg_sub, 2000);	/* 0.5 Hz updates */

	/* --- LOCAL SETPOINT VALUE --- */
	mavlink_subs.spl_sub = orb_subscribe(ORB_ID(vehicle_local_position_setpoint));
	orb_set_interval(mavlink_subs.spl_sub, 2000);	/* 0.5 Hz updates */

	/* --- ATTITUDE SETPOINT VALUE --- */
	mavlink_subs.spa_sub = orb_subscribe(ORB_ID(vehicle_attitude_setpoint));
	orb_set_interval(mavlink_subs.spa_sub, 2000);	/* 0.5 Hz updates */

	/* --- RATES SETPOINT VALUE --- */
	mavlink_subs.rates_setpoint_sub = orb_subscribe(ORB_ID(vehicle_rates_setpoint));
	orb_set_interval(mavlink_subs.rates_setpoint_sub, 2000);  /* 0.5 Hz updates */

	/* --- ACTUATOR OUTPUTS --- */
	mavlink_subs.act_0_sub = orb_subscribe(ORB_ID(actuator_outputs_0));
	mavlink_subs.act_1_sub = orb_subscribe(ORB_ID(actuator_outputs_1));
	mavlink_subs.act_2_sub = orb_subscribe(ORB_ID(actuator_outputs_2));
	mavlink_subs.act_3_sub = orb_subscribe(ORB_ID(actuator_outputs_3));
	/* rate limits set externally based on interface speed, set a basic default here */
	orb_set_interval(mavlink_subs.act_0_sub, 100);	/* 10Hz updates */
	orb_set_interval(mavlink_subs.act_1_sub, 100);	/* 10Hz updates */
	orb_set_interval(mavlink_subs.act_2_sub, 100);	/* 10Hz updates */
	orb_set_interval(mavlink_subs.act_3_sub, 100);	/* 10Hz updates */

	/* --- ACTUATOR ARMED VALUE --- */
	mavlink_subs.armed_sub = orb_subscribe(ORB_ID(actuator_armed));
	orb_set_interval(mavlink_subs.armed_sub, 100);	/* 10Hz updates */

	/* --- MAPPED MANUAL CONTROL INPUTS --- */
	mavlink_subs.man_control_sp_sub = orb_subscribe(ORB_ID(manual_control_setpoint));
	/* rate limits set externally based on interface speed, set a basic default here */
	orb_set_interval(mavlink_subs.man_control_sp_sub, 100);	/* 10Hz updates */

	/* --- ACTUATOR CONTROL VALUE --- */
	mavlink_subs.actuators_effective_sub = orb_subscribe(ORB_ID_VEHICLE_ATTITUDE_CONTROLS_EFFECTIVE);
	orb_set_interval(mavlink_subs.actuators_effective_sub, 100);	/* 10Hz updates */

	mavlink_subs.actuators_sub = orb_subscribe(ORB_ID_VEHICLE_ATTITUDE_CONTROLS);
	orb_set_interval(mavlink_subs.actuators_sub, 100);	/* 10Hz updates */

	/* --- DEBUG VALUE OUTPUT --- */
	mavlink_subs.debug_key_value = orb_subscribe(ORB_ID(debug_key_value));
	orb_set_interval(mavlink_subs.debug_key_value, 100);	/* 10Hz updates */

	/* --- FLOW SENSOR --- */
	mavlink_subs.optical_flow = orb_subscribe(ORB_ID(optical_flow));
	orb_set_interval(mavlink_subs.optical_flow, 200); 	/* 5Hz updates */

	/* --- AIRSPEED / VFR / HUD --- */
	mavlink_subs.airspeed_sub = orb_subscribe(ORB_ID(airspeed));
	orb_set_interval(mavlink_subs.airspeed_sub, 200); 	/* 5Hz updates */

	/* start the listener loop */
	pthread_attr_t uorb_attr;
	pthread_attr_init(&uorb_attr);

//.........这里部分代码省略.........
开发者ID:avnishks,项目名称:px4,代码行数:101,代码来源:orb_listener.c


示例2: conpar_threads_wrapper

static int conpar_threads_wrapper(integer nov, integer na, integer nra, integer nca, doublereal ***a, integer ncb, doublereal ***b, integer nrc, doublereal ***c, doublereal **d, integer *irf, integer *icf)

{
  conpar_parallel_arglist *data;
  int i,j,k;
  pthread_t *th;
  void * retval;
  pthread_attr_t attr;
  int retcode;
#ifdef USAGE
  struct timeval *pthreads_wait;
  time_start(&pthreads_wait);
#endif
  
  data = (conpar_parallel_arglist *)malloc(sizeof(conpar_parallel_arglist)*global_num_procs);
  th = (pthread_t *)malloc(sizeof(pthread_t)*global_num_procs);

  for(i=0;i<global_num_procs;i++) {
    integer loop_start, loop_end;
    
    /*start and end of the computed loop*/
    loop_start = (i*na)/global_num_procs;
    loop_end = ((i+1)*na)/global_num_procs;
    
    /*3D Arrays*/ 
    data[i].a = a + loop_start;
    data[i].b = b + loop_start;
    data[i].c = c + loop_start;
    
    /*2D Arrays*/
    data[i].irf = irf + loop_start*nra;
    data[i].icf = icf + loop_start*nca;
    
    /*Scalars*/
    data[i].nrc = nrc;
    data[i].ncb = ncb;
    data[i].nov = nov;
    data[i].nra = nra;
    data[i].nca = nca;
    
    data[i].na = loop_end - loop_start;
    
  }
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
  for(i=0;i<global_num_procs;i++) {
    retcode = pthread_create(&th[i], &attr, conpar_threads_process, (void *) &data[i]);
    if (retcode != 0) fprintf(stderr, "create %d failed %d\n", i, retcode);
  }
  
  for(i=0;i<global_num_procs;i++) {
    retcode = pthread_join(th[i], &retval);
    if (retcode != 0) fprintf(stderr, "join %d failed %d\n", i, retcode);
    /* This is were we sum into the global copy of the d array */  
    for(j=0;j<nrc;j++)
      for (k=0; k<ncb;k++)
	d[j][k] += data[i].d[j][k];
    free_dmatrix(data[i].d);
  }  

  free(data);
  free(th);
#ifdef USAGE
  time_end(pthreads_wait,"conpar pthreads wrapper",fp9);
#endif
  return 0;
}
开发者ID:pratikmallya,项目名称:Multifario,代码行数:67,代码来源:conpar.c


示例3: ThreadAttr

 ThreadAttr():
     thread_stack_size(262144)
 {
     pthread_attr_init(&attr);
     pthread_attr_setstacksize(&attr, thread_stack_size);
 }
开发者ID:BackupTheBerlios,项目名称:xicorr-svn,代码行数:6,代码来源:thread.cpp


示例4: main


//.........这里部分代码省略.........
   // perror("listen");
    exit( -1 );
  }
  if(cases == 0){
    while ( 1 ) {

      // Accept incoming connections
      struct sockaddr_in clientIPAddress;
      int alen = sizeof( clientIPAddress );
      int slaveSocket = accept( masterSocket,
  			      (struct sockaddr *)&clientIPAddress,
  			      (socklen_t*)&alen);

     
     /* if ( slaveSocket < 0  ) {
        perror( "accept" );
        exit( -1 );
      }*/
       
      // Process request.
      processTimeRequest( slaveSocket );

      // Close socket
      close( slaveSocket );
      }
  } else if(cases == 1){
    printf("case 1\n"); 

    while ( 1 ) {

      // Accept incoming connections
      struct sockaddr_in clientIPAddress;
      int alen = sizeof( clientIPAddress );
      int slaveSocket = accept( masterSocket,
              (struct sockaddr *)&clientIPAddress,
              (socklen_t*)&alen);

      if ( slaveSocket < 0 ) {
      //  perror( "accept" );
        exit( -1 );
      }
      pid_t f = fork();
      if(f == 0){
      // Process request.
      processTimeRequest( slaveSocket );
      close( slaveSocket );
      exit(EXIT_SUCCESS);
      }
      /* if(slaveSocket == -1 && errno == EINTR){
          continue;
        }*/

      close(slaveSocket);
      
      }
  }
  else if(cases == 2){
    printf("case 2\n"); 

    while ( 1 ) {

      // Accept incoming connections
      struct sockaddr_in clientIPAddress;
      int alen = sizeof( clientIPAddress );
      int slaveSocket = accept( masterSocket,
              (struct sockaddr *)&clientIPAddress,
              (socklen_t*)&alen);

      if ( slaveSocket < 0 ) {
      //  perror( "accept" );
        exit( -1 );
      }else{
        pthread_t t1;      
        pthread_attr_t attr;

        pthread_attr_init( &attr ); 
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      // Process request.
        pthread_create(&t1, &attr, (void * (*) (void*)) processTimeRequest,(void *) slaveSocket);
//      processTimeRequest( slaveSocket );
       // close( slaveSocket );
       // pthread_join(t1,NULL);    
    	}
      
      }
  }else if(cases == 3){
    printf("case3\n");
    pthread_t tid[5];
    pthread_attr_t attr;

    pthread_attr_init( &attr ); 
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    for (int i = 0; i < 5; i++)
    {
       pthread_create(&tid[i], &attr, (void * (*) (void*)) poolRequest,(void *) masterSocket);
    }

    pthread_join(tid[0],NULL);
  }
}
开发者ID:akandi6,项目名称:HTTP-Server,代码行数:101,代码来源:myhttpd.cpp


示例5: sigfillset

void *clamukofsth(void *arg)
{
	struct thrarg *tharg = (struct thrarg *) arg;
	sigset_t sigset;
        struct sigaction act;
	pthread_t *clamuko_pids = NULL;
	const char *groupname = "ClamAV";
	int count;
	int started;

    /* is another server thread already working? */
    if(pthread_mutex_trylock(&running_mutex))
	return NULL;

    /* ignore all signals except SIGUSR1 */
    sigfillset(&sigset);
    sigdelset(&sigset, SIGUSR1);
    /* The behavior of a process is undefined after it ignores a
     * SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal */
    sigdelset(&sigset, SIGFPE);
    sigdelset(&sigset, SIGILL);
    sigdelset(&sigset, SIGSEGV);
#ifdef SIGBUS
    sigdelset(&sigset, SIGBUS);
#endif
    pthread_sigmask(SIG_SETMASK, &sigset, NULL);

    count = optget(tharg->opts, "ClamukoScannerCount")->numarg;
    if(count < 1) goto out;

    clamuko_pids = calloc(count, sizeof(pthread_t));
    if(!clamuko_pids) goto out;

    if(setup_shutdown_handle(groupname)) goto out;

    act.sa_handler = clamuko_exit;
    sigfillset(&(act.sa_mask));
    sigaction(SIGUSR1, &act, NULL);
    sigaction(SIGSEGV, &act, NULL);

    for(started = 0; started < count; started++) {
	pthread_attr_t clamuko_attr;

	if(pthread_attr_init(&clamuko_attr)) break;
	pthread_attr_setdetachstate(&clamuko_attr, PTHREAD_CREATE_JOINABLE);
	if(pthread_create(&clamuko_pids[started], &clamuko_attr,
			  clamuko_scanth, tharg)) break;
	logg("Clamuko: Started scanner thread %d.\n", started);
    }

    pthread_cond_wait(&shutdown_cond, &running_mutex);
    logg("Clamuko: Stop signal received.\n");

    shutdown_clamuko();

    for(started-- ; started >= 0; started--) {
	logg("Clamuko: Waiting for scanner thread %d to finish.\n", started);
	pthread_join(clamuko_pids[started], NULL);
    }

    logg("Clamuko: Stopped.\n");
out:
    if(clamuko_pids) free(clamuko_pids);
    pthread_mutex_unlock(&running_mutex);
    return NULL;
}
开发者ID:bogus,项目名称:mydlp-host-win32,代码行数:66,代码来源:clamukofs.c


示例6: bu_parallel


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

	/* Check to see if this is one the threads we created */
	for (i = 0; i < nthreadc; i++) {
	    if (thread_tbl[i] == thread) {
		thread_tbl[i] = (rt_thread_t)-1;
		nthreade++;
		break;
	    }
	}

	if ((thread_tbl[i] != (rt_thread_t)-1) && i < nthreadc) {
	    bu_log("bu_parallel(): unknown thread %d completed.\n",
		   thread);
	}

	if (UNLIKELY(bu_debug & BU_DEBUG_PARALLEL))
	    bu_log("bu_parallel(): thread completed: (thread: %d)\t(loop:%d) (nthreadc:%zu) (nthreade:%zu)\n",
		   thread, x, nthreadc, nthreade);
    }

    if (UNLIKELY(bu_debug & BU_DEBUG_PARALLEL))
	bu_log("bu_parallel(): %zu threads created.  %zud threads exited.\n", nthreadc, nthreade);
#  endif	/* SUNOS */

#  if defined(HAVE_PTHREAD_H)

    /* Create the posix threads.
     *
     * Start at 1 so we can treat the parent as thread 0.
     */
    nthreadc = 0;
    for (x = 0; x < ncpu; x++) {
	pthread_attr_t attrs;
	pthread_attr_init(&attrs);
	pthread_attr_setstacksize(&attrs, 10*1024*1024);

	parallel_wait_for_slot(throttle, parent, ncpu);

	if (pthread_create(&thread, &attrs, (void *(*)(void *))parallel_interface_arg, &thread_context[x])) {
	    bu_log("ERROR: bu_parallel: pthread_create(0x0, 0x0, 0x%lx, 0x0, 0, %p) failed for processor thread # %zu\n",
		   (unsigned long int)parallel_interface_arg, (void *)&thread, x);

	} else {
	    if (UNLIKELY(bu_debug & BU_DEBUG_PARALLEL)) {
		bu_log("bu_parallel(): created thread: (thread: %p) (loop: %zu) (nthreadc: %zu)\n",
		       (void*)thread, x, nthreadc);
	    }
	    thread_tbl[nthreadc] = thread;
	    nthreadc++;
	}

	/* done with the attributes after create */
	pthread_attr_destroy(&attrs);
    }

    if (UNLIKELY(bu_debug & BU_DEBUG_PARALLEL)) {
	for (i = 0; i < nthreadc; i++) {
	    bu_log("bu_parallel(): thread_tbl[%d] = %p\n", i, (void *)thread_tbl[i]);
	}
#    ifdef SIGINFO
	/* may be BSD-only (calls _thread_dump_info()) */
	raise(SIGINFO);
#    endif
    }

    /*
开发者ID:behollis,项目名称:brlcad-svn-rev65072-gsoc2015,代码行数:67,代码来源:parallel.c


示例7: WRAPPER

WRAPPER(int, pthread_create, pthread_t *thr, const pthread_attr_t *attr, 
	 void * (*start) (void *), void *arg)
{
  DECLARE(int, munmap, void *p, size_t l);
  DECLARE(void *, mmap, void *p, size_t l, int prot, int flags, int fd, off_t of);
  DECLARE(int, pthread_create, pthread_t *thr, const pthread_attr_t *attr, 
	  void * (*start) (void *), void *arg);
  int result;
  pthread_attr_t override_attr;
  void *override_stack;
  size_t override_stacksize;
  void *override_stack_alloc = (void *) 0;
  size_t override_stacksize_alloc = 0;
  unsigned i;

  TRACE ("pthread_create\n");

  /* Garbage-collect dead threads' stacks.  */
  LOCKTH ();
  for (i = 0; i < LIBMUDFLAPTH_THREADS_MAX; i++)
    {
      struct pthread_info *pi = & __mf_pthread_info [i];
      if (! pi->used_p)
	continue;
      if (! pi->dead_p)
	continue;

      /* VERBOSE_TRACE ("thread %u pi %p stack cleanup deferred (%u)\n",
	 (unsigned) pi->self, pi, pi->dead_p); */
	      
      /* Delay actual deallocation by a few cycles, try to discourage the
	 race mentioned at the end of __mf_pthread_spawner().  */
      if (pi->dead_p)
	pi->dead_p ++;
      if (pi->dead_p >= 10 /* XXX */)
	{
	  if (pi->stack)
	    CALL_REAL (munmap, pi->stack_alloc, pi->stack_size_alloc);

	  VERBOSE_TRACE ("slot %u freed, stack %p\n", i, pi->stack_alloc);
	  memset (pi, 0, sizeof (*pi));

	  /* One round of garbage collection is enough.  */
	  break;
	}
    }
  UNLOCKTH ();

  /* Let's allocate a stack for this thread, if one is not already
     supplied by the caller.  We don't want to let e.g. the
     linuxthreads manager thread do this allocation.  */
  if (attr != NULL)
    override_attr = *attr;
  else
    pthread_attr_init (& override_attr);

  /* Get supplied attributes, if any.  */
  /* XXX: consider using POSIX2K attr_getstack() */
  if (pthread_attr_getstackaddr (& override_attr, & override_stack) != 0 ||
      pthread_attr_getstacksize (& override_attr, & override_stacksize) != 0)
    {
      override_stack = NULL;
      override_stacksize = 0;
    }

  /* Do we need to allocate the new thread's stack?  */
  if (__mf_opts.thread_stack && override_stack == NULL)
    {
      uintptr_t alignment = 256; /* power of two */

      /* Perturb the initial stack addresses slightly, to encourage
	 threads to have nonconflicting entries in the lookup cache
	 for their tracked stack objects.  */
      static unsigned perturb = 0;
      const unsigned perturb_delta = 32;
      const unsigned perturb_count = 16;
      perturb += perturb_delta;
      if (perturb > perturb_delta*perturb_count) perturb = 0;

      /* Use glibc x86 defaults */
/* Should have been defined in <limits.h> */
#ifndef PTHREAD_STACK_MIN
#define PTHREAD_STACK_MIN 65536
#endif
      override_stacksize = max (PTHREAD_STACK_MIN, __mf_opts.thread_stack * 1024);


#if defined(MAP_ANONYMOUS)
#define MF_MAP_ANON MAP_ANONYMOUS
#elif defined(MAP_ANON)
#define MF_MAP_ANON MAP_ANON
#endif

#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif

#ifdef MF_MAP_ANON
      override_stack = CALL_REAL (mmap, NULL, override_stacksize, 
				  PROT_READ|PROT_WRITE, 
//.........这里部分代码省略.........
开发者ID:DJHartley,项目名称:iphone-dev,代码行数:101,代码来源:mf-hooks3.c


示例8: message


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

	// save the socket ID for the next calls
	fp->rmt_sockctrl= sockctrl;	// Needed to send an error on the ctrl connection

	// Now I can set the filter
	if ( daemon_unpackapplyfilter(fp, &nread, &plen, errbuf) )
		goto error;


	// Now, I can send a RPCAP start capture reply message
	if ( sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
		RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE) == -1)
		goto error;

	rpcap_createhdr( (struct rpcap_header *) sendbuf, RPCAP_MSG_STARTCAP_REPLY, 0, sizeof(struct rpcap_startcapreply) );

	startcapreply= (struct rpcap_startcapreply *) &sendbuf[sendbufidx];
	
	if ( sock_bufferize(NULL, sizeof(struct rpcap_startcapreply), NULL,
		&sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errbuf, PCAP_ERRBUF_SIZE) == -1)
		goto error;

	memset(startcapreply, 0, sizeof(struct rpcap_startcapreply) );
	startcapreply->bufsize= htonl(fp->bufsize);

	if (!serveropen_dp)
	{
		unsigned short port = (unsigned short)strtoul(portdata,NULL,10);
		startcapreply->portdata= htons(port);
	}

	if ( sock_send(sockctrl, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
		goto error;

	if (!serveropen_dp)
	{
	SOCKET socktemp;	// We need another socket, since we're going to accept() a connection

		// Connection creation
		saddrlen = sizeof(struct sockaddr_storage);

		socktemp= accept(sockdata, (struct sockaddr *) &saddr, &saddrlen);
		
		if (socktemp == -1)
		{
			sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
			goto error;
		}

		// Now that I accepted the connection, the server socket is no longer needed
		sock_close(sockdata, errbuf, PCAP_ERRBUF_SIZE);
		sockdata= socktemp;
	}

	fp->rmt_sockdata= sockdata;

	/* GV we need this to create the thread as detached. */
	/* GV otherwise, the thread handle is not destroyed  */
	pthread_attr_init(&detachedAttribute); 
	pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);
	
	// Now we have to create a new thread to receive packets
	if ( pthread_create(threaddata, &detachedAttribute, (void *) daemon_thrdatamain, (void *) fp) )
	{
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error creating the data thread");
		pthread_attr_destroy(&detachedAttribute);
		goto error;
	}

	pthread_attr_destroy(&detachedAttribute);
	// Check if all the data has been read; if not, discard the data in excess
	if (nread != plen)
		sock_discard(sockctrl, plen - nread, NULL, 0);

	return fp;

error:
	rpcap_senderror(sockctrl, errbuf, PCAP_ERR_STARTCAPTURE, NULL);

	if (addrinfo)
		freeaddrinfo(addrinfo);

	if (threaddata)
		pthread_cancel(*threaddata);

	if (sockdata)
		sock_close(sockdata, NULL, 0);

	// Check if all the data has been read; if not, discard the data in excess
	if (nread != plen)
		sock_discard(sockctrl, plen - nread, NULL, 0);

	if (fp)
	{
		pcap_close(fp);
		fp= NULL;
	}

	return NULL;
}
开发者ID:CM44,项目名称:npcap,代码行数:101,代码来源:daemon.c


示例9: main

int
main(int argc, char* argv[])
{
    pthread_t*     threads;
    int*           tids;
    pthread_attr_t attr;
    int            ret;
    int            mix_sig, i;

    /* Parse arguments */
    if(argc < 2) {
        fprintf(stderr, "%s <numProcesors> <maxLoop>\n", argv[0]);
        exit(1);
    }
    NumProcs = atoi(argv[1]);
    assert(NumProcs > 0 && NumProcs <= 32);
    if (argc >= 3) {
        MaxLoop = atoi(argv[2]);
        assert(MaxLoop > 0);
    }

    /* Initialize the mix array */
    for(i = 0; i < MAX_ELEM; i++) {
        m[i].value = mix(i,i);
    }

    /* Initialize barrier counter */
    startCounter = NumProcs;

    /* Initialize array of thread structures */
    threads = (pthread_t *) malloc(sizeof(pthread_t) * NumProcs);
    assert(threads != NULL);
    tids = (int *) malloc(sizeof (int) * NumProcs);
    assert(tids != NULL);

    /* Initialize thread attribute */
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

    ret = pthread_mutex_init(&threadLock, NULL);
    assert(ret == 0);

    for(i=0; i < NumProcs; i++) {
        /* ************************************************************
         * pthread_create takes 4 parameters
         *  p1: threads(output)
         *  p2: thread attribute
         *  p3: start routine, where new thread begins
         *  p4: arguments to the thread
         * ************************************************************ */
        tids[i] = i+1;
        ret = pthread_create(&threads[i], &attr, ThreadBody, &tids[i]);
        assert(ret == 0);
    }

    /* Wait for each of the threads to terminate */
    for(i=0; i < NumProcs; i++) {
        ret = pthread_join(threads[i], NULL);
        assert(ret == 0);
    }

    /* compute the result */
    mix_sig = sig[0];
    for(i = 1; i < NumProcs ; i++) {
        mix_sig = mix(sig[i], mix_sig);
    }

    /* end of parallel phase */

    /* ************************************************************
     * print results
     *  1. mix_sig  : deterministic race?
     *  2. &mix_sig : deterministic stack layout?
     *  3. malloc   : deterministic heap layout?
     * ************************************************************ */
    printf("\n\nShort signature: %08x @ %p @ %p\n\n\n",
           mix_sig, &mix_sig, (void*)malloc(PAGE_SIZE/5));
    fflush(stdout);
    usleep(5);

    pthread_mutex_destroy(&threadLock);
    pthread_attr_destroy(&attr);

    return 0;
}
开发者ID:supermartian,项目名称:racey-suite,代码行数:85,代码来源:racey-freqsyscall.c


示例10: vcos_thread_create

VCOS_STATUS_T vcos_thread_create(VCOS_THREAD_T *thread,
                                 const char *name,
                                 VCOS_THREAD_ATTR_T *attrs,
                                 VCOS_THREAD_ENTRY_FN_T entry,
                                 void *arg)
{
   VCOS_STATUS_T st;
   pthread_attr_t pt_attrs;
   VCOS_THREAD_ATTR_T *local_attrs = attrs ? attrs : &default_attrs;
   int rc;

   vcos_assert(thread);
   memset(thread, 0, sizeof(VCOS_THREAD_T));

   rc = pthread_attr_init(&pt_attrs);
   if (rc < 0)
      return VCOS_ENOMEM;

   st = vcos_semaphore_create(&thread->suspend, NULL, 0);
   if (st != VCOS_SUCCESS)
   {
      pthread_attr_destroy(&pt_attrs);
      return st;
   }

   pthread_attr_setstacksize(&pt_attrs, local_attrs->ta_stacksz);
#if VCOS_CAN_SET_STACK_ADDR
   if (local_attrs->ta_stackaddr)
   {
      pthread_attr_setstackaddr(&pt_attrs, local_attrs->ta_stackaddr);
   }
#else
   vcos_demand(local_attrs->ta_stackaddr == 0);
#endif

   /* pthread_attr_setpriority(&pt_attrs, local_attrs->ta_priority); */

   vcos_assert(local_attrs->ta_stackaddr == 0); /* Not possible */

   thread->entry = entry;
   thread->arg = arg;
   thread->legacy = local_attrs->legacy;

   strncpy(thread->name, name, sizeof(thread->name));
   thread->name[sizeof(thread->name)-1] = '\0';
   memset(thread->at_exit, 0, sizeof(thread->at_exit));

   rc = pthread_create(&thread->thread, &pt_attrs, vcos_thread_entry, thread);

   pthread_attr_destroy(&pt_attrs);

   if (rc < 0)
   {
      vcos_semaphore_delete(&thread->suspend);
      return VCOS_ENOMEM;
   }
   else
   {
      return VCOS_SUCCESS;
   }
}
开发者ID:Allroad,项目名称:userland,代码行数:61,代码来源:vcos_pthreads.c


示例11: pthread_create

int pthread_create(pthread_t            *tid,
                   const pthread_attr_t *attr, 
                   void *(*start) (void *), void *parameter)
{
    int result;
    void *stack;
    char name[RT_NAME_MAX];
    static rt_uint16_t pthread_number = 0;
    _pthread_data_t *ptd;

    /* tid shall be provided */
    RT_ASSERT(tid != RT_NULL);

    /* allocate posix thread data */
    ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
    if (ptd == RT_NULL)
        return ENOMEM;
    /* clean posix thread data memory */
    rt_memset(ptd, 0, sizeof(_pthread_data_t));
    ptd->canceled = 0;
    ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
    ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
    ptd->magic = PTHREAD_MAGIC;

    if (attr != RT_NULL)
        ptd->attr = *attr;
    else 
    {
        /* use default attribute */
        pthread_attr_init(&ptd->attr);
    }

    rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
    if (ptd->attr.stack_base == 0)
    {
        stack = (void*)rt_malloc(ptd->attr.stack_size);
    }
    else
        stack = (void*)(ptd->attr.stack_base);

    if (stack == RT_NULL) 
    {
        rt_free(ptd);

        return ENOMEM;
    }

    /* pthread is a static thread object */
    ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
    if (ptd->tid == RT_NULL)
    {
        if (ptd->attr.stack_base == 0)
            rt_free(stack);
        rt_free(ptd);

        return ENOMEM;
    }

    if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
    {
        ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
        if (ptd->joinable_sem == RT_NULL)
        {
            if (ptd->attr.stack_base != 0)
                rt_free(stack);
            rt_free(ptd);

            return ENOMEM;
        }
    }
    else
        ptd->joinable_sem = RT_NULL;

    /* set parameter */
    ptd->thread_entry = start;
    ptd->thread_parameter = parameter;

    /* initial this pthread to system */
    if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, 
        stack, ptd->attr.stack_size, 
        ptd->attr.priority, 5) != RT_EOK)
    {
        if (ptd->attr.stack_base == 0)
            rt_free(stack);
        if (ptd->joinable_sem != RT_NULL)
            rt_sem_delete(ptd->joinable_sem);
        rt_free(ptd);

        return EINVAL;
    }

    /* set pthread id */
    *tid = ptd->tid;

    /* set pthread cleanup function and ptd data */
    (*tid)->cleanup = _pthread_cleanup;
    (*tid)->user_data = (rt_uint32_t)ptd;

    /* start thread */
    result = rt_thread_startup(*tid);
//.........这里部分代码省略.........
开发者ID:chinesebear,项目名称:rt-thread,代码行数:101,代码来源:pthread.c


示例12: main

main()
{

  int fromlen;
  int i, s, ns, len;
  struct sockaddr_un saun, fsaun;
  
  for(i=0 ;i<3; i++)
    idleThread[i] = i;

  for(i=0; i<3;i++)
    initialiseConnection(i);

  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("server: socket");
    exit(1);
  }

  saun.sun_family = AF_UNIX;
  strcpy(saun.sun_path, ADDRESS);

  unlink(ADDRESS);
  len = sizeof(saun.sun_family) + strlen(saun.sun_path);

  if (bind(s, &saun, len) < 0) {
    perror("server: bind");
    exit(1);
  }

  if (listen(s, 5) < 0) {
    perror("server: listen");
    exit(1);
  }
  
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
  
  while(1){
    
    ns = accept(s, (struct sockadd*)&fsaun, &fromlen);
    if (ns < 0) {
      perror("server: accept");
      exit(1);
    }
    
    int curthread = comeBackYesterday();
    //int curthread = merryGoRound();
    int par[2]={ns,curthread};
    active++;

    if(pthread_create(&thread[curthread],NULL,connectToClient,(void*)par)){
      perror("server: cant create thread");
      exit(1);
    }
    printf("Sent to server : %d\n",curthread);
    pthread_detach(thread[curthread]);
    
    /*while(active>=No_of_servers){
      //puts("come back yesterday");
    }*/
  }
  
  if (ns < 0) {
    perror("server: accept");
    exit(1);
  }

  close(s);
  return 0;
}
开发者ID:vaibhavdaga,项目名称:Load-Balancer,代码行数:70,代码来源:balancer.c


示例13: pthread_attr_init

TSExec::TSExec() {
	pthread_attr_init(&fAttr);
}
开发者ID:errato,项目名称:ephenation-client,代码行数:3,代码来源:TSExec.cpp


示例14: program


//.........这里部分代码省略.........
+---------------------------------------------------------------------*/

        /*
         * Create threads array...
         */
       writer_th = (pthread_t *) malloc ((size_t) (num_writers * sizeof (pthread_t)));
       reader_th = (pthread_t *) malloc ((size_t) (num_writers * num_readers * sizeof (pthread_t)));
	/*
	 * Initializes mutexes and sets their attributes
	 */
        for (i=0; i<num_writers; i++) {

	if (pthread_mutex_init(&mutex_r[i] , (pthread_mutexattr_t *)NULL) != 0)
		sys_error ("Can't initialize mutex_r", __LINE__);

	if (pthread_mutex_init (&cond_mutex[i], (pthread_mutexattr_t *)NULL))
		sys_error ("Can't initialize cond_mutex", __LINE__);
	if (pthread_cond_init (&cond_var[i], (pthread_condattr_t *)NULL))
		sys_error ("cond_init(&cond_var) failed", __LINE__);
        /*
         * lock the access to the shared memory data segment --
         * get lock now to insure the writer gets first access to the segment.
         *
         */

	thread_hold[i]=1;

	}

	/*
	 * Creates a thread attributes object and initializes it
	 * with default values.
	*/
        if (pthread_attr_init(&newattr))
                sys_error ("attr_init(&newattr) failed", __LINE__);
	/*
	 * Sets the value of the detachstate attribute of a thread attributes
	 * object :
	 * PTHREAD_CREATE_UNDETACHED	Specifies that the thread will be
	 * created in undetached state.
	*/
#ifdef _LINUX_
	// the DEFAULT state for linux pthread_create is to be "undetatched" or joinable
	/* if (pthread_attr_setdetachstate (&newattr, PTHREAD_CREATE_JOINABLE))
                sys_error ("attr_setdetachstate(&newattr) failed", __LINE__);*/
#else
        if (pthread_attr_setdetachstate (&newattr, PTHREAD_CREATE_UNDETACHED))
                sys_error ("attr_setdetachstate(&newattr) failed", __LINE__);
#endif

        /*
         * Create all num_writers threads .  Each writer thread will fill
	 * the "scratch" shared memory segment (shmptr) up with data and
         * will store the result in cksum array accessible by the main.
         */

        for (i = 0; i < num_writers; i++)
        {
                if (pthread_create (&writer_th[i], &newattr, writer, (void *) (long)i))
                        sys_error ("writer: pthread_create failed", __LINE__);

        /*
         * Create all num_readers threads .  Each reader thread will compute
         * the checksum of the shared memory segment (shmptr) and will store
         * the result in the other shared memory segment (checksum)accessible
         * by the writer.
开发者ID:ystk,项目名称:debian-ltp,代码行数:67,代码来源:shmem_test_07.c


示例15: main

int main(int argc, const char * argv[])
{
    
    pthread_t tid1, tid2;
    param p1, p2;
    pthread_attr_t attr;
    int detachstate, scope, sched, algoritmo;
    size_t stacksize;
    
    
    p1.tid = 1;
    strcpy(p1.nombre, "Hilo 1");
    
    p2.tid = 2;
    strcpy(p2.nombre, "Hilo 2");
    
    pthread_key_create(&tsd, NULL);
    
    pthread_attr_init(&attr);
    
    pthread_attr_getdetachstate(&attr, &detachstate);
    pthread_attr_getscope(&attr, &scope);
    pthread_attr_getstacksize(&attr, &stacksize);
    pthread_attr_getschedpolicy(&attr, &algoritmo);
    pthread_attr_getinheritsched(&attr, &sched);
    
    printf("Hilo 1 creado con valores siguientes: \n");
    
    printf("Valores iniciales de atributos: \n");
    printf("DETACHSTATE = %d \n", detachstate);
    printf("SCOPE = %d \n", scope);
    printf("STACKSIZE = %zu bytes\n", stacksize);
    printf("SCHEDPOLICY = %d \n", algoritmo);
    printf("INHERSCHED = %d \n", sched);
    
    pthread_create(&tid1, &attr, hilo, (void *) &p1);
    
    
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
    
    printf("Hilo 2 creado con valores siguientes: \n");
    
    pthread_attr_getdetachstate(&attr, &detachstate);
    pthread_attr_getscope(&attr, &scope);
    pthread_attr_getstacksize(&attr, &stacksize);
    pthread_attr_getschedpolicy(&attr, &algoritmo);
    pthread_attr_getinheritsched(&attr, &sched);
    
    printf("Valores iniciales de atributos: \n");
    printf("DETACHSTATE = %d \n", detachstate);
    printf("SCOPE = %d \n", scope);
    printf("STACKSIZE = %zu bytes\n", stacksize);
    printf("SCHEDPOLICY = %d \n", algoritmo);
    printf("INHERSCHED = %d \n", sched);
    
    pthread_create(&tid2, &attr, hilo, (void *) &p2);
    
    if (pthread_equal(tid1, tid2)) {
        printf(" Son iguales \n");
    }
    else {
        printf("Son diferentes \n");
    }
    
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    
    pthread_key_delete(tsd);
    
    pthread_attr_destroy(&attr);
    
    pthread_exit(NULL);
    
    return 0;
}
开发者ID:vcubells,项目名称:tc2025,代码行数:78,代码来源:main.c


示例16: priority_inheritance

void priority_inheritance(void)
{
#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD)
  pthread_t lowpri[NLOWPRI_THREADS];
  pthread_t medpri;
  pthread_t highpri[NHIGHPRI_THREADS];
  pthread_addr_t result;
  pthread_attr_t attr;
  struct sched_param sparam;
  int my_pri;
  int status;
  int i;

  printf("priority_inheritance: Started\n");

  g_middlestate = NOTSTARTED;
  for (i = 0; i < NHIGHPRI_THREADS; i++) g_highstate[i] = NOTSTARTED;
  for (i = 0; i < NLOWPRI_THREADS; i++)  g_lowstate[i]  = NOTSTARTED;

  status = sched_getparam (getpid(), &sparam);
  if (status != 0)
    {
      printf("priority_inheritance: sched_getparam failed\n");
      sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY;
    }
  my_pri  = sparam.sched_priority;

  g_highpri = sched_get_priority_max(SCHED_FIFO);
  g_lowpri = sched_get_priority_min(SCHED_FIFO);
  g_medpri = my_pri - 1;

  sem_init(&g_sem, 0, NLOWPRI_THREADS);
  dump_nfreeholders("priority_inheritance:");

  /* Start the low priority threads */

  for (i = 0; i < NLOWPRI_THREADS; i++)
    {
      int threadno = i+1;
      printf("priority_inheritance: Starting lowpri_thread-%d (of %d) at %d\n",
             threadno, NLOWPRI_THREADS, g_lowpri);
      status = pthread_attr_init(&attr);
      if (status != 0)
        {
          printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status);
        }
      sparam.sched_priority = g_lowpri;
      status = pthread_attr_setschedparam(&attr,& sparam);
      if (status != OK)
        {
          printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status);
        }
      else
        {
          printf("priority_inheritance: Set lowpri_thread-%d priority to %d\n",
                 threadno, sparam.sched_priority);
        }

      status = pthread_create(&lowpri[i], &attr, lowpri_thread, (void*)threadno);
      if (status != 0)
        {
          printf("priority_inheritance: pthread_create failed, status=%d\n", status);
        }
    }
  printf("priority_inheritance: Waiting...\n");
  sleep(2);
  dump_nfreeholders("priority_inheritance:");

  /* Start the medium priority thread */

  printf("priority_inheritance: Starting medpri_thread at %d\n", g_medpri);
  status = pthread_attr_init(&attr);
  if (status != 0)
    {
      printf("priority_inheritance: pthread_attr_init failed, status=%d\n", status);
    }

  sparam.sched_priority = g_medpri;
  status = pthread_attr_setschedparam(&attr,& sparam);
  if (status != OK)
    {
      printf("priority_inheritance: pthread_attr_setschedparam failed, status=%d\n", status);
    }
  else
    {
      printf("priority_inheritance: Set medpri_thread priority to %d\n", sparam.sched_priority);
    }
  fflush(stdout);

  status = pthread_create(&medpri, &attr, medpri_thread, NULL);
  if (status != 0)
    {
      printf("priority_inheritance: pthread_create failed, status=%d\n", status);
    }
  printf("priority_inheritance: Waiting...\n");
  sleep(1);
  dump_nfreeholders("priority_inheritance:");

  /* Start the high priority threads */

//.........这里部分代码省略.........
开发者ID:l--putt,项目名称:nuttx-bb,代码行数:101,代码来源:prioinherit.c


示例17: get_stack

/*
 * Get the initial address and size of current thread's stack
 */
static int
get_stack(void **addr, size_t *size)
{
#define CHECK_ERR(expr)				\
    {int err = (expr); if (err) return err;}
#if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP || \
    (defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP)
    pthread_attr_t attr;
    size_t guard = 0;

# ifdef HAVE_PTHREAD_GETATTR_NP /* Linux */
    STACK_GROW_DIR_DETECTION;
    CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
#   ifdef HAVE_PTHREAD_ATTR_GETSTACK
    CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
    STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
#   else
    CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
    CHECK_ERR(pthread_attr_getstacksize(&attr, size));
#   endif
# elif defined HAVE_PTHREAD_ATTR_GET_NP /* FreeBSD, DragonFly BSD, NetBSD */
    CHECK_ERR(pthread_attr_init(&attr));
    CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
#   ifdef HAVE_PTHREAD_ATTR_GETSTACK
    CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
    STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
#   else
    CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
    CHECK_ERR(pthread_attr_getstacksize(&attr, size));
    STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
#   endif
# else /* MacOS X */
    pthread_t th = pthread_self();
    *addr = pthread_get_stackaddr_np(th);
    *size = pthread_get_stacksize_np(th);
    CHECK_ERR(pthread_attr_init(&attr));
# endif
    CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
    *size -= guard;
    pthread_attr_destroy(&attr);
#elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
    stack_t stk;
# if defined HAVE_THR_STKSEGMENT /* Solaris */
    CHECK_ERR(thr_stksegment(&stk));
# else /* OpenBSD */
    CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
# endif
    *addr = stk.ss_sp;
    *size = stk.ss_size;
#elif defined HAVE_PTHREAD_GETTHRDS_NP /* AIX */
    pthread_t th = pthread_self();
    struct __pthrdsinfo thinfo;
    char reg[256];
    int regsiz=sizeof(reg);
    CHECK_ERR(pthread_getthrds_np(&th, PTHRDSINFO_QUERY_ALL,
				   &thinfo, sizeof(thinfo),
				   &reg, &regsiz));
    *addr = thinfo.__pi_stackaddr;
    *size = thinfo.__pi_stacksize;
    STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
#else
#error STACKADDR_AVAILABLE is defined but not implemented.
#endif
    return 0;
#undef CHECK_ERR
}
开发者ID:ayumin,项目名称:ruby,代码行数:69,代码来源:thread_pthread.c


示例18: main


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C++ pthread_attr_setdetachstate函数代码示例发布时间:2022-05-30
下一篇:
C++ pthread_attr_getstacksize函数代码示例发布时间: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