当前位置: 首页>>代码示例>>C++>>正文


C++ pthread_setspecific函数代码示例

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


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

示例1: pthread_once

void *Consume (void *arg)
/* Consumer thread function. */
{
	msg_block_t *pmb;
	statistics_t * ps;
	int my_number, tstatus;
	struct timespec timeout, delta;
	
	delta.tv_sec = 2;
	delta.tv_nsec = 0;
		
	/* Create thread-specific storage key */
	tstatus = pthread_once (&once_control, once_init_function);
	if (tstatus != 0) err_abort (tstatus, "One time init failed");

	pmb = (msg_block_t *)arg;

	/* Allocate storage for thread-specific statistics */
	ps = calloc (sizeof(statistics_t), 1);
	if (ps == NULL) errno_abort ("Cannot allocate memory");
	tstatus = pthread_setspecific (ts_key, ps);
	if (tstatus != 0) err_abort (tstatus, "Error setting ts storage");
	ps->pmblock = pmb;
	/* Give this thread a unique number */
	/* Note that the mutex is "overloaded" to protect data	*/
	/* outside the message block structure			*/
	tstatus = pthread_mutex_lock (&pmb->nGuard);
	if (tstatus != 0) err_abort (tstatus, "Lock error");
	ps->th_number = thread_number++;
	tstatus = pthread_mutex_unlock (&pmb->nGuard);
	if (tstatus != 0) err_abort (tstatus, "Unlock error");
	
	/* Consume the NEXT message when prompted by the user */
	while (!pmb->fStop) { /* This is the only thread accessing stdin, stdout */
		tstatus = pthread_mutex_lock (&pmb->nGuard);
		if (tstatus != 0) err_abort (tstatus, "Lock error");
		/* Get the next message. Use a timed wait so as to be able	*/
		/* to sample the stop flag peridically.				*/
		do { 
			pthread_get_expiration_np (&delta, &timeout);
			tstatus = pthread_cond_timedwait 
				(&pmb->mReady, &pmb->nGuard, &timeout);
			if (tstatus != 0 && tstatus != ETIMEDOUT) 
				err_abort (tstatus, "CV wait error");
		} while (!pmb->f_ready && !pmb->fStop);
		if (!pmb->fStop) {
/*			printf ("Message received\n"); */
			accumulate_statistics ();
			pmb->f_consumed = 1;
			pmb->f_ready = 0;
		}
		tstatus = pthread_cond_signal (&pmb->mconsumed);
		if (tstatus != 0) err_abort (tstatus, "Signal error");
		tstatus = pthread_mutex_unlock (&pmb->nGuard);
		if (tstatus != 0) err_abort (tstatus, "Unlock error");
	}
	
	/* Shutdown. Report the statistics */
	tstatus = pthread_mutex_lock (&pmb->nGuard);
	if (tstatus != 0) err_abort (tstatus, "Lock error");
	report_statistics ();	
	tstatus = pthread_mutex_unlock (&pmb->nGuard);
	if (tstatus != 0) err_abort (tstatus, "Unlock error");

	/* Terminate the consumer thread. The destructor will 	*/
	/* free the memory allocated for the statistics		*/
	return NULL;		
}
开发者ID:oneminot,项目名称:wsp,代码行数:68,代码来源:multiPCav.c

示例2: setGlThreadSpecific

void setGlThreadSpecific(gl_hooks_t const *value) {
    pthread_setspecific(gGLWrapperKey, value);
}
开发者ID:e0234,项目名称:android_frameworks_base,代码行数:3,代码来源:egl.cpp

示例3: s_signalListener

static void * s_signalListener( void * my_stack )
{
   static HB_BOOL s_bFirst = HB_TRUE;

   sigset_t   passall;
   HB_STACK * pStack = ( HB_STACK * ) my_stack;

#if defined( HB_OS_BSD )
   int sig;
#else
   siginfo_t sinfo;
#endif

#ifdef HB_THREAD_TLS_KEYWORD
   hb_thread_stack = my_stack;
#else
   pthread_setspecific( hb_pkCurrentStack, my_stack );
#endif

   pStack->th_id = HB_CURRENT_THREAD();
   hb_threadLinkStack( pStack );
   HB_STACK_LOCK;

   /* and now accepts all signals */
   sigemptyset( &passall );

   /* and wait for all signals */
   sigaddset( &passall, SIGHUP );
   sigaddset( &passall, SIGQUIT );
   sigaddset( &passall, SIGILL );
   sigaddset( &passall, SIGABRT );
   sigaddset( &passall, SIGFPE );
   sigaddset( &passall, SIGSEGV );
   sigaddset( &passall, SIGTERM );
   sigaddset( &passall, SIGUSR1 );
   sigaddset( &passall, SIGUSR2 );
   sigaddset( &passall, SIGHUP );

   pthread_cleanup_push( hb_threadTerminator, my_stack );
   pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
   pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL );

   for( ;; )
   {
      /* allow safe cancelation */
      HB_STACK_UNLOCK;

      /* reset signal handling; this is done here (so I don't
         mangle with pthread_ calls, and I don't hold mutexes),
         and just once (doing it twice would be useless). */
      if( s_bFirst )
      {
         pthread_sigmask( SIG_SETMASK, &passall, NULL );
         s_bFirst = HB_FALSE;
      }

      /* This is also a cancelation point. When the main thread
         is done, it will kill all the threads having a stack
         including this one.
         ATM we don't care very much about signal handling during
         termination: no handler is set for them, so the DFL
         action is taken (and that should be fine). */
#if defined( HB_OS_BSD )
      sigwait( &passall, &sig );
#else
      sigwaitinfo( &passall, &sinfo );
#endif

      /* lock stack before passing the ball to VM. */
      HB_STACK_LOCK;
#if defined( HB_OS_BSD )
      s_signalHandler( sig, NULL, NULL );
#else
      s_signalHandler( sinfo.si_signo, &sinfo, NULL );
#endif
   }

   pthread_cleanup_pop( 1 );
   return 0;
}
开发者ID:orangesocks,项目名称:harbour-core,代码行数:80,代码来源:hbserv.c

示例4: ASSERT

void ThreadIdentifierData::initialize(ThreadIdentifier id)
{
    ASSERT(!identifier());
    pthread_setspecific(m_key, new ThreadIdentifierData(id));
}
开发者ID:604339917,项目名称:JavaScriptCore-iOS-1,代码行数:5,代码来源:ThreadIdentifierDataPthreads.cpp

示例5: __glXSetCurrentContext

_X_HIDDEN void
__glXSetCurrentContext(__GLXcontext * c)
{
   pthread_once(&once_control, init_thread_data);
   pthread_setspecific(ContextTSD, c);
}
开发者ID:MttDs,项目名称:new-rexeno-tindpe,代码行数:6,代码来源:glxcurrent.c

示例6: POSIXthreadSetSelf

static CVMBool
POSIXthreadSetSelf(const CVMThreadID *self)
{
    return (pthread_setspecific(key, self) == 0);
}
开发者ID:AllBinary,项目名称:phoneme-components-cdc,代码行数:5,代码来源:posix_threads_md.c

示例7: async_die_is_recursing

static int async_die_is_recursing(void)
{
	void *ret = pthread_getspecific(async_die_counter);
	pthread_setspecific(async_die_counter, (void *)1);
	return ret != NULL;
}
开发者ID:136357477,项目名称:git,代码行数:6,代码来源:run-command.c

示例8: DThreadData_New

static DThreadData* DThreadData_New()
{
	DThreadData *self = (DThreadData*) dao_calloc( 1, sizeof(DThreadData) );
	pthread_setspecific( thdSpecKey, self );
	return self;
}
开发者ID:daokoder,项目名称:dao,代码行数:6,代码来源:daoThread.c

示例9: entry

void * entry (void *arg)
{
  pthread_setspecific(key,
                      (void *)GC_HIDE_POINTER(GC_STRDUP("hello, world")));
  return arg;
}
开发者ID:OpenModelica,项目名称:OMCompiler-3rdParty,代码行数:6,代码来源:threadkey_test.c

示例10: pthread_setspecific

void ThreadKey::setKey(void *ptr)
{
    if(key != KEY_INVALID)
        pthread_setspecific(key, ptr);
}
开发者ID:dong777,项目名称:zkonvif,代码行数:5,代码来源:threadkey.cpp

示例11: DThread_Destroy

void DThread_Destroy( DThread *self )
{
	DMutex_Destroy( & self->mutex );
	DCondVar_Destroy( & self->condv );
	pthread_setspecific( thdSpecKey, NULL );
}
开发者ID:daokoder,项目名称:dao,代码行数:6,代码来源:daoThread.c

示例12: tls_set

void tls_set(TLS_DATA *tls_data) {
    pthread_setspecific(pthread_key, tls_data);
}
开发者ID:mtrojnar,项目名称:stunnel,代码行数:3,代码来源:tls.c

示例13: vlc_threadvar_set

/**
 * Sets a thread-specific variable.
 * @param key thread-local variable key (created with vlc_threadvar_create())
 * @param value new value for the variable for the calling thread
 * @return 0 on success, a system error code otherwise.
 */
int vlc_threadvar_set (vlc_threadvar_t key, void *value)
{
    return pthread_setspecific (key, value);
}
开发者ID:cmassiot,项目名称:vlc-broadcast,代码行数:10,代码来源:pthread.c

示例14: strerr_storage_init

static void strerr_storage_init(void) {
	zassert(pthread_key_create(&strerrstorage,strerr_storage_free));
	zassert(pthread_setspecific(strerrstorage,NULL));
}
开发者ID:davies,项目名称:moosefs,代码行数:4,代码来源:strerr.c

示例15: birdloop_set_current

static inline void
birdloop_set_current(struct birdloop *loop)
{
  pthread_setspecific(current_loop_key, loop);
}
开发者ID:yubo,项目名称:bird,代码行数:5,代码来源:io.c


注:本文中的pthread_setspecific函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。