本文整理汇总了C++中TryEnterCriticalSection函数的典型用法代码示例。如果您正苦于以下问题:C++ TryEnterCriticalSection函数的具体用法?C++ TryEnterCriticalSection怎么用?C++ TryEnterCriticalSection使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TryEnterCriticalSection函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lock_with_timeout
status_t lock_with_timeout(bigtime_t time)// だめだめ
{
#ifdef NTK_WINNT
bigtime_t start_time = GetTickCount();//timeGetTime();
while(TryEnterCriticalSection(&m_critical_section))
{
if(GetTickCount() - start_time > time)//timeGetTime()
return Locker::status::TIME_OUT_ERROR;
snooze(1);// 数値は適当
}
++m_lock_count;
return st::OK;
#else
time = time;
return status_t(st::ERR,
"TryEnterCriticalSection: Windows9x ではサポートされてないようです。");
#endif
}
示例2: diff
bool MutexImpl::tryLockImpl(long milliseconds)
{
const int sleepMillis = 5;
Timestamp now;
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
do
{
try
{
if (TryEnterCriticalSection(&_cs) == TRUE)
return true;
}
catch (...)
{
throw SystemException("cannot lock mutex");
}
Sleep(sleepMillis);
}
while (!now.isElapsed(diff));
return false;
}
示例3: loom_mutex_trylock_real
int loom_mutex_trylock_real(const char *file, int line, MutexHandle m)
{
#ifndef NTELEMETRY
TmU64 matchId;
#endif
lmAssert(m != 0, "loom_mutex_lock_real - tried to lock NULL");
tmTryLockEx(gTelemetryContext, &matchId, 1000, file, line, m, "mutex_trylock");
if (TryEnterCriticalSection((CRITICAL_SECTION *)m))
{
tmEndTryLockEx(gTelemetryContext, matchId, file, line, m, TMLR_SUCCESS);
tmSetLockState(gTelemetryContext, m, TMLS_LOCKED, "mutex_trylock");
return 1;
}
else
{
tmEndTryLockEx(gTelemetryContext, matchId, file, line, m, TMLR_FAILED);
return 0;
}
}
示例4: return
bool PdfMutex::TryLock()
{
#ifdef PODOFO_MULTI_THREAD
#ifdef _WIN32
return (TryEnterCriticalSection( &m_cs ) ? true : false);
#else
int nRet = pthread_mutex_trylock( &m_mutex );
if( nRet == 0 )
return true;
else if( nRet == EBUSY )
return false;
else
{
PODOFO_RAISE_ERROR( ePdfError_MutexError );
}
#endif // _WIN32
#endif // PODOFO_MULTI_THREAD
// If we have no multithreading support always
// simulate succesfull locking
return true;
}
示例5: FlushBuffer
VOID FlushBuffer(BOOL Force)
{
if (!Force) {
BOOL Entered = TryEnterCriticalSection(&CriticalSect);
if(!Entered)
return;
} else
EnterCriticalSection(&CriticalSect);
if (BufLen != 0) {
Buf[BufLen] = 0;
Buf[BufLen+1] = 0;
DoFlushBuffer(Buf, BufLen);
// Update 'BackSpaces' for next invocation of DoFlushBuffer
BackSpaces = min(0, BackSpaces + OutputPos + BufLen);
OutputPos = BufPos - BufLen;
BufPos = 0;
BufLen = 0;
}
LeaveCriticalSection(&CriticalSect);
}
示例6: pthread_mutex_trylock_annotate_np
int pthread_mutex_trylock_annotate_np(pthread_mutex_t *mutex, const char* file, int line)
{
int contention = 0;
pthread_np_assert_live_mutex(mutex,"trylock");
if (*mutex == PTHREAD_MUTEX_INITIALIZER) {
pthread_mutex_lock(&mutex_init_lock);
if (*mutex == PTHREAD_MUTEX_INITIALIZER) {
pthread_mutex_init(mutex, NULL);
}
pthread_mutex_unlock(&mutex_init_lock);
}
if ((*mutex)->owner) {
pthshow("Mutex #x%p -> #x%p: tried contention; owned by #x%p, wanted by #x%p",
mutex, *mutex,
(*mutex)->owner,
pthread_self());
pthshow("Mutex #x%p -> #x%p: contention notes: old %s +%d, new %s +%d",
mutex, *mutex,
(*mutex)->file,(*mutex)->line, file, line);
contention = 1;
}
if (TryEnterCriticalSection(&(*mutex)->cs)) {
if (contention) {
pthshow("Mutex #x%p -> #x%p: contention end; left by #x%p, taken by #x%p",
mutex, *mutex,
(*mutex)->owner,
pthread_self());
pthshow("Mutex #x%p -> #x%p: contention notes: old %s +%d, new %s +%d",
mutex, *mutex,
(*mutex)->file,(*mutex)->line, file, line);
}
(*mutex)->owner = pthread_self();
(*mutex)->file = file;
(*mutex)->line = line;
return 0;
}
else
return EBUSY;
}
示例7: CPLAcquireMutex
int CPLAcquireMutex( void *hMutexIn, double dfWaitInSeconds )
{
#ifdef USE_WIN32_MUTEX
HANDLE hMutex = (HANDLE) hMutexIn;
DWORD hr;
hr = WaitForSingleObject( hMutex, (int) (dfWaitInSeconds * 1000) );
return hr != WAIT_TIMEOUT;
#else
CRITICAL_SECTION *pcs = (CRITICAL_SECTION *)hMutexIn;
BOOL ret;
while( (ret = TryEnterCriticalSection(pcs)) == 0 && dfWaitInSeconds > 0.0 )
{
CPLSleep( MIN(dfWaitInSeconds,0.125) );
dfWaitInSeconds -= 0.125;
}
return ret;
#endif
}
示例8: winMutexTry
static int winMutexTry(sqlite3_mutex *p){
int rc = SQLITE_BUSY;
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) );
/*
** The sqlite3_mutex_try() routine is very rarely used, and when it
** is used it is merely an optimization. So it is OK for it to always
** fail.
**
** The TryEnterCriticalSection() interface is only available on WinNT.
** And some windows compilers complain if you try to use it without
** first doing some #defines that prevent SQLite from building on Win98.
** For that reason, we will omit this optimization for now. See
** ticket #2685.
*/
#if 0
if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
p->owner = GetCurrentThreadId();
p->nRef++;
rc = SQLITE_OK;
}
#endif
return rc;
}
示例9: switch
bool recursive_mutex::scoped_lock::internal_try_acquire( recursive_mutex& m ) {
#if _WIN32||_WIN64
switch( m.state ) {
case INITIALIZED:
break;
case DESTROYED:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
break;
default:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
break;
}
#endif /* _WIN32||_WIN64 */
bool result;
#if _WIN32||_WIN64
result = TryEnterCriticalSection(&m.impl)!=0;
#else
result = pthread_mutex_trylock(&m.impl)==0;
#endif /* _WIN32||_WIN64 */
if( result )
my_mutex = &m;
return result;
}
示例10: epicsMutexOsdTryLock
/*
* epicsMutexOsdTryLock ()
*/
epicsMutexLockStatus epicsMutexOsdTryLock ( epicsMutexOSD * pSem )
{
if ( thisIsNT ) {
if ( TryEnterCriticalSection ( &pSem->os.criticalSection ) ) {
return epicsMutexLockOK;
}
else {
return epicsMutexLockTimeout;
}
}
else {
DWORD status = WaitForSingleObject ( pSem->os.mutex, 0 );
if ( status != WAIT_OBJECT_0 ) {
if (status == WAIT_TIMEOUT) {
return epicsMutexLockTimeout;
}
else {
return epicsMutexLockError;
}
}
}
return epicsMutexLockOK;
}
示例11: hal_replytimereq
static TBOOL hal_replytimereq(struct TTimeRequest *tr)
{
TBOOL success = TFALSE;
struct TMessage *msg = TGETMSGPTR(tr);
struct TMsgPort *mp = msg->tmsg_RPort;
CRITICAL_SECTION *mplock = THALGetObject((TAPTR) &mp->tmp_Lock,
CRITICAL_SECTION);
if (TryEnterCriticalSection(mplock))
{
struct TTask *sigtask = mp->tmp_SigTask;
struct HALThread *t =
THALGetObject((TAPTR) &sigtask->tsk_Thread, struct HALThread);
#ifndef HAL_USE_ATOMICS
if (TryEnterCriticalSection(&t->hth_SigLock))
#endif
{
tr->ttr_Req.io_Error = 0;
msg->tmsg_Flags = TMSG_STATUS_REPLIED | TMSGF_QUEUED;
TAddTail(&mp->tmp_MsgList, &msg->tmsg_Node);
#ifndef HAL_USE_ATOMICS
if (mp->tmp_Signal & ~t->hth_SigState)
{
t->hth_SigState |= mp->tmp_Signal;
SetEvent(t->hth_SigEvent);
}
LeaveCriticalSection(&t->hth_SigLock);
#else
if (mp->tmp_Signal &
~(TUINT) InterlockedOr(&t->hth_SigState, mp->tmp_Signal))
SetEvent(t->hth_SigEvent);
#endif
success = TTRUE;
}
LeaveCriticalSection(mplock);
}
return success;
}
示例12: Update
PLUGIN_EXPORT double Update(void* data)
{
MeasureData* measure = (MeasureData*)data;
if (TryEnterCriticalSection(&g_CriticalSection))
{
if (!g_Thread)
{
++g_UpdateCount;
if (g_UpdateCount > g_InstanceCount)
{
if (HasRecycleBinChanged())
{
// Delay next check.
g_UpdateCount = g_InstanceCount * -2;
DWORD id;
HANDLE thread = CreateThread(NULL, 0, QueryRecycleBinThreadProc, NULL, 0, &id);
if (thread)
{
CloseHandle(thread);
g_Thread = true;
}
}
else
{
g_UpdateCount = 0;
}
}
}
LeaveCriticalSection(&g_CriticalSection);
}
return measure->count ? g_BinCount : g_BinSize;
}
示例13: TryLock
bool TryLock() {
return TryEnterCriticalSection(&critical_section) != 0;
};
示例14: TryEnterCriticalSection
//==============================================================================
Bool Mutex::tryLock()
{
CRITICAL_SECTION* mtx = reinterpret_cast<CRITICAL_SECTION*>(m_impl);
BOOL enter = TryEnterCriticalSection(mtx);
return enter;
}
示例15: TryLock
bit OwnedLock::TryLock() const
{
if (TryEnterCriticalSection((CRITICAL_SECTION*)hand)) return true;
else return false;
}