本文整理汇总了C++中pthread_mutex_trylock函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_mutex_trylock函数的具体用法?C++ pthread_mutex_trylock怎么用?C++ pthread_mutex_trylock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_mutex_trylock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: call_trylock
int
call_trylock()
{
return pthread_mutex_trylock(&call_mutex);
}
示例2: __cilkrts_os_mutex_trylock
int __cilkrts_os_mutex_trylock(struct os_mutex *p)
{
int status;
status = pthread_mutex_trylock (&p->mutex);
return (status == 0);
}
示例3: pthread_mutex_trylock
bool PThreadMutex::TryEnter()
{
return pthread_mutex_trylock(&mutex) == 0;
}
示例4: pthread_rwlock_trywrlock
int
pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock)
{
int result, result1;
pthread_rwlock_t rwl;
if (rwlock == NULL || *rwlock == NULL)
{
return EINVAL;
}
/*
* We do a quick check to see if we need to do more work
* to initialise a static rwlock. We check
* again inside the guarded section of ptw32_rwlock_check_need_init()
* to avoid race conditions.
*/
if (*rwlock == PTHREAD_RWLOCK_INITIALIZER)
{
result = ptw32_rwlock_check_need_init (rwlock);
if (result != 0 && result != EBUSY)
{
return result;
}
}
rwl = *rwlock;
if (rwl->nMagic != PTW32_RWLOCK_MAGIC)
{
return EINVAL;
}
if ((result = pthread_mutex_trylock (&(rwl->mtxExclusiveAccess))) != 0)
{
return result;
}
if ((result =
pthread_mutex_trylock (&(rwl->mtxSharedAccessCompleted))) != 0)
{
result1 = pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
return ((result1 != 0) ? result1 : result);
}
if (rwl->nExclusiveAccessCount == 0)
{
if (rwl->nCompletedSharedAccessCount > 0)
{
rwl->nSharedAccessCount -= rwl->nCompletedSharedAccessCount;
rwl->nCompletedSharedAccessCount = 0;
}
if (rwl->nSharedAccessCount > 0)
{
if ((result =
pthread_mutex_unlock (&(rwl->mtxSharedAccessCompleted))) != 0)
{
(void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
return result;
}
if ((result =
pthread_mutex_unlock (&(rwl->mtxExclusiveAccess))) == 0)
{
result = EBUSY;
}
}
else
{
rwl->nExclusiveAccessCount = 1;
}
}
else
{
result = EBUSY;
}
return result;
}
示例5: main
int main ( void )
{
int r;
/* pthread_t thr; */
/* pthread_attr_t thra; */
pthread_mutexattr_t mxa, mxa2;
pthread_mutex_t mx, mx2, mx3, mx4;
pthread_cond_t cv;
struct timespec abstime;
pthread_rwlock_t rwl;
pthread_rwlock_t rwl2;
pthread_rwlock_t rwl3;
sem_t s1;
# if __GLIBC_PREREQ(2,4)
fprintf(stderr,
"\n\n------ This is output for >= glibc 2.4 ------\n");
# else
fprintf(stderr,
"\n\n------ This is output for < glibc 2.4 ------\n");
# endif
/* --------- pthread_create/join --------- */
fprintf(stderr,
"\n---------------- pthread_create/join ----------------\n\n");
/* make pthread_create fail */
/* It's amazingly difficult to make pthread_create fail
without first soaking up all the machine's resources.
Instead, in order to demonstrate that it's really wrapped,
create a child thread, generate a race error, and join with it
again. */
/* This just segfaults:
memset( &thra, 0xFF, sizeof(thra) );
r= pthread_create( &thr, NULL, lazy_child, NULL ); assert(r);
*/
{ pthread_t child;
r= pthread_create( &child, NULL, racy_child, NULL ); assert(!r);
sleep(1); /* just to ensure parent thread reports race, not child */
unprotected = 5678;
r= pthread_join( child, NULL ); assert(!r);
}
/* make pthread_join fail */
r= pthread_join( pthread_self(), NULL ); assert(r);
/* --------- pthread_mutex_lock et al --------- */
fprintf(stderr,
"\n---------------- pthread_mutex_lock et al ----------------\n\n");
/* make pthread_mutex_init fail */
#if defined(__sun__)
pthread_mutexattr_init( &mxa );
memset( mxa.__pthread_mutexattrp, 0xFF, 5 * sizeof(int) );
#else
memset( &mxa, 0xFF, sizeof(mxa) );
#endif
r= pthread_mutex_init( &mx, &mxa );
# if __GLIBC_PREREQ(2,4)
assert(r); /* glibc >= 2.4: the call should fail */
# else
assert(!r); /* glibc < 2.4: oh well, glibc didn't bounce this */
# endif
/* make pthread_mutex_destroy fail */
r= pthread_mutex_init( &mx2, NULL ); assert(!r);
r= pthread_mutex_lock( &mx2 ); assert(!r);
r= pthread_mutex_destroy( &mx2 );
/* make pthread_mutex_lock fail (skipped on < glibc 2.4 because it
doesn't fail, hence hangs the test) */
# if __GLIBC_PREREQ(2,4)
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_lock( &mx3 ); assert(r);
# else
fprintf(stderr, "\nmake pthread_mutex_lock fail: "
"skipped on glibc < 2.4\n\n");
# endif
/* make pthread_mutex_trylock fail */
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_trylock( &mx3 ); assert(r);
/* make pthread_mutex_timedlock fail */
memset( &abstime, 0, sizeof(abstime) );
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_timedlock( &mx3, &abstime ); assert(r);
/* make pthread_mutex_unlock fail */
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_unlock( &mx3 );
# if __GLIBC_PREREQ(2,4)
assert(r);
# else
assert(!r);
# endif
/* --------- pthread_cond_wait et al --------- */
//.........这里部分代码省略.........
示例6: return
bool Mutex::trylock() {
return (pthread_mutex_trylock(&(this->_m)) == 0);
}
示例7: my_pthread_mutex_trylock
int
my_pthread_mutex_trylock (pthread_mutex_t *__mutex)
{
pthread_mutex_t *realmutex = late_init_pthread_mutex(__mutex);
return pthread_mutex_trylock(realmutex);
}
示例8: pthread_self
bool CAMutex::Try(bool& outWasLocked)
{
bool theAnswer = false;
outWasLocked = false;
#if TARGET_OS_MAC
pthread_t theCurrentThread = pthread_self();
if(!pthread_equal(theCurrentThread, mOwner))
{
// this means the current thread doesn't already own the lock
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%p %.4f: CAMutex::Try: thread %p is try-locking %s, owner: %p\n", theCurrentThread, ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), theCurrentThread, mName, mOwner);
#endif
// go ahead and call trylock to see if we can lock it.
int theError = pthread_mutex_trylock(&mMutex);
if(theError == 0)
{
// return value of 0 means we successfully locked the lock
mOwner = theCurrentThread;
theAnswer = true;
outWasLocked = true;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%p %.4f: CAMutex::Try: thread %p has locked %s, owner: %p\n", theCurrentThread, ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), theCurrentThread, mName, mOwner);
#endif
}
else if(theError == EBUSY)
{
// return value of EBUSY means that the lock was already locked by another thread
theAnswer = false;
outWasLocked = false;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%p %.4f: CAMutex::Try: thread %p failed to lock %s, owner: %p\n", theCurrentThread, ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), theCurrentThread, mName, mOwner);
#endif
}
else
{
// any other return value means something really bad happenned
ThrowIfError(theError, CAException(theError), "CAMutex::Try: call to pthread_mutex_trylock failed");
}
}
else
{
// this means the current thread already owns the lock
theAnswer = true;
outWasLocked = false;
}
#elif TARGET_OS_WIN32
if(mOwner != GetCurrentThreadId())
{
// this means the current thread doesn't own the lock
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%lu %.4f: CAMutex::Try: thread %lu is try-locking %s, owner: %lu\n", GetCurrentThreadId(), ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), GetCurrentThreadId(), mName, mOwner);
#endif
// try to acquire the mutex
OSStatus theError = WaitForSingleObject(mMutex, 0);
if(theError == WAIT_OBJECT_0)
{
// this means we successfully locked the lock
mOwner = GetCurrentThreadId();
theAnswer = true;
outWasLocked = true;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%lu %.4f: CAMutex::Try: thread %lu has locked %s, owner: %lu\n", GetCurrentThreadId(), ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), GetCurrentThreadId(), mName, mOwner);
#endif
}
else if(theError == WAIT_TIMEOUT)
{
// this means that the lock was already locked by another thread
theAnswer = false;
outWasLocked = false;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%lu %.4f: CAMutex::Try: thread %lu failed to lock %s, owner: %lu\n", GetCurrentThreadId(), ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), GetCurrentThreadId(), mName, mOwner);
#endif
}
else
{
// any other return value means something really bad happenned
ThrowIfError(theError, CAException(GetLastError()), "CAMutex::Try: call to lock the mutex failed");
}
}
else
{
// this means the current thread already owns the lock
theAnswer = true;
outWasLocked = false;
}
#endif
return theAnswer;
}
示例9: pthread_mutex_trylock
bool CLinuxMutex::TryLock()
{
//pthread_mutex_trylock returns 0 if lock is acquired.
//Will return EBUSY if lock is not acquired
return pthread_mutex_trylock(&_mtx) == 0;
}
示例10: core_queue_packet
int core_queue_packet(struct packet *p, unsigned int flags, unsigned int thread_affinity) {
// Update the counters
registry_perf_inc(p->input->perf_pkts_in, 1);
registry_perf_inc(p->input->perf_bytes_in, p->len);
if (!core_run)
return POM_ERR;
debug_core("Queuing packet %p (%u.%06u)", p, pom_ptime_sec(p->ts), pom_ptime_usec(p->ts));
// Find the right thread to queue to
struct core_processing_thread *t = NULL;
if (flags & CORE_QUEUE_HAS_THREAD_AFFINITY) {
t = core_processing_threads[thread_affinity % core_num_threads];
pom_mutex_lock(&t->pkt_queue_lock);
} else {
static volatile unsigned int start = 0;
unsigned int i;
while (1) {
unsigned int thread_id = start;
for (i = 0; i < core_num_threads; i++) {
thread_id++;
if (thread_id >= core_num_threads)
thread_id -= core_num_threads;
t = core_processing_threads[thread_id];
int res = pthread_mutex_trylock(&t->pkt_queue_lock);
if (res == EBUSY) {
// Thread is busy, go to the next one
continue;
} else if (res) {
pomlog(POMLOG_ERR "Error while locking a processing thread pkt_queue mutex : %s", pom_strerror(res));
abort();
return POM_ERR;
}
// We've got the lock, check if it's ok to queue here
if (t->pkt_count < CORE_THREAD_PKT_QUEUE_MAX) {
// Use this thread
break;
}
// Too many packets pending in this thread, go to the next one
pom_mutex_unlock(&t->pkt_queue_lock);
}
if (i < core_num_threads) {
// We locked on a thread
start = thread_id;
break;
}
// No thread found
if (core_pkt_queue_count >= ((CORE_THREAD_PKT_QUEUE_MAX - 1) * core_num_threads)) {
// Queue full
if (flags & CORE_QUEUE_DROP_IF_FULL) {
packet_release(p);
registry_perf_inc(perf_pkt_dropped, 1);
debug_core("Dropped packet %p (%u.%06u) to thread %u", p, pom_ptime_sec(p->ts), pom_ptime_usec(p->ts));
return POM_OK;
}
// We're not going to drop this. Wait then
debug_core("All queues full. Waiting ...");
pom_mutex_lock(&core_pkt_queue_wait_lock);
// Recheck the count after locking
if (core_pkt_queue_count >= ((CORE_THREAD_PKT_QUEUE_MAX - 1) * core_num_threads)) {
int res = pthread_cond_wait(&core_pkt_queue_wait_cond, &core_pkt_queue_wait_lock);
if (res) {
pomlog(POMLOG_ERR "Error while waiting for the core pkt_queue condition : %s", pom_strerror(res));
abort();
}
}
pom_mutex_unlock(&core_pkt_queue_wait_lock);
}
}
}
// We've got the thread's lock, add it to the queue
struct core_packet_queue *tmp = NULL;
if (t->pkt_queue_unused) {
tmp = t->pkt_queue_unused;
t->pkt_queue_unused = tmp->next;
} else {
tmp = malloc(sizeof(struct core_packet_queue));
if (!tmp) {
pom_mutex_unlock(&t->pkt_queue_lock);
pom_oom(sizeof(struct core_packet_queue));
return POM_ERR;
}
}
tmp->pkt = p;
tmp->next = NULL;
//.........这里部分代码省略.........
示例11: do_test
//.........这里部分代码省略.........
return 1;
}
if (pthread_mutexattr_destroy (&a) != 0)
{
puts ("mutexattr_destroy failed");
return 1;
}
if (pthread_barrierattr_init (&ba) != 0)
{
puts ("barrierattr_init failed");
return 1;
}
if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
{
puts ("barrierattr_setpshared failed");
return 1;
}
if (pthread_barrier_init (b, &ba, 2) != 0)
{
puts ("barrier_init failed");
return 1;
}
if (pthread_barrierattr_destroy (&ba) != 0)
{
puts ("barrierattr_destroy failed");
return 1;
}
err = pthread_mutex_trylock (m);
if (err == 0)
{
puts ("mutex_trylock succeeded");
return 1;
}
else if (err != EBUSY)
{
puts ("mutex_trylock didn't return EBUSY");
return 1;
}
*p = 0;
if (pthread_mutex_unlock (m) != 0)
{
puts ("parent: 1st mutex_unlock failed");
return 1;
}
puts ("going to fork now");
pid = fork ();
if (pid == -1)
{
puts ("fork failed");
return 1;
}
else if (pid == 0)
{
if (pthread_mutex_lock (m) != 0)
{
puts ("child: mutex_lock failed");
return 1;
示例12: mutex_try_lock
inline int mutex_try_lock(mutex_t m)
{
return pthread_mutex_trylock(&m->m_mutex);
}
示例13: main
//.........这里部分代码省略.........
}
}
}
// altrimenti
else {
// prova ad aprire il file che mappa la memoria condivisa
if ((sm=shm_open("/shared_mtx",O_RDWR,S_IRUSR|S_IWUSR))<0) {
printf("Error !!! File %s, line %d\n",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
}
// se sei il primo processo
if (d==-1)
// imposta le dimensioni del file/memoria condivisa per
// contenere il mutex
if (ftruncate(sm,sizeof(pthread_mutex_t))<0) {
printf("Error !!! File %s, line %d\n",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
// mappa il file associato alla memoria ottenura dal sistema
// per la condivisone nel proprio spazio di memoria
if ((mutex=(void *)mmap(NULL,sizeof(pthread_mutex_t),PROT_WRITE|PROT_READ,MAP_SHARED,sm,0))==MAP_FAILED) {
printf("Error !!! File %s, line %d\n",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
printf("Shared mutex obtained\n");
// se sei il primo processo
if (d==-1) {
// inizializza il mutex per essere condiviso tra processi
// e non solo tra thread
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(mutex,&mattr);
pthread_mutexattr_destroy(&mattr);
printf("Mutex initialized\n");
}
// finche' non si deve uscire
while (!wanna_exit) {
// genera un intervallo casuale
i=rand()%9+1;
// esci se richiesto
if (wanna_exit) {
printf("Exit...\n");
break;
}
// o vai a dormire
printf("Going to sleep for %d sec...\n",i);
sleep(i);
// esci se richiesto
if (wanna_exit) {
printf("Exit...\n");
break;
}
// prova ad ottenere il mutex
if (pthread_mutex_trylock(mutex)) {
printf("Wait for the mutex...\n");
pthread_mutex_lock(mutex);
}
// quando l'hai ottenuto
// genera un intervallo casuale
i=rand()%9+1;
// esci se richiesto dopo aver rilasciato il mutex
if (wanna_exit) {
pthread_mutex_unlock(mutex);
printf("Exit...\n");
break;
}
// altrimenti dormi
printf("Mutex obtained and now wait for %d sec...\n",i);
sleep(i);
// se si deve uscire rilascia prima il mutex
if (wanna_exit) {
pthread_mutex_unlock(mutex);
printf("Exit...\n");
break;
}
// sblocca il mutex
printf("Unlock mutex...\n");
pthread_mutex_unlock(mutex);
}
// abbiamo finito e rimuoviamo l'associazione della file condiviso
// alla memoria del processo
munmap(mutex,sizeof(pthread_mutex_t));
// se siamo il primo processo
if (d==-1) {
// impostiamo la cancellazione del file/memoria condivisa
// (verra' effettivamente rimosso quando tutti i processi che lo
// condividono chiuderanno il descrittore relativo (in questo
// caso alla terminazione del processo)
shm_unlink("/shared_mtx");
printf("Removing shared mutex\n");
}
exit(EXIT_SUCCESS);
}
示例14: csoundLockMutexNoWait
PUBLIC int csoundLockMutexNoWait(void *mutex_)
{
return pthread_mutex_trylock((pthread_mutex_t*) mutex_);
}
示例15: try_lock
bool try_lock() { return pthread_mutex_trylock(&_pmutex) == 0; }