本文整理汇总了C++中pthread_spin_init函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_spin_init函数的具体用法?C++ pthread_spin_init怎么用?C++ pthread_spin_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_spin_init函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
开发者ID:carriercomm,项目名称:fastpp,代码行数:41,代码来源:nogoto.c
示例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;
}
开发者ID:BearGrass,项目名称:dnspod-sr,代码行数:41,代码来源:init.c
示例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);
}
开发者ID:Blackbird88,项目名称:pcsx2,代码行数:41,代码来源:ptw32_spinlock_check_need_init.c
示例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;
}
开发者ID:8l,项目名称:rose,代码行数:41,代码来源:1-1.c
示例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;
}
开发者ID:Nan619,项目名称:ltp-ddt,代码行数:41,代码来源:4-1.c
示例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;
}
开发者ID:DMDZYP,项目名称:pthread-win32,代码行数:21,代码来源:spin2.c
示例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;
}
开发者ID:ParaStation,项目名称:psmpi2,代码行数:24,代码来源:usdf_timer.c
示例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;
}
开发者ID:Avic,项目名称:pthread-win32,代码行数:21,代码来源:spin3.c
示例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;
}
开发者ID:RoyMenczer,项目名称:ibssa2,代码行数:39,代码来源:ssa_signal_handler.c
示例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);
}
开发者ID:SavithaSwaroop,项目名称:Evinrude-CAMI-PNML,代码行数:22,代码来源:spin_capsule.c
示例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;
}
开发者ID:md-jamal,项目名称:Linuxpro,代码行数:22,代码来源:pthread_spinlock.c
示例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;
}
开发者ID:8l,项目名称:rose,代码行数:38,代码来源:3-1.c
示例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);
}
开发者ID:DesiD,项目名称:CsInfoPa,代码行数:38,代码来源:spinlock.c
示例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;
}
开发者ID:ArcEye,项目名称:RTAI,代码行数:38,代码来源:tst-spin3.c
示例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);
}
开发者ID:yxliang,项目名称:darknet,代码行数:38,代码来源:ptw32_spinlock_check_need_init.c
示例16: main
int main (int argc, char *argv[]) {
pthread_t thread[NTHREADS];
int err;
pthread_spin_init( &spinlock_global, 0);
for(int i=0;i<NTHREADS;i++) {
err=pthread_create(&(thread[i]),NULL,&func,NULL);
if(err!=0)
error(err,"pthread_create");
}
for(int i=0; i<1000000000;i++) { /*...*/ }
for(int i=NTHREADS-1;i>=0;i--) {
err=pthread_join(thread[i],NULL);
if(err!=0)
error(err,"pthread_join");
}
printf("global: %ld\n",global);
return(EXIT_SUCCESS);
}
开发者ID:HappyRave,项目名称:SystemesInformatiques,代码行数:23,代码来源:pthread-spin.c
示例17: nk_cond_create
nk_status nk_cond_create(nk_host *host, nk_cond **ret) {
nk_status status;
status = NK_ERR_NOMEM;
nk_cond *c = nk_freelist_alloc(&host->cond_freelist);
if (!c) {
goto err;
}
if (pthread_spin_init(&c->lock, PTHREAD_PROCESS_PRIVATE)) {
goto err;
}
c->host = host;
QUEUE_INIT(&c->waiters);
*ret = c;
return NK_OK;
err:
if (c) {
nk_freelist_free(&host->cond_freelist, c);
}
return status;
}
开发者ID:cfallin,项目名称:nk,代码行数:24,代码来源:sync.c
示例18: nk_mutex_create
nk_status nk_mutex_create(nk_host *host, nk_mutex **ret) {
nk_status status;
status = NK_ERR_NOMEM;
nk_mutex *m = nk_freelist_alloc(&host->mutex_freelist);
if (!m) {
goto err;
}
if (pthread_spin_init(&m->lock, PTHREAD_PROCESS_PRIVATE)) {
goto err;
}
m->host = host;
QUEUE_INIT(&m->waiters);
*ret = m;
return NK_OK;
err:
if (m) {
nk_freelist_free(&host->mutex_freelist, m);
}
return status;
}
开发者ID:cfallin,项目名称:nk,代码行数:24,代码来源:sync.c
示例19: codec_async_mem_init
void codec_async_mem_init(int frame_width, int frame_height, int channels)
{
_codec_mem_pool_size = 80;
pthread_spin_init(&_mem_spin, 0);
mem = (codec_buffer_t *)malloc(_codec_mem_pool_size * sizeof(codec_buffer_t));
for (int i = 0 ; i < _codec_mem_pool_size; i++) {
mem[i].index = i;
mem[i].data = (void *)malloc(frame_width * frame_height * channels * sizeof(uint8_t) + // Reference Camera Frame, UINT8, 4 channels
sizeof(short) + // Depth Data availability flag
frame_width * frame_height * sizeof(float) + // Depth Data, float, 1 channels
sizeof(int) + // Depth Data aligned representation step offset
sizeof(int) // Frame Counter
);
}
_mem_count = 0;
_mem_head = mem;
_mem_tail = mem;
sem_init(&_sem_empty, 0, _codec_mem_pool_size);
}
开发者ID:hvictor,项目名称:Release,代码行数:24,代码来源:codec_async_mem.cpp
示例20: Driver
Driver_FILE::Driver_FILE(const int& _framesize_in_byte, std::string _conn_string):
Driver(_framesize_in_byte, _conn_string),
empty_frame(Frame(_framesize_in_byte))
{
#ifdef __MACH__
this->spinlock = PTHREAD_MUTEX_INITIALIZER;
#else
pthread_spin_init(&this->spinlock, 0);
#endif
assert(LOGICBLOCKSIZE <= this->framesize_in_byte && "Driver_FILE: O_DIRECT cannot deal with OBJECT that is smaller than LOGICBLOCKSIZE");
#ifdef __MACH__
fd = open(this->conn_string.c_str(), O_RDWR | O_CREAT , (mode_t) 0600);
#else
fd = open64(this->conn_string.c_str(), O_RDWR | O_CREAT | O_DIRECT, (mode_t) 0600);
#endif
if(fd <= 0){
assert(false && "Driver_FILE: Fail to openfile");
}
}
开发者ID:zhangce,项目名称:deepdive,代码行数:24,代码来源:driver_file.cpp
注:本文中的pthread_spin_init函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论