int tcp_sleep(int msec) { #ifdef PVWIN32 Sleep(msec); return 0; #endif #ifdef PVUNIX fd_set wset,rset,eset; struct timeval timeout; FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset); timeout.tv_sec = msec / 1000; timeout.tv_usec = (msec % 1000) * 1000; select(1,&rset,&wset,&eset,&timeout); return 0; #endif #ifdef __VMS struct timespec interval; interval.tv_sec = msec / 1000; interval.tv_nsec = (msec % 1000) * 1000 * 1000; /* wait msec msec */ pthread_delay_np(&interval); return 0; #endif }
int main(int argc, char * argv[]) { struct timespec interval = {1L, 500000000L}; assert(pthread_delay_np(&interval) == 0); return 0; }
// Sleep for a number of microseconds int usleep(unsigned int microseconds) { struct timespec waittime; waittime.tv_sec = microseconds / 1000000; waittime.tv_nsec = (microseconds % 1000000) * 1000; pthread_delay_np ( &waittime ); return 0; }
void reader_function(void) { while(1) { sem_wait( &readers_turn ); printf("consume_item\n"); pthread_delay_np( &delay ); sem_post( &writers_turn ); } }
void ThreadImpl::sleepImpl(long milliseconds) { #if defined(__digital__) // This is specific to DECThreads struct timespec interval; interval.tv_sec = milliseconds / 1000; interval.tv_nsec = (milliseconds % 1000)*1000000; pthread_delay_np(&interval); #elif POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_ANDROID || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX || POCO_OS == POCO_OS_VXWORKS Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); int rc; do { struct timespec ts; ts.tv_sec = (long) remainingTime.totalSeconds(); ts.tv_nsec = (long) remainingTime.useconds()*1000; Poco::Timestamp start; rc = ::nanosleep(&ts, 0); if (rc < 0 && errno == EINTR) { Poco::Timestamp end; Poco::Timespan waited = start.elapsed(); if (waited < remainingTime) remainingTime -= waited; else remainingTime = 0; } } while (remainingTime > 0 && rc < 0 && errno == EINTR); if (rc < 0 && remainingTime > 0) throw Poco::SystemException("Thread::sleep(): nanosleep() failed"); #else Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); int rc; do { struct timeval tv; tv.tv_sec = (long) remainingTime.totalSeconds(); tv.tv_usec = (long) remainingTime.useconds(); Poco::Timestamp start; rc = ::select(0, NULL, NULL, NULL, &tv); if (rc < 0 && errno == EINTR) { Poco::Timestamp end; Poco::Timespan waited = start.elapsed(); if (waited < remainingTime) remainingTime -= waited; else remainingTime = 0; } } while (remainingTime > 0 && rc < 0 && errno == EINTR); if (rc < 0 && remainingTime > 0) throw Poco::SystemException("Thread::sleep(): select() failed"); #endif }
// 函数名: mysleep // 编程 : 陈永华 2004-2-11 12:35:51 // 描述 : 本线程进入睡眠状态milliseconds毫秒 // 返回 : void // 参数 : unsigned int milliseconds void mysleep(unsigned int milliseconds) { #ifdef WIN32 Sleep(milliseconds); #else #ifdef AIX struct timespec delay; delay.tv_sec =0; delay.tv_nsec = milliseconds*1000; pthread_delay_np(&delay); #else usleep(milliseconds*1000); #endif #endif }
void vogl_sleep(unsigned int milliseconds) { #ifdef WIN32 struct timespec interval; interval.tv_sec = milliseconds / 1000; interval.tv_nsec = (milliseconds % 1000) * 1000000L; pthread_delay_np(&interval); #else while (milliseconds) { int msecs_to_sleep = VOGL_MIN(milliseconds, 1000); usleep(msecs_to_sleep * 1000); milliseconds -= msecs_to_sleep; } #endif }
void * func(void * arg) { struct timespec interval = {5, 500000000L}; assert(pthread_mutex_lock(&mx) == 0); #ifdef _MSC_VER #pragma inline_depth(0) #endif pthread_cleanup_push(pthread_mutex_unlock, &mx); assert(pthread_delay_np(&interval) == 0); pthread_cleanup_pop(1); #ifdef _MSC_VER #pragma inline_depth() #endif return (void *)(size_t)1; }
void thread::sleep(const xtime& xt) { for (int foo=0; foo < 5; ++foo) { #if defined(BOOST_HAS_WINTHREADS) int milliseconds; to_duration(xt, milliseconds); Sleep(milliseconds); #elif defined(BOOST_HAS_PTHREADS) # if defined(BOOST_HAS_PTHREAD_DELAY_NP) timespec ts; to_timespec_duration(xt, ts); int res = 0; res = pthread_delay_np(&ts); assert(res == 0); # elif defined(BOOST_HAS_NANOSLEEP) timespec ts; to_timespec_duration(xt, ts); // nanosleep takes a timespec that is an offset, not // an absolute time. nanosleep(&ts, 0); # else mutex mx; mutex::scoped_lock lock(mx); condition cond; cond.timed_wait(lock, xt); # endif #elif defined(BOOST_HAS_MPTASKS) int microseconds; to_microduration(xt, microseconds); Duration lMicroseconds(kDurationMicrosecond * microseconds); AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds)); threads::mac::detail::safe_delay_until(&sWakeTime); #endif xtime cur; xtime_get(&cur, TIME_UTC); if (xtime_cmp(xt, cur) <= 0) return; } }
void sleep(const system_time& st) { detail::thread_data_base* const thread_info=detail::get_current_thread_data(); if(thread_info) { unique_lock<mutex> lk(thread_info->sleep_mutex); while(thread_info->sleep_condition.timed_wait(lk,st)); } else { xtime const xt=get_xtime(st); for (int foo=0; foo < 5; ++foo) { # if defined(BOOST_HAS_PTHREAD_DELAY_NP) timespec ts; to_timespec_duration(xt, ts); BOOST_VERIFY(!pthread_delay_np(&ts)); # elif defined(BOOST_HAS_NANOSLEEP) timespec ts; to_timespec_duration(xt, ts); // nanosleep takes a timespec that is an offset, not // an absolute time. nanosleep(&ts, 0); # else mutex mx; mutex::scoped_lock lock(mx); condition cond; cond.timed_wait(lock, xt); # endif xtime cur; xtime_get(&cur, TIME_UTC); if (xtime_cmp(xt, cur) <= 0) return; } } }
void* DataFream(void* Param) { while(DataLoopDF) { JoyStictUpdeta(); pthread_mutex_lock( &mutex_DataFream ); DataLoopDF=DataLoop; rtri+=0.2f; if(rtri>=360.0f) rtri=rtri-360.0f; rquad-=0.15f; if(rquad>=360.0f) rquad=rquad-360.0f; testframe1=testframe1+0.25f; if(testframe1>=100.0f) testframe1=0.0f; UpDataInputDF=true; pthread_mutex_unlock( &mutex_DataFream ); /* if(ispad&&ispadEffect) { g_pDevice->Acquire(); if( g_pEffect ) g_pEffect->Start( 1, 0 ); // Start the effect }*/ //UpdateInputState(MainhDlg); QueryPerformanceFrequency(&feqf); QueryPerformanceCounter(&DataThreadf); delay.tv_nsec=max(10000000-int((DataThreadf.QuadPart-DataThread.QuadPart)/feqf.QuadPart*1000000000.0),100); pthread_delay_np( &delay ); QueryPerformanceCounter(&DataThread); } return NULL; }
void TerminateSpy () { struct timespec killSleep; void *exit_code; if (NULL == pthread_getspecific (mainThread)) return; if (spyThreadSetup) { pthread_cancel (spyThread); killSleep.tv_sec = 1; killSleep.tv_nsec = 250000000; pthread_delay_np (&killSleep); pthread_join (spyThread, &exit_code); spyThreadSetup = FALSE; } if (spyLockSetup) { pthread_mutex_destroy (&spyLock); spyLockSetup = FALSE; } if (spy != -1) { close(spy); spy = -1; } }
void IvoryLifePolling (pthread_addr_t argument) { pthread_t self = pthread_self (); struct timespec pollingSleep; pollingSleep.tv_sec = 0; pollingSleep.tv_nsec = 0; pthread_cleanup_push ((pthread_cleanuproutine_t)pthread_detach, (void*)self); while (TRUE) { begin_MUTEX_LOCKED (signalLock); EmbCommAreaPtr->pollTime += pollingSleep.tv_nsec; PollMessageChannels (); if (EmbCommAreaPtr->reset_request != NoResetRequest) ProcessResetRequest (); else if (EmbCommAreaPtr->pollTime > OneQuarterSecond) { EmbCommAreaPtr->pollTime = 0; EmbCommAreaPtr->guest_to_host_signals |= EmbCommAreaPtr->live_guest_to_host_signals; if (pthread_cond_broadcast (&EmbCommAreaPtr->signalSignal)) vpunt (NULL, "Unable to send Life Support signal signal in thread %lx", self); } else if (EmbCommAreaPtr->reawaken) { EmbCommAreaPtr->guest_to_host_signals |= EmbCommAreaPtr->reawaken; EmbCommAreaPtr->reawaken = 0; if (pthread_cond_broadcast (&EmbCommAreaPtr->signalSignal)) vpunt (NULL, "Unable to send Life Support signal signal in thread %lx", self); } end_MUTEX_LOCKED (signalLock); if (EmbCommAreaPtr->clock_interval > 0) { EmbCommAreaPtr->pollClockTime -= pollingSleep.tv_nsec; if (EmbCommAreaPtr->pollClockTime <= 0) { EmbSendSignal (EmbCommAreaPtr->clock_signal); EmbCommAreaPtr->pollClockTime = 1000 * EmbCommAreaPtr->clock_interval; } if (EmbCommAreaPtr->pollClockTime > OneQuarterSecond) pollingSleep.tv_nsec = OneQuarterSecond; else pollingSleep.tv_nsec = EmbCommAreaPtr->pollClockTime; } else pollingSleep.tv_nsec = OneSixteenthSecond; UpdateVLMStatus (); if (0) { printf("sleep; interval %d, time %d, %d\n", EmbCommAreaPtr->clock_interval, pollingSleep.tv_sec, pollingSleep.tv_nsec); } pollingSleep.tv_sec = 1; pollingSleep.tv_nsec = 0; if (pthread_delay_np (&pollingSleep)) vpunt (NULL, "Unable to sleep in thread %lx", self); } pthread_cleanup_pop (TRUE); }