• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ pthread_rwlock_tryrdlock函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中pthread_rwlock_tryrdlock函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_rwlock_tryrdlock函数的具体用法?C++ pthread_rwlock_tryrdlock怎么用?C++ pthread_rwlock_tryrdlock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了pthread_rwlock_tryrdlock函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:linqingtao,项目名称:tinytable,代码行数:31,代码来源:atomiccache.cpp


示例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;
}
开发者ID:spitbreak,项目名称:MazeExplorer,代码行数:57,代码来源:safemaze.cpp


示例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;
        }
    }
}
开发者ID:bowlofstew,项目名称:ConcurrencyFreaks,代码行数:12,代码来源:di_rwlock.c


示例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));
}
开发者ID:nick0lay,项目名称:platform_bionic,代码行数:49,代码来源:pthread_test.cpp


示例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;
}
开发者ID:jlopezr,项目名称:zmq-broker,代码行数:30,代码来源:test01.c


示例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;
}
开发者ID:lixu1,项目名称:LinuxC,代码行数:33,代码来源:3_rwlock.c


示例7: IMPL

 bool RWLock::try_read_lock()
 {
     IMPL(impl);
     if (pthread_rwlock_tryrdlock(&impl->rwlock) == 0)
         return true;
     return false;
 }
开发者ID:BackupTheBerlios,项目名称:xicorr-svn,代码行数:7,代码来源:rw_lock.cpp


示例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;
}
开发者ID:520SRig,项目名称:valgrind,代码行数:60,代码来源:trylock.c


示例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);

}
开发者ID:AlfredChenxf,项目名称:zetascale,代码行数:41,代码来源:fthXLock.c


示例10: TRI_TryReadLockReadWriteLock

bool TRI_TryReadLockReadWriteLock (TRI_read_write_lock_t* lock) {
  int rc;

  rc = pthread_rwlock_tryrdlock(lock);

  return (rc == 0);
}
开发者ID:eranshir,项目名称:ArangoDB,代码行数:7,代码来源:locks-posix.c


示例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;
}
开发者ID:andrey-gvrd,项目名称:rusl,代码行数:7,代码来源:pthread_rwlock-ebusy.c


示例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();
}
开发者ID:dsagal,项目名称:native_client,代码行数:27,代码来源:rwlock_test.c


示例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
}
开发者ID:ProfDrLuigi,项目名称:zanka,代码行数:7,代码来源:wi-lock.c


示例14: PlatformRWTryRLock

int PlatformRWTryRLock(RWLock lock) {
#ifndef WIN32
   pthread_rwlock_t *l = lock;
   return pthread_rwlock_tryrdlock(l);
#else
#endif
}
开发者ID:sscreation,项目名称:BlockManager,代码行数:7,代码来源:platformops.c


示例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
}
开发者ID:jubatus,项目名称:jubatus_core,代码行数:25,代码来源:rwmutex.cpp


示例16: ptt_pthread_rwlock_tryrdlock

DLL_EXPORT int ptt_pthread_rwlock_tryrdlock(RWLOCK *rwlock, char *loc)
{
int result;
    PTTRACE ("tryrd before", rwlock, NULL, loc, PTT_MAGIC);
    result = pthread_rwlock_tryrdlock(rwlock);
    PTTRACE ("tryrd after", rwlock, NULL, loc, result);
    return result;
}
开发者ID:adozenlines,项目名称:sandhawk,代码行数:8,代码来源:pttrace.c


示例17: pthread_rwlock_tryrdlock

// ReadLock with absolute timeout
bool RWLockImplPosix::readLock(const ::icl_core::TimeStamp& timeout)
{
#ifdef _SYSTEM_DARWIN_
  int ret = pthread_rwlock_tryrdlock(m_rwlock);
  while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
  {
    // one microsecond
    icl_core::os::usleep(1);
    ret = pthread_rwlock_tryrdlock(m_rwlock);
  }
  return (ret == 0);
#else
  struct timespec timeout_timespec = timeout.timespec();
  int ret = pthread_rwlock_timedrdlock(m_rwlock, &timeout_timespec);
  return (ret == 0);
#endif
}
开发者ID:FelixPe,项目名称:gpu-voxels,代码行数:18,代码来源:RWLockImplPosix.cpp


示例18: try_read_locker

 try_read_locker(pthread_rwlock_t &lock)
   : L(lock), ok(false)
 {
   int rv = pthread_rwlock_tryrdlock(&L);
   if(rv != 0 && rv != EBUSY)
     throw strerror_exception("Failed trying rdlock", rv);
   ok = (rv == 0);
 }
开发者ID:beevek,项目名称:libkrb,代码行数:8,代码来源:locker.hpp


示例19: read_try_lock

Error RWLockPosix::read_try_lock() {

	if (pthread_rwlock_tryrdlock(&rwlock) != 0) {
		return ERR_BUSY;
	} else {
		return OK;
	}
}
开发者ID:KellyThomas,项目名称:godot,代码行数:8,代码来源:rw_lock_posix.cpp


示例20: __ast_rwlock_tryrdlock

int __ast_rwlock_tryrdlock(const char *filename, int line, const char *func, ast_rwlock_t *t, const char *name)
{
	int res;

#ifdef DEBUG_THREADS
	struct ast_lock_track *lt = NULL;
#ifdef HAVE_BKTR
	struct ast_bt *bt = NULL;
#endif

	if (t->tracking) {
		lt = ast_get_reentrancy(&t->track);
	}

	if (lt) {
#ifdef HAVE_BKTR
		struct ast_bt tmp;

		/* The implementation of backtrace() may have its own locks.
		 * Capture the backtrace outside of the reentrancy lock to
		 * avoid deadlocks. See ASTERISK-22455. */
		ast_bt_get_addresses(&tmp);

		ast_reentrancy_lock(lt);
		if (lt->reentrancy < AST_MAX_REENTRANCY) {
			lt->backtrace[lt->reentrancy] = tmp;
			bt = &lt->backtrace[lt->reentrancy];
		}
		ast_reentrancy_unlock(lt);

		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t, bt);
#else
		ast_store_lock_info(AST_RDLOCK, filename, line, func, name, t);
#endif
	}
#endif /* DEBUG_THREADS */

	res = pthread_rwlock_tryrdlock(&t->lock);

#ifdef DEBUG_THREADS
	if (!res && lt) {
		ast_reentrancy_lock(lt);
		if (lt->reentrancy < AST_MAX_REENTRANCY) {
			lt->file[lt->reentrancy] = filename;
			lt->lineno[lt->reentrancy] = line;
			lt->func[lt->reentrancy] = func;
			lt->thread[lt->reentrancy] = pthread_self();
			lt->reentrancy++;
		}
		ast_reentrancy_unlock(lt);
		ast_mark_lock_acquired(t);
	} else if (lt) {
		ast_mark_lock_failed(t);
	}
#endif /* DEBUG_THREADS */

	return res;
}
开发者ID:LorneyZizzart,项目名称:asterisk,代码行数:58,代码来源:lock.c



注:本文中的pthread_rwlock_tryrdlock函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ pthread_rwlock_trywrlock函数代码示例发布时间:2022-05-30
下一篇:
C++ pthread_rwlock_rdlock函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap