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

C++ pthread_once函数代码示例

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

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



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

示例1: server_agent

/* the agent is now a separate thread */
void *
server_agent(void *params) {
	int client_fd, n, errcode;
	enum header_types type;
	unsigned int len;
	char to_search[MAX_SEARCH_STR];
	char remote_obj[REMOTE_NAME_MAX];
	time_type t_start, t_end;
	double tdiff;
	msg_one msg1;
	search *mysearch;
	Statistics statistic;

	/* we are now successfully connected to a remote client */
	client_fd = ((struct thread_params *) params)->client_fd;
	fprintf(stderr, "Starting Agent fd: %d, thread id: %lu \n", client_fd,
			(unsigned long) pthread_self());

	/* do some initialization*/
	memset(&statistic, 0, sizeof(Statistics));
	errcode = pthread_detach(pthread_self());
	if (errcode != 0) {
		fprintf(stderr, "pthread_detach server agent: %s\n", strerror(errcode));
	}
	pthread_once(&init_done, thread_init);

	get_time(&t_start);
	len = sizeof(msg_one);
	/* first message should be the file name */
	if ((n = our_recv_message(client_fd, &type, &len, &msg1)) < 0)
		return NULL;
	if (type != OPTION_PARAMETER)
		return NULL;

	/*firstly build the search object */
	mysearch = build_search(&msg1);
	mysearch->client_fd = client_fd;
	/* then print the search options */
	print_search_para(client_fd, mysearch);

	/* receive the second message*/
	len = MAX_SEARCH_STR;
	if ((n = our_recv_message(client_fd, &type, &len, to_search)) < 0)
		return NULL;
	if (type != TO_SEARCH)
		return NULL;
	len = strlen(to_search);
	if ((mysearch->search_pattern = (char*) malloc(len + 1)) == NULL) {
		perror("malloc");
		free(params);
		return NULL;
	}
	strncpy(mysearch->search_pattern, to_search, len+1);

	/*build its own shift table if needed */
	build_shifttable(mysearch);

	/* receive the second message*/
	len = REMOTE_NAME_MAX;
	if ((n = our_recv_message(client_fd, &type, &len, remote_obj)) < 0)
		return NULL;
	if (type != REMOTE_NAME)
		return NULL;
	/* do the search job */
	search_given(remote_obj, mysearch);

	/* send the statistics message 6*/
	/*wait for directory search to be finished if it is on the server side*/
	while (mysearch->stk_count != 0) {
		pthread_cond_wait(&(mysearch->ready), &(mysearch->lock));
	}

	len = sizeof(Statistics);
	/* make a copy of little/big endian adapted statistical structure*/
	update_statistics_sock(&statistic, &mysearch->statistics);
	trans_stat2send(&statistic);
	if (our_send_message(mysearch->client_fd, STATISTICS_MSG, len, &statistic)
			!= 0) {
		fprintf(stderr, "Fail to send statistics\n");
		return NULL;
	}
	get_time(&t_end);
	tdiff = time_diff(&t_start, &t_end);
	fprintf(stderr, "Search Statistics: client fd %u, thread id %lu\n",
			mysearch->client_fd, (unsigned long) pthread_self());
	print_stat(stderr, &mysearch->statistics, tdiff);
	destroy_search(mysearch);
	fprintf(stderr, "Terminating Agent fd: %d, thread id: %lu \n", client_fd,
			(unsigned long) pthread_self());
	free(params);
	return NULL;
} /* server_agent */
开发者ID:ytz2,项目名称:cs820,代码行数:93,代码来源:server.c


示例2: __Unwind_SjLj_SetTopOfFunctionStack

	static void __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext* fc)
	{
		pthread_once(&sOnceFlag, __Unwind_SjLj_MakeTopOfFunctionStackKey);
		pthread_setspecific(sPerThreadTopOfFunctionStack, fc);
	}
开发者ID:rotten-apples,项目名称:libunwind,代码行数:5,代码来源:Unwind-sjlj.c


示例3: semcompat_new

sem_t * semcompat_new(
    int pshared,
    unsigned int value)
{
    sem_t * ret;
    int errno_save;

    if (pthread_once(&support_unnamed_initialized, initialize_support_unnamed) != 0)
    {
        // errno is set by pthread_once
        return SEM_FAILED;
    }

    if (support_unnamed)
    {
        ret = malloc(sizeof(sem_t));
        if (ret == NULL)
        {
            // errno is set by malloc
            return SEM_FAILED;
        }

        if (sem_init(ret, pshared, value) != 0)
        {
            errno_save = errno;
            free(ret);
            errno = errno_save;
            return SEM_FAILED;
        }

        return ret;
    }
    else
    {
        size_t i;
        char name[SEM_NAME_SIZE];

        for (i = 0; i < SEM_OPEN_MAX_TRIES; ++i)
        {
            make_sem_name(name);

            ret = sem_open(name, O_CREAT | O_EXCL, 0600, value);
            if (ret == SEM_FAILED)
            {
                if (errno == EEXIST)
                {
                    // try another name
                    continue;
                }
                else
                {
                    // errno is set by sem_open
                    return SEM_FAILED;
                }
            }
            else
            {
                // Now that it's open, we don't want any other processes to
                // access it by name.
                if (sem_unlink(name) != 0)
                {
                    LOG(LOG_WARNING,
                        "failed to unlink semaphore %s, continuing anyway",
                        name);
                }

                return ret;
            }
        }

        LOG(LOG_ERR, "failed to create a semaphore after %d tries",
            SEM_OPEN_MAX_TRIES);
        errno = EAGAIN;
        return SEM_FAILED;
    }
}
开发者ID:dseomn,项目名称:rpstir,代码行数:76,代码来源:semaphore_compat.c


示例4: BindReplyMachPortToThread

static OSStatus BindReplyMachPortToThread(mach_port_t *replyPortPtr)
    // Get a reply port for this thread, remembering that we've done this 
    // in per-thread storage.
    // 
    // On success, *replyPortPtr is the port to use for this thread's reply 
    // port.  It will be MACH_PORT_NULL if you call it from the main thread.
{
    OSStatus    err;
    
    assert( replyPortPtr != NULL);
    assert(*replyPortPtr == MACH_PORT_NULL);

    // Initialise ourselves the first time that we're called.
    
    err = (OSStatus) pthread_once(&sInited, InitRoutine);
    
    // If something went wrong, return the latched error.
    
    if ( (err == noErr) && (sPerThreadStorageKeyInitErrNum != noErr) ) {
        err = sPerThreadStorageKeyInitErrNum;
    }
    
    // Now do the real work.
    
    if (err == noErr) {
        if ( pthread_main_np() ) {
            // This is the main thread, so do nothing; leave *replyPortPtr set 
            // to MACH_PORT_NULL.
            assert(*replyPortPtr == MACH_PORT_NULL);
        } else {
            PerThreadStorage *  storage;

            // Get the per-thread storage for this thread.
            
            storage = (PerThreadStorage *) pthread_getspecific(sPerThreadStorageKey);
            if (storage == NULL) {

                // The per-thread storage hasn't been allocated yet for this specific 
                // thread.  Let's go allocate it and attach it to this thread.
                
                err = AllocatePortFromPool(&storage);
                if (err == noErr) {
                    err = (OSStatus) pthread_setspecific(sPerThreadStorageKey, (void *) storage);
                    if (err != noErr) {
                        ReturnPortToPool(storage);
                        storage = NULL;
                    }
                }
            }
            assert( (err == noErr) == (storage != NULL) );
            
            // If all went well, copy the port out to our client.
            
            if (err == noErr) {
                assert(storage->magic == kPerThreadStorageMagic);
                assert(storage->port  != MACH_PORT_NULL);
                *replyPortPtr = storage->port;
            }
        }
    }

    // no error + MACH_PORT_NULL is a valid response if we're on the main 
    // thread.
    //
    // assert( (err == noErr) == (*replyPortPtr != MACH_PORT_NULL) );
    assert( (*replyPortPtr == MACH_PORT_NULL) || (err == noErr) );
    
    return err;
}
开发者ID:Acidburn0zzz,项目名称:NicePlayer,代码行数:69,代码来源:SendThreadSafe.c


示例5: xeno_sigshadow_install_once

void xeno_sigshadow_install_once(void)
{
	static pthread_once_t sigshadow_installed = PTHREAD_ONCE_INIT;
	pthread_once(&sigshadow_installed, xeno_sigshadow_install);
}
开发者ID:beaverinventeam,项目名称:xenomai-2.6,代码行数:5,代码来源:sigshadow.c


示例6: s_init

static inline void
s_init(void) {
  (void)pthread_once(&s_once, s_once_proc);
}
开发者ID:GPtpvWKrrZWERTEg,项目名称:mccp,代码行数:4,代码来源:strutils.c


示例7: cobalt_init_umm

void cobalt_init_umm(__u32 vdso_offset)
{
	pthread_once(&init_bind_once, init_bind);
	init_loadup(vdso_offset);
}
开发者ID:rcn-ee,项目名称:xenomai-3,代码行数:5,代码来源:umm.c


示例8: sge_err_init

/* initialization function that has to be called before threads are spawned */
void 
sge_err_init(void) {
   DENTER(ERR_LAYER, "sge_err_init");
   pthread_once(&sge_err_once, sge_err_once_init);
   DEXIT;
}
开发者ID:valhallasw,项目名称:son-of-gridengine,代码行数:7,代码来源:sge_err.c


示例9: time_init

/* Initializes the timetracking module, if not already initialized. */
static void
time_init(void)
{
    static pthread_once_t once = PTHREAD_ONCE_INIT;
    pthread_once(&once, do_init_time);
}
开发者ID:PatrickGuo,项目名称:ovs-2.1.3,代码行数:7,代码来源:timeval.c


示例10: epicsThreadInit

static void epicsThreadInit(void)
{
    static pthread_once_t once_control = PTHREAD_ONCE_INIT;
    int status = pthread_once(&once_control,once);
    checkStatusQuit(status,"pthread_once","epicsThreadInit");
}
开发者ID:ukaea,项目名称:epics,代码行数:6,代码来源:osdThread.c


示例11: initialize

static void initialize(void)
{
    pthread_once(&gWrapSimInitialized, initOnce);
}
开发者ID:Andproject,项目名称:platform_development,代码行数:4,代码来源:Init.c


示例12: lae_stderr

lae_stream * lae_stderr()
{
    static pthread_once_t once = PTHREAD_ONCE_INIT;
    pthread_once( &once, lae_filestream_stderr_init );
    return filestream_stderr;
}
开发者ID:lifeaether,项目名称:lae,代码行数:6,代码来源:lae_filestream.c


示例13: initializeWebThread

void initializeWebThread()
{
    pthread_once(&initializeWebThreadKeyOnce, initializeWebThreadOnce);
}
开发者ID:edcwconan,项目名称:webkit,代码行数:4,代码来源:MainThread.cpp


示例14: virOnce

int virOnce(virOnceControlPtr once, virOnceFunc init)
{
    return pthread_once(&once->once, init);
}
开发者ID:ryu25ish,项目名称:libvirt,代码行数:4,代码来源:virthreadpthread.c


示例15: initializeMainThreadToProcessMainThread

void initializeMainThreadToProcessMainThread()
{
    pthread_once(&initializeMainThreadKeyOnce, initializeMainThreadToProcessMainThreadOnce);
}
开发者ID:edcwconan,项目名称:webkit,代码行数:4,代码来源:MainThread.cpp


示例16: AcquireLock

CfLock AcquireLock(char *operand, char *host, time_t now, Attributes attr, Promise *pp, int ignoreProcesses)
{
    unsigned int pid;
    int i, err, sum = 0;
    time_t lastcompleted = 0, elapsedtime;
    char *promise, cc_operator[CF_BUFSIZE], cc_operand[CF_BUFSIZE];
    char cflock[CF_BUFSIZE], cflast[CF_BUFSIZE], cflog[CF_BUFSIZE];
    char str_digest[CF_BUFSIZE];
    CfLock this;
    unsigned char digest[EVP_MAX_MD_SIZE + 1];

    /* Register a cleanup handler */
    pthread_once(&lock_cleanup_once, &RegisterLockCleanup);

    this.last = (char *) CF_UNDEFINED;
    this.lock = (char *) CF_UNDEFINED;
    this.log = (char *) CF_UNDEFINED;

    if (now == 0)
    {
        return this;
    }

    this.last = NULL;
    this.lock = NULL;
    this.log = NULL;

/* Indicate as done if we tried ... as we have passed all class
   constraints now but we should only do this for level 0
   promises. Sub routine bundles cannot be marked as done or it will
   disallow iteration over bundles */

    if (pp->done)
    {
        return this;
    }

    if (RlistLen(CF_STCK) == 1)
    {
        *(pp->donep) = true;
        /* Must not set pp->done = true for editfiles etc */
    }

    PromiseHash(pp, operand, digest, CF_DEFAULT_DIGEST);
    HashPrintSafe(CF_DEFAULT_DIGEST, digest, str_digest);

/* As a backup to "done" we need something immune to re-use */

    if (THIS_AGENT_TYPE == AGENT_TYPE_AGENT)
    {
        if (IsItemIn(DONELIST, str_digest))
        {
            CfOut(OUTPUT_LEVEL_VERBOSE, "", " -> This promise has already been verified");
            return this;
        }

        PrependItem(&DONELIST, str_digest, NULL);
    }

/* Finally if we're supposed to ignore locks ... do the remaining stuff */

    if (IGNORELOCK)
    {
        this.lock = xstrdup("dummy");
        return this;
    }

    promise = BodyName(pp);
    snprintf(cc_operator, CF_MAXVARSIZE - 1, "%s-%s", promise, host);
    strncpy(cc_operand, operand, CF_BUFSIZE - 1);
    CanonifyNameInPlace(cc_operand);
    RemoveDates(cc_operand);

    free(promise);

    CfDebug("AcquireLock(%s,%s), ExpireAfter=%d, IfElapsed=%d\n", cc_operator, cc_operand, attr.transaction.expireafter,
            attr.transaction.ifelapsed);

    for (i = 0; cc_operator[i] != '\0'; i++)
    {
        sum = (CF_MACROALPHABET * sum + cc_operator[i]) % CF_HASHTABLESIZE;
    }

    for (i = 0; cc_operand[i] != '\0'; i++)
    {
        sum = (CF_MACROALPHABET * sum + cc_operand[i]) % CF_HASHTABLESIZE;
    }

    snprintf(cflog, CF_BUFSIZE, "%s/cf3.%.40s.runlog", CFWORKDIR, host);
    snprintf(cflock, CF_BUFSIZE, "lock.%.100s.%s.%.100s_%d_%s", PromiseGetBundle(pp)->name, cc_operator, cc_operand, sum, str_digest);
    snprintf(cflast, CF_BUFSIZE, "last.%.100s.%s.%.100s_%d_%s", PromiseGetBundle(pp)->name, cc_operator, cc_operand, sum, str_digest);

    CfDebug("LOCK(%s)[%s]\n", PromiseGetBundle(pp)->name, cflock);

// Now see if we can get exclusivity to edit the locks

    CFINITSTARTTIME = time(NULL);

    WaitForCriticalSection();

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


示例17: chpl_topo_getNumCPUsLogical

int chpl_topo_getNumCPUsLogical(chpl_bool accessible_only) {
  CHK_ERR(pthread_once(&numCPUs_ctrl, getNumCPUs) == 0);
  return (accessible_only) ? numCPUsLogAcc : numCPUsLogAll;
}
开发者ID:DawidvC,项目名称:chapel,代码行数:4,代码来源:topo-hwloc.c


示例18: vnode_create

extern errno_t vnode_create(int flavor, size_t size, void *data, vnode_t *vnPtr)
{
    int         err;
    int         junk;
    vnode_t     vn;
    vnode_t     newVN;
    vnode_t     vnToRecycle;
    CFIndex     vnCount;
    CFIndex     vnIndex;
    static pthread_once_t sVNodesControl = PTHREAD_ONCE_INIT;
    
    assert(flavor == VNCREATE_FLAVOR);
    assert(size == VCREATESIZE);
    assert(data != NULL);
    assert(vnPtr != NULL);
    
    // Initialise gVNodes
    
    junk = pthread_once(&sVNodesControl, InitVNodes);
    assert(junk == 0);
    
    newVN = NULL;
    vn = NULL;
    vnToRecycle = NULL;
    do {
        err = EAGAIN;
        
        junk = pthread_mutex_lock(&gVNodesLock);
        assert(junk == 0);
        
        vnCount = CFArrayGetCount(gVNodes);
        if (vnCount  < gVNodesMax) {
            // We can just add a vnode.
            
            if (newVN == NULL) {
                junk = pthread_mutex_unlock(&gVNodesLock);
                assert(junk == 0);

                newVN = (vnode_t) malloc(sizeof(*vn));
                assert(newVN != NULL);
                
                junk = pthread_mutex_init(&newVN->mtx, NULL);
                assert(junk == 0);

                newVN->getPutRefCount = 1;
                newVN->vid = 0;
                newVN->fsnode = ((struct vnode_fsparam *) data)->vnfs_fsnode;

                junk = pthread_mutex_lock(&gVNodesLock);
                assert(junk == 0);
            } else {
                CFArrayAppendValue(gVNodes, newVN);
                vn = newVN;
                newVN = NULL;
                err = 0;
            }
        } else {
            // We must recycle a vnode.
            
            for (vnIndex = 0; vnIndex < vnCount; vnIndex++) {
                vnode_t     thisVN;

                thisVN = (vnode_t) CFArrayGetValueAtIndex(gVNodes, vnIndex);
                if (thisVN->getPutRefCount == 0) {
                    vnToRecycle = thisVN;
                    err = 0;

                    // Move the vnode we're recycling (well, the one that 
                    // we're /planning/ to recycle) to the end of the list, 
                    // so that it doesn't get immediately recycled again.
                    
                    CFArrayRemoveValueAtIndex(gVNodes, vnIndex);
                    CFArrayAppendValue(gVNodes, vnToRecycle);
                    break;
                }
            }
        }

        junk = pthread_mutex_unlock(&gVNodesLock);
        assert(junk == 0);

        if ( (err == 0) && (vnToRecycle != NULL) ) {
            assert(vn == NULL);
            
            junk = pthread_mutex_lock(&vnToRecycle->mtx);
            assert(junk == 0);
            
            if (vnToRecycle->getPutRefCount == 0) {
                // Stop anyone else messing with it

                vnToRecycle->getPutRefCount = 1;
                
                // Detach it from the file system.  This is super bogus because 
                // we're doing this with the vnode lock held.  If the client code 
                // called back into us to do anything interesting, they'd deadlock. 
                // However, that currently doesn't happen and, besides, dropping 
                // the lock is /hard/ (just look at the VFS implementation :-).

                gReclaimCallback(vnToRecycle);

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


示例19: CRYPTO_once

void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
  if (pthread_once(once, init) != 0) {
    abort();
  }
}
开发者ID:eggyo,项目名称:eggyo-node,代码行数:5,代码来源:thread_pthread.c


示例20: ChannelToCond

static pthread_cond_t * ChannelToCond(void *chan)
{
    int                 err;
    int                 junk;
    pthread_cond_t *    cond;
    pthread_cond_t *    newCond;
    static pthread_once_t sChannelToCondControl = PTHREAD_ONCE_INIT;

    newCond = NULL;
    
    // Lazy init of gChannelToCond.
    
    junk = pthread_once(&sChannelToCondControl, InitChannelToCond);
    assert(junk == 0);
    
    // Look up the channel to find or create the associated conditional variable.
    
    junk = pthread_mutex_lock(&gChannelToCondLock);
    assert(junk == 0);
    
    do {
        err = 0;

        cond = (pthread_cond_t *) CFDictionaryGetValue(gChannelToCond, chan);
        if (cond == NULL) {
            if (newCond == NULL) {
                junk = pthread_mutex_unlock(&gChannelToCondLock);
                assert(junk == 0);

                newCond = (pthread_cond_t *) malloc(sizeof(*newCond));
                assert(newCond != NULL);
                
                junk = pthread_cond_init(newCond, NULL);
                assert(junk == 0);
                
                err = EAGAIN;

                junk = pthread_mutex_lock(&gChannelToCondLock);
                assert(junk == 0);
            } else {
                CFDictionaryAddValue(gChannelToCond, chan, newCond);
                cond = newCond;
                newCond = NULL;
            }
        }
    } while (err == EAGAIN);
    
    junk = pthread_mutex_unlock(&gChannelToCondLock);
    assert(junk == 0);
    
    // If we created newCond but didn't use it, free it now.
    
    if (newCond != NULL) {
        junk = pthread_cond_destroy(newCond);
        assert(junk == 0);
        
        free(newCond);
    }
    
    return cond;
}
开发者ID:fruitsamples,项目名称:MFSLives,代码行数:61,代码来源:UserSpaceKernel.c



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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