本文整理汇总了C++中pthread_spin_init函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_spin_init函数的具体用法?C++ pthread_spin_init怎么用?C++ pthread_spin_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_spin_init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
int i;
int tid[NUM_THREADS];
pthread_t thread[NUM_THREADS];
/** < Ensure that locks are cacheline aligned */
assert(sizeof(lock_t) == 64);
/** < Allocate the shared nodes */
red_printf("Allocting %d nodes\n", NUM_NODES);
nodes = (node_t *) malloc(NUM_NODES * sizeof(node_t));
assert(nodes != NULL);
for(i = 0; i < NUM_NODES; i ++) {
nodes[i].a = rand();
nodes[i].b = nodes[i].a + 1;
}
/** < Allocate the striped spinlocks */
red_printf("Allocting %d locks\n", NUM_LOCKS);
locks = (lock_t *) malloc(NUM_LOCKS * sizeof(lock_t));
assert(locks != NULL);
for(i = 0; i < NUM_LOCKS; i++) {
pthread_spin_init(&locks[i].lock, 0);
}
/** < Launch several reader threads and a writer thread */
for(i = 0; i < NUM_THREADS; i++) {
tid[i] = i;
red_printf("Launching reader thread with tid = %d\n", tid[i]);
pthread_create(&thread[i], NULL, reader, &tid[i]);
}
for(i = 0; i < NUM_THREADS; i++) {
pthread_join(thread[i], NULL);
}
exit(0);
}
示例2: server_init
static struct server *
server_init(void)
{
struct server *s = malloc(sizeof(struct server));
if (s == NULL)
dns_error(0, "out of memory in server_init");
s->nfetcher = FETCHER_NUM;
s->nquizzer = QUIZZER_NUM;
s->authors = NULL;
s->fetchers = NULL;
s->pkg = 0;
pthread_spin_init(&s->eventlist.lock, 0);
//pthread_mutex_init(&s->lock,NULL);
s->eventlist.head = NULL;
if ((s->ludp = create_listen_ports(SERVER_PORT, UDP, (uchar *)SRV_ADDR)) < 0)
dns_error(0, "can not open udp");
set_sock_buff(s->ludp, 10);
if ((s->ltcp = create_listen_ports(SERVER_PORT, TCP, (uchar *)SRV_ADDR)) < 0)
dns_error(0, "can not open tcp");
s->datasets =
htable_create(NULL, dict_comp_str_equ, HASH_TABLE_SIZE,
MULTI_HASH);
if (s->datasets == NULL)
dns_error(0, "htable create");
s->forward = htable_create(NULL, dict_comp_str_equ, 1024, 1);
if (s->forward == NULL)
dns_error(0, "create forward");
s->qlist =
htable_create(NULL, dict_comp_str_equ,
QLIST_TABLE_SIZE, 1);
if (s->qlist == NULL)
dns_error(0, "create qlist");
s->ttlexp = create_rbtree(rbt_comp_ttl_gt, NULL);
if (s->ttlexp == NULL)
dns_error(0, "create ttl tree");
s->recordsindb = 0;
s->refreshflag = 0;
s->lastrefresh = global_now;
s->is_forward = 0;
return s;
}
示例3: ptw32_spinlock_check_need_init
INLINE int
ptw32_spinlock_check_need_init (pthread_spinlock_t * lock)
{
int result = 0;
/*
* The following guarded test is specifically for statically
* initialised spinlocks (via PTHREAD_SPINLOCK_INITIALIZER).
*
* Note that by not providing this synchronisation we risk
* introducing race conditions into applications which are
* correctly written.
*/
EnterCriticalSection (&ptw32_spinlock_test_init_lock);
/*
* We got here possibly under race
* conditions. Check again inside the critical section
* and only initialise if the spinlock is valid (not been destroyed).
* If a static spinlock has been destroyed, the application can
* re-initialise it only by calling pthread_spin_init()
* explicitly.
*/
if (*lock == PTHREAD_SPINLOCK_INITIALIZER)
{
result = pthread_spin_init (lock, PTHREAD_PROCESS_PRIVATE);
}
else if (*lock == NULL)
{
/*
* The spinlock has been destroyed while we were waiting to
* initialise it, so the operation that caused the
* auto-initialisation should fail.
*/
result = EINVAL;
}
LeaveCriticalSection (&ptw32_spinlock_test_init_lock);
return (result);
}
示例4: main
int main()
{
int rc = 0;
printf("main: initialize spin lock\n");
if(pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0)
{
printf("main: Error at pthread_spin_init()\n");
return PTS_UNRESOLVED;
}
printf("main: attempt spin lock\n");
/* We should get the lock */
if(pthread_spin_lock(&spinlock) != 0)
{
printf("Unresolved: main cannot get spin lock when no one owns the lock\n");
return PTS_UNRESOLVED;
}
printf("main: acquired spin lock\n");
printf("main: unlock spin lock\n");
if(pthread_spin_unlock(&spinlock) != 0)
{
printf("main: Error at pthread_spin_unlock()\n");
return PTS_UNRESOLVED;
}
printf("main: destroy spin lock\n");
rc = pthread_spin_destroy(&spinlock);
if(rc != 0)
{
printf("Test FAILED: Error at pthread_spin_destroy()"
"Return code : %d\n", rc);
return PTS_FAIL;
}
printf("Test PASSED\n");
return PTS_PASS;
}
示例5: main
int main()
{
int rc = 0;
pthread_t child_thread;
#ifdef PTHREAD_PROCESS_PRIVATE
pshared = PTHREAD_PROCESS_PRIVATE;
#else
pshared = -1;
#endif
printf("main: initialize spin lock\n");
rc = pthread_spin_init(&spinlock, pshared);
if (rc != 0) {
printf("Test FAILED: Error at pthread_spin_init()\n");
return PTS_FAIL;
}
printf("main: attempt spin lock\n");
/* We should get the lock */
if (pthread_spin_lock(&spinlock) != 0) {
printf
("Error: main cannot get spin lock when no one owns the lock\n");
return PTS_UNRESOLVED;
}
printf("main: acquired spin lock\n");
printf("main: create thread\n");
if (pthread_create(&child_thread, NULL, fn_chld, NULL) != 0) {
printf("main: Error creating child thread\n");
return PTS_UNRESOLVED;
}
/* Wait for thread to end execution */
pthread_join(child_thread, NULL);
return PTS_PASS;
}
示例6: test_spin2
int
test_spin2(void)
#endif
{
pthread_t t;
assert(pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE) == 0);
assert(pthread_spin_lock(&lock) == 0);
assert(pthread_create(&t, NULL, func, NULL) == 0);
assert(pthread_join(t, NULL) == 0);
assert(pthread_spin_unlock(&lock) == 0);
assert(pthread_spin_destroy(&lock) == 0);
assert(washere == 1);
return 0;
}
示例7: usdf_timer_init
/*
* Initialize timer data
*/
int
usdf_timer_init(struct usdf_fabric *fp)
{
int i;
pthread_spin_init(&fp->fab_timer_lock, PTHREAD_PROCESS_PRIVATE);
fp->fab_timer_buckets = calloc(USDF_NUM_TIMER_BUCKETS,
sizeof(struct usdf_timer_bucket));
if (fp->fab_timer_buckets == NULL) {
return -FI_ENOMEM;
}
for (i = 0; i < USDF_NUM_TIMER_BUCKETS; ++i) {
LIST_INIT(&fp->fab_timer_buckets[i]);
}
fp->fab_cur_bucket = 0;
fp->fab_cur_bucket_ms = usdf_get_ms();
return 0;
}
示例8: test_spin3
int
test_spin3(void)
#endif
{
pthread_t t;
wasHere = 0;
assert(pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE) == 0);
assert(pthread_spin_lock(&spin) == 0);
assert(pthread_create(&t, NULL, unlocker, (void *) 0) == 0);
assert(pthread_join(t, NULL) == 0);
/*
* Our spinlocks don't record the owner thread so any thread can unlock the spinlock,
* but nor is it an error for any thread to unlock a spinlock that is not locked.
*/
assert(pthread_spin_unlock(&spin) == 0);
assert(pthread_spin_destroy(&spin) == 0);
assert(wasHere == 2);
return 0;
}
示例9: ssa_set_ssa_signal_handler
int ssa_set_ssa_signal_handler()
{
struct sigaction action;
int ret;
#if 0
/*
* addr2line utility doesn't work with alternative stack
*/
stack_t our_stack;
our_stack.ss_sp = (void *) malloc(SIGSTKSZ);
our_stack.ss_size = SIGSTKSZ;
our_stack.ss_flags = 0;
if (sigaltstack(&our_stack, NULL) != 0)
return 1;
#endif
ret = pthread_spin_init(&signal_handler_lock, 0);
if (ret)
return ret;
ret = get_exe_path();
if (ret)
return ret;
action.sa_sigaction = ssa_signal_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
if (sigaction(SIGSEGV, &action, NULL) != 0)
return 1;
if (sigaction(SIGFPE, &action, NULL) != 0)
return 1;
if (sigaction(SIGILL, &action, NULL) != 0)
return 1;
return 0;
}
示例10: bianca_init
void bianca_init() {
int i;
_BIANCAperror = (void (*)(const char*)) dlsym (RTLD_NEXT, "perror");
_BIANCAclose = (int (*)(int)) dlsym (RTLD_NEXT, "close");
_BIANCAexit = (void (*)(int)) dlsym (RTLD_NEXT, "exit");
_BIANCAwrite = (int (*)(int, const void*, size_t)) dlsym (RTLD_NEXT, "write");
_BIANCA__builtin_puts = (int (*)(const char*)) dlsym (RTLD_NEXT, "__builtin_puts");
_BIANCApthread_create = (int (*)(pthread_t*, const pthread_attr_t*, void *(*start_routine)(void*), void*)) dlsym (RTLD_NEXT, "pthread_create");
_BIANCAprintf = (int (*)(const char*,...)) dlsym (RTLD_NEXT, "printf");
_BIANCApthread_join = (int (*)(pthread_t, void **value_ptr)) dlsym (RTLD_NEXT, "pthread_join");
pthread_spin_init(&lock, NULL);
/* Initialisation de la table des identités */
for (i = 0; i < 100; i++) { hashid.threads[i] = (pthread_t) -1; }
hashid.size = 0;
/* Initialisation du réseau */
global_table = init(id(pthread_self()), P0_1_ENTRY);
hashid.processes[0] = find_process_id(&global_table,0);
}
示例11: main
int main()
{
pthread_t tcb1, tcb2;
int rv;
pthread_spin_init(&spin,
PTHREAD_PROCESS_PRIVATE /* PTHREAD_PROCESS_SHARED */
);
rv = pthread_create(&tcb1, NULL, thread1, NULL);
if (rv)
puts("Failed to create thread");
rv = pthread_create(&tcb2, NULL, thread2, NULL);
if (rv)
puts("Failed to create thread");
pthread_join(tcb1, NULL);
pthread_join(tcb2, NULL);
puts(" Exit Main");
return 0;
}
示例12: fn_chld
static void* fn_chld(void *arg)
{
int rc = 0;
/* Initialize spin lock */
if(pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0)
{
printf("main: Error at pthread_spin_init()\n");
exit(PTS_UNRESOLVED);
}
/* Lock the spinlock */
printf("thread: attempt spin lock\n");
rc = pthread_spin_lock(&spinlock);
if(rc != 0)
{
printf("Error: thread failed to get spin lock error code:%d\n" , rc);
exit(PTS_UNRESOLVED);
}
printf("thread: acquired spin lock\n");
/* Wait for main to try and unlock this spinlock */
sem = INMAIN;
while(sem == INMAIN)
sleep(1);
/* Cleanup just in case */
pthread_spin_unlock(&spinlock);
if(pthread_spin_destroy(&spinlock) != 0)
{
printf("Error at pthread_spin_destroy()");
exit(PTS_UNRESOLVED);
}
pthread_exit(0);
return NULL;
}
示例13: main
int main(int argc, char *argv[]) {
int seed;
// inizializza il seme
seed=time(NULL);
srand(seed);
printf("Pid:%d\n",getpid());
// ancora non vogliamo uscire
wanna_exit=0;
// imposta il signal handler
signal(SIGUSR1,sig_user_exit);
// inizializza lo spinlock
pthread_spin_init(&spinlock,PTHREAD_PROCESS_PRIVATE);
// conservati il tuo thread id (anche se qui non serve)
pthread[0]=pthread_self();
// crea il secondo thread
if (pthread_create(&pthread[1],NULL,(void *(*)(void *))do_something,(void *)((long int)1)))
printf("Error creating thread!\n");
// vai alla funzione relativa alla gestione dello spinlock
do_something(0);
// se hai finito aspetta il secondo thread
// simile al wait del fork, per evitare di produrre "thread zombie"
// e spreco di risorse, a meno che non venga utilizzato
// pthread_detach e il thread abbia gli attributi corretti
pthread_join(pthread[1],NULL);
// libera le risorse relative allo spinlock
pthread_spin_destroy(&spinlock);
printf("All done\n");
// esci
exit(EXIT_SUCCESS);
}
示例14: do_test
static int
do_test (void)
{
pthread_spinlock_t s;
if (pthread_spin_init (&s, PTHREAD_PROCESS_PRIVATE) != 0)
{
puts ("spin_init failed");
return 1;
}
if (pthread_spin_lock (&s) != 0)
{
puts ("1st spin_lock failed");
return 1;
}
/* Set an alarm for 1 second. The wrapper will expect this. */
alarm (1);
#ifdef ORIGINAL_TEST /* ORIGINAL */
/* This call should never return. */
pthread_spin_lock (&s);
puts ("2nd spin_lock returned");
#else /* !ORIGINAL */
int r = pthread_spin_lock (&s);
if (!r) {
puts ("2nd spin_lock succeeded");
} else if (r != EDEADLOCK) {
puts ("2nd spin_lock failed but did not EDEADLOCKed");
}
// needed to avoid freezing linux
pthread_soft_real_time_np();
while (pthread_spin_trylock (&s) == EBUSY) rt_sleep(nano2count(10000));
#endif /* ORIGINAL */
return 1;
}
示例15: ptw32_spinlock_check_need_init
INLINE int
ptw32_spinlock_check_need_init (pthread_spinlock_t * lock)
{
int result = 0;
ptw32_mcs_local_node_t node;
/*
* The following guarded test is specifically for statically
* initialised spinlocks (via PTHREAD_SPINLOCK_INITIALIZER).
*/
ptw32_mcs_lock_acquire(&ptw32_spinlock_test_init_lock, &node);
/*
* We got here possibly under race
* conditions. Check again inside the critical section
* and only initialise if the spinlock is valid (not been destroyed).
* If a static spinlock has been destroyed, the application can
* re-initialise it only by calling pthread_spin_init()
* explicitly.
*/
if (*lock == PTHREAD_SPINLOCK_INITIALIZER)
{
result = pthread_spin_init (lock, PTHREAD_PROCESS_PRIVATE);
}
else if (*lock == NULL)
{
/*
* The spinlock has been destroyed while we were waiting to
* initialise it, so the operation that caused the
* auto-initialisation should fail.
*/
result = EINVAL;
}
ptw32_mcs_lock_release(&node);
return (result);
}