本文整理汇总了C++中pthread_once函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_once函数的具体用法?C++ pthread_once怎么用?C++ pthread_once使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_once函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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 */
示例2: __Unwind_SjLj_SetTopOfFunctionStack
static void __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext* fc)
{
pthread_once(&sOnceFlag, __Unwind_SjLj_MakeTopOfFunctionStackKey);
pthread_setspecific(sPerThreadTopOfFunctionStack, fc);
}
示例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;
}
}
示例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;
}
示例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);
}
示例6: s_init
static inline void
s_init(void) {
(void)pthread_once(&s_once, s_once_proc);
}
示例7: cobalt_init_umm
void cobalt_init_umm(__u32 vdso_offset)
{
pthread_once(&init_bind_once, init_bind);
init_loadup(vdso_offset);
}
示例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;
}
示例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);
}
示例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");
}
示例11: initialize
static void initialize(void)
{
pthread_once(&gWrapSimInitialized, initOnce);
}
示例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;
}
示例13: initializeWebThread
void initializeWebThread()
{
pthread_once(&initializeWebThreadKeyOnce, initializeWebThreadOnce);
}
示例14: virOnce
int virOnce(virOnceControlPtr once, virOnceFunc init)
{
return pthread_once(&once->once, init);
}
示例15: initializeMainThreadToProcessMainThread
void initializeMainThreadToProcessMainThread()
{
pthread_once(&initializeMainThreadKeyOnce, initializeMainThreadToProcessMainThreadOnce);
}