本文整理汇总了C++中pthread_rwlock_tryrdlock函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_rwlock_tryrdlock函数的具体用法?C++ pthread_rwlock_tryrdlock怎么用?C++ pthread_rwlock_tryrdlock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_rwlock_tryrdlock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run_free_cache
void* run_free_cache(void* args) {
AtomicCacheBase::m_last_expired_ms = currentms();
uint64_t cur_ms = 0;
while (AtomicCacheBase::m_run_thread) {
int ret = pthread_rwlock_tryrdlock(AtomicCacheBase::m_rwlock);
Sleeper sleeper;
while (ret != 0) {
sleeper.wait();
ret = pthread_rwlock_tryrdlock(AtomicCacheBase::m_rwlock);
}
// printf("rd lock %d\n",ret);
AtomicQueue<AtomicCacheBase*>::iterator iter = AtomicCacheBase::m_caches->begin();
int i = 0;
for (; iter != AtomicCacheBase::m_caches->end(); iter++) {
(*iter)->run_free();
//printf("free caches %d node_t %p base %p\n", i++, iter.ptr, *iter);
}
pthread_rwlock_unlock(AtomicCacheBase::m_rwlock);
// printf("un lock rd %d\n",ret);
// sleep
// if process has timer it will break when the timer expires
// so we need to make sure the real time
cur_ms = currentms();
while (cur_ms < AtomicCacheBase::m_last_expired_ms + AtomicCacheBase::m_max_delay_seconds * 1000) {
usleep(1000 * (AtomicCacheBase::m_last_expired_ms + AtomicCacheBase::m_max_delay_seconds * 1000 - cur_ms));
cur_ms = currentms();
}
AtomicCacheBase::m_last_expired_ms = cur_ms;
}
return NULL;
}
示例2: pthread_rwlock_rdlock
bool SafeMaze::TestRWLock()
{
for(unsigned int i = 0; i < m_uiY; i++)
{
for(unsigned int j = 0; j < m_uiX; j++)
pthread_rwlock_rdlock(&m_ppObjsMutex[i][j]);
}
for(unsigned int i = 0; i < m_uiY; i++)
{
for(unsigned int j = 0; j < m_uiX; j++)
{
bool ret = (pthread_rwlock_tryrdlock(&m_ppObjsMutex[i][j]) == 0 && pthread_rwlock_trywrlock(&m_ppObjsMutex[i][j]) != 0);
if(ret)
{
pthread_rwlock_unlock(&m_ppObjsMutex[i][j]);
pthread_rwlock_unlock(&m_ppObjsMutex[i][j]);
continue;
}
else
{
std::cout << "rwlock, x: " << j << " y: " << i << " has exception." << std::endl;
pthread_rwlock_unlock(&m_ppObjsMutex[i][j]);
pthread_rwlock_unlock(&m_ppObjsMutex[i][j]);
return false;
}
}
}
for(unsigned int i = 0; i < m_uiY; i++)
{
for(unsigned int j = 0; j < m_uiX; j++)
{
pthread_rwlock_wrlock(&m_ppObjsMutex[i][j]);
}
}
for(unsigned int i = 0; i < m_uiY; i++)
{
for(unsigned int j = 0; j < m_uiX; j++)
{
bool ret = (pthread_rwlock_tryrdlock(&m_ppObjsMutex[i][j]) != 0 && pthread_rwlock_trywrlock(&m_ppObjsMutex[i][j]));
if(ret)
{
pthread_rwlock_unlock(&m_ppObjsMutex[i][j]);
continue;
}
else
{
std::cout << "rwlock, x: " << j << " y: " << i << " has exception." << std::endl;
pthread_rwlock_unlock(&m_ppObjsMutex[i][j]);
return false;
}
}
}
return true;
}
示例3: while
void *di_rwlock_rdlock_held(di_rwlock_t * self, pthread_rwlock_t *held_lock) {
while (1) {
if (pthread_rwlock_tryrdlock(&self->rwlock1) == 0) {
*held_lock = self->rwlock1;
return self->instance1;
}
if (pthread_rwlock_tryrdlock(&self->rwlock2) == 0) {
*held_lock = self->rwlock2;
return self->instance2;
}
}
}
示例4: TEST
TEST(pthread, pthread_rwlock_smoke) {
pthread_rwlock_t l;
ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
// Single read lock
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Multiple read lock
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Write lock
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Try writer lock
ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Try reader lock
ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// Try writer lock after unlock
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
#ifdef __BIONIC__
// EDEADLK in "read after write"
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
// EDEADLK in "write after write"
ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
ASSERT_EQ(0, pthread_rwlock_unlock(&l));
#endif
ASSERT_EQ(0, pthread_rwlock_destroy(&l));
}
示例5: printf
void *rdlockThread(void *arg)
{
int rc;
int count=0;
printf("Entered thread, getting read lock with mp wait\n");
Retry:
rc = pthread_rwlock_tryrdlock(&rwlock);
if (rc == EBUSY) {
if (count >= 10) {
printf("Retried too many times, failure!\n");
exit(EXIT_FAILURE);
}
++count;
printf("Could not get lock, do other work, then RETRY...\n");
sleep(1);
goto Retry;
}
compResults("pthread_rwlock_tryrdlock() 1\n", rc);
sleep(2);
printf("unlock the read lock\n");
rc = pthread_rwlock_unlock(&rwlock);
compResults("pthread_rwlock_unlock()\n", rc);
printf("Secondary thread complete\n");
return NULL;
}
示例6: while
void *tf10(void *arg)
{
pthread_rwlock_t q_rwlock;
long long count;
struct job *p,*q;
struct job *task=NULL;
pthread_t tid;
struct timeval begintime,endtime;
float elapsed_time;
tid=pthread_self();
count=0;
while(count<100000000)
{
if(pthread_rwlock_tryrdlock(&q_rwlock)==0)
{
q=(struct job*)arg;
p=q->next;
while(p!=NULL)
{
if(tid==p->tid)
count++;
p=p->next;
}
pthread_rwlock_unlock(&q_rwlock);
}
}
gettimeofday(&endtime,NULL);
elapsed_time=1000000*(endtime.tv_sec-begintime.tv_sec)+endtime.tv_usec-begintime.tv_usec;
endtime.tv_usec-begintime.tv_usec;
elapsed_time/=1000000;
printf("This total used time is :%f seconds.\n",elapsed_time);
return (void *)0;
}
示例7: IMPL
bool RWLock::try_read_lock()
{
IMPL(impl);
if (pthread_rwlock_tryrdlock(&impl->rwlock) == 0)
return true;
return false;
}
示例8: main
int main(int argc, char** argv)
{
int r;
pthread_mutex_t mutex;
pthread_rwlock_t rwlock;
struct timespec abs_timeout;
time(&abs_timeout.tv_sec);
abs_timeout.tv_nsec = 0;
abs_timeout.tv_sec += 10;
r = pthread_rwlock_init(&rwlock, NULL); assert(r == 0);
fprintf(stderr, "Locking rwlock via pthread_rwlock_wrlock().\n");
r = pthread_rwlock_wrlock(&rwlock); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
fprintf(stderr, "Locking rwlock via pthread_rwlock_trywrlock().\n");
r = pthread_rwlock_trywrlock(&rwlock); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
fprintf(stderr, "Locking rwlock via pthread_rwlock_timedwrlock().\n");
#ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
r = pthread_rwlock_timedwrlock(&rwlock, &abs_timeout); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
#endif
fprintf(stderr, "Locking rwlock via pthread_rwlock_rdlock().\n");
r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
r = pthread_rwlock_rdlock(&rwlock); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
fprintf(stderr, "Locking rwlock via pthread_rwlock_tryrdlock().\n");
r = pthread_rwlock_tryrdlock(&rwlock); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
fprintf(stderr, "Locking rwlock via pthread_rwlock_timedrdlock().\n");
#ifdef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
r = pthread_rwlock_timedrdlock(&rwlock, &abs_timeout); assert(r == 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
#endif
fprintf(stderr, "Attempt to lock for writing recursively (not allowed).\n");
r = pthread_rwlock_wrlock(&rwlock); assert(r == 0);
r = pthread_rwlock_wrlock(&rwlock); assert(r != 0);
r = pthread_rwlock_unlock(&rwlock); assert(r == 0);
r = pthread_rwlock_destroy(&rwlock); assert(r == 0);
r = pthread_mutex_init(&mutex, NULL); assert(r == 0);
fprintf(stderr, "Locking mutex via pthread_mutex_trylock().\n");
r = pthread_mutex_trylock(&mutex); assert(r == 0);
r = pthread_mutex_unlock(&mutex); assert(r == 0);
fprintf(stderr, "Locking mutex via pthread_mutex_lock().\n");
r = pthread_mutex_lock(&mutex); assert(r == 0);
r = pthread_mutex_unlock(&mutex); assert(r == 0);
fprintf(stderr, "Locking mutex via pthread_mutex_timedlock().\n");
#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
r = pthread_mutex_timedlock(&mutex, &abs_timeout); assert(r == 0);
r = pthread_mutex_unlock(&mutex); assert(r == 0);
#endif
r = pthread_mutex_destroy(&mutex); assert(r == 0);
return 0;
}
示例9: fthXTryLock
/**
* @brief Try for cross lock (callable from fth thread ONLY).
*
* @param cross <IN> cross lock structure pointer
* @param write <IN> Nonzero for write lock, zero for read lock
*/
int fthXTryLock(XLock_t *cross, int write) {
// Wait for the fth lock to be free
if (__sync_val_compare_and_swap(&cross->fthLock, 0, 1) != 0) {
return (1);
}
// Now aquire the queing lock (should be free except for race)
while (pthread_rwlock_trywrlock(&cross->qLock) != 0) {
// Another fthread might be waiting for the lock - avoid race
fthYield(0); // Avoid race between 2 fthreads
}
// Release the FTH lock now that we have the Q lock
(void) __sync_fetch_and_sub(&cross->fthLock, 1);
// Now we have the pthread queueing lock so everyone will wait behind us
if (write) {
if (pthread_rwlock_trywrlock(&cross->lock) != 0) { // Try to get it
pthread_rwlock_unlock(&cross->qLock); // We failed - clean up
return (2); // Try failed
}
} else {
if (pthread_rwlock_tryrdlock(&cross->lock) != 0) { // Try to get it
pthread_rwlock_unlock(&cross->qLock); // We failed - clean up
return (2); // Try failed
}
}
// Release the Q lock now that we have the full lock
pthread_rwlock_unlock(&cross->qLock);
return (0);
}
示例10: TRI_TryReadLockReadWriteLock
bool TRI_TryReadLockReadWriteLock (TRI_read_write_lock_t* lock) {
int rc;
rc = pthread_rwlock_tryrdlock(lock);
return (rc == 0);
}
示例11: pthread_rwlock_tryrdlock
static void *tryrdlock(void *arg)
{
int r = pthread_rwlock_tryrdlock(arg);
if (r != EBUSY)
t_error("tryrdlock for wrlocked lock returned %s, want EBUSY\n", strerror(r));
return 0;
}
示例12: test_multiple_readers
void test_multiple_readers(void) {
fprintf(stderr, "test_multiple_readers\n");
tell_thread_to_acquire_lock(READ_LOCK);
/*
* Now attempt to acquire the lock on the main thread.
* Since they are both readers this should succeed.
* Try with tryrdlock, rdlock and timedrdlock.
*/
int rc = pthread_rwlock_tryrdlock(&g_rwlock);
ASSERT_EQ(rc, 0);
rc = pthread_rwlock_unlock(&g_rwlock);
ASSERT_EQ(rc, 0);
struct timespec t = { 0, 0 };
rc = pthread_rwlock_timedrdlock(&g_rwlock, &t);
ASSERT_EQ(rc, 0);
rc = pthread_rwlock_unlock(&g_rwlock);
ASSERT_EQ(rc, 0);
rc = pthread_rwlock_rdlock(&g_rwlock);
ASSERT_EQ(rc, 0);
rc = pthread_rwlock_unlock(&g_rwlock);
ASSERT_EQ(rc, 0);
tell_thread_to_release_lock();
}
示例13: wi_rwlock_tryrdlock
wi_boolean_t wi_rwlock_tryrdlock(wi_rwlock_t *lock) {
#ifdef WI_PTHREADS
return (pthread_rwlock_tryrdlock(&lock->rwlock) == 0);
#else
return true;
#endif
}
示例14: PlatformRWTryRLock
int PlatformRWTryRLock(RWLock lock) {
#ifndef WIN32
pthread_rwlock_t *l = lock;
return pthread_rwlock_tryrdlock(l);
#else
#endif
}
示例15: assert
bool rw_mutex::impl::read_lock(double sec)
{
#ifdef __linux__
if (!valid) return false;
#ifdef JUBATUS_UTIL_CONCURRENT_RWMUTEX_ERRORCHECK
assert(holders_lk.lock()==true);
assert(holders.count(thread::id())==0);
#endif
bool result;
if (sec<1e-9) {
result = pthread_rwlock_tryrdlock(&lk);
} else {
timespec end=to_timespec(get_clock_time()+sec);
result = pthread_rwlock_timedrdlock(&lk, &end)==0;
}
#ifdef JUBATUS_UTIL_CONCURRENT_RWMUTEX_ERRORCHECK
if (result) holders.insert(thread::id());
assert(holders_lk.unlock()==true);
#endif
return result;
#else
return false;
#endif
}