本文整理汇总了C++中sem_timedwait函数的典型用法代码示例。如果您正苦于以下问题:C++ sem_timedwait函数的具体用法?C++ sem_timedwait怎么用?C++ sem_timedwait使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sem_timedwait函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: defined
bool cFUSemaphore::Down(uint32 blockTime)
{
#if defined(WIN32)
return WaitForSingleObject(semaphoreHandle, blockTime) == WAIT_OBJECT_0;
#elif defined(FP_APPLE)
return MPWaitOnSemaphore(semaphoreHandle, (blockTime == (uint32)-1) ? kDurationForever : blockTime) == noErr;
#elif defined(LINUX) || defined(ANDROID) || defined(IOS)
if (blockTime == (uint32) -1)
{
return sem_wait(&semaphoreHandle) == 0;
}
else
{
#ifdef IOS
//fuck because I am fucking lazy to implement this so...
return sem_wait(&semaphoreHandle) == 0;
#else
// The basic POSIX semaphore does not support a block time, but rather a deadline.
timespec deadline;
clock_gettime(CLOCK_REALTIME, &deadline);
deadline.tv_nsec += (blockTime * 1000);
return sem_timedwait(&semaphoreHandle, &deadline) == 0;
#endif
}
#endif
}
开发者ID:fatmingwang,项目名称:FM79979,代码行数:30,代码来源:FUSemaphore.cpp
示例2: do_test
static int
do_test (void)
{
int res = 0;
int r;
struct timespec t = { -2, 0 };
pthread_t pth;
sem_init (&sem, 0, 0);
r = sem_timedwait (&sem, &t);
if (r != -1 || errno != ETIMEDOUT)
{
puts ("sem_timedwait did not fail with ETIMEDOUT");
res = 1;
}
pthread_mutex_lock (&m1);
pthread_rwlock_wrlock (&rw1);
pthread_rwlock_rdlock (&rw2);
pthread_mutex_lock (&m2);
if (pthread_create (&pth, 0, th, 0) != 0)
{
puts ("cannot create thread");
return 1;
}
r = pthread_cond_timedwait (&c, &m2, &t);
if (r != ETIMEDOUT)
{
puts ("pthread_cond_timedwait did not return ETIMEDOUT");
res = 1;
}
void *thres;
pthread_join (pth, &thres);
return res | (thres != NULL);
}
开发者ID:AubrCool,项目名称:glibc,代码行数:35,代码来源:tst-abstime.c
示例3: UNUSED
int DbDumperWriter::write_record(bool flush)
{
int ret = OB_SUCCESS;
RecordInfo * rec = NULL;
struct timespec timeout;
UNUSED(flush);
timeout.tv_sec = time(NULL) + kSemWaitTimeout;
timeout.tv_nsec = 0;
if (!records_.empty()) {
CThreadGuard gard(&records_lock_);
//no problem doing this, because only one thread is waiting on the queue
rec = records_.front();
} else if (running_) {
sem_timedwait(&sem_empty_, &timeout);
}
if (rec != NULL) {
CThreadGuard gard(&records_lock_);
if (file_ == NULL || (ret = file_->Append(rec->buf, rec->length)) != OB_SUCCESS) {
TBSYS_LOG(ERROR, "Write record failed, ret:%d, path:%s, len:%d", ret, path_.c_str(), rec->length);
} else {
writen_lines_++;
records_.pop_front();
free_record(rec);
}
}
return ret;
}
开发者ID:Abioy,项目名称:oceanbase,代码行数:32,代码来源:db_dumper_writer.cpp
示例4: semaphore_posix_thrash
/*
* semaphore_posix_thrash()
* exercise the semaphore
*/
static void semaphore_posix_thrash(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
do {
int i;
struct timespec timeout;
if (clock_gettime(CLOCK_REALTIME, &timeout) < 0) {
pr_fail_dbg(name, "clock_gettime");
return;
}
timeout.tv_sec++;
for (i = 0; i < 1000; i++) {
if (sem_timedwait(&shared->sem_posix.sem, &timeout) < 0) {
if (errno == ETIMEDOUT)
goto timed_out;
if (errno != EINTR)
pr_fail_dbg(name, "sem_wait");
break;
}
(*counter)++;
if (sem_post(&shared->sem_posix.sem) < 0) {
pr_fail_dbg(name, "sem_post");
break;
}
timed_out:
if (!opt_do_run)
break;
}
} while (opt_do_run && (!max_ops || *counter < max_ops));
}
开发者ID:jamesodhunt,项目名称:stress-ng,代码行数:38,代码来源:stress-sem.c
示例5: XOS_DBT
///////////////////////////////////////////////////////////////////////
/// Function: TimedWait
///
/// Author: $author$
/// Date: 3/31/2012
///////////////////////////////////////////////////////////////////////
virtual XosError TimedWait
(const struct timespec& untilTime)
{
XosError error = XOS_ERROR_FAILED;
int err;
if ((m_isCreated))
if (!(err = sem_timedwait(&m_sem, &untilTime)))
error = XOS_ERROR_NONE;
else
if (ETIMEDOUT == err)
{
error = XOS_ERROR_TIMEDOUT;
XOS_DBT(("() timed out on sem_timedwait()\n"));
}
else
if (EINTR == err)
{
error = XOS_ERROR_INTERRUPTED;
XOS_DBT(("() interrupted on sem_timedwait()\n"));
}
else
{ XOS_DBE(("() failed (%d) on sem_timedwait()\n", err)); }
return error;
}
开发者ID:medusade,项目名称:mxde,代码行数:32,代码来源:XosPThreadSemaphore.hpp
示例6: semaphore_timed_wait
inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time)
{
if(abs_time == boost::posix_time::pos_infin) {
semaphore_wait(handle);
return true;
}
#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
timespec tspec = ptime_to_timespec(abs_time);
for (;;) {
int res = sem_timedwait(handle, &tspec);
if(res == 0)
return true;
if (res > 0) {
//buggy glibc, copy the returned error code to errno
errno = res;
}
if(system_error_code() == ETIMEDOUT) {
return false;
}
throw interprocess_exception(system_error_code());
}
return false;
#else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
boost::posix_time::ptime now;
do {
if(semaphore_try_wait(handle))
return true;
thread_yield();
} while((now = microsec_clock::universal_time()) < abs_time);
return false;
#endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
}
开发者ID:jellchou,项目名称:short_text_classification,代码行数:32,代码来源:semaphore_wrapper.hpp
示例7: safeTx
/* I have an XBee controlling relays for lights. It seems to become a little deaf when a number of
relays are on simultaneously, so I made safeTx(). safeTS() will retry the communication upto
retryCount times... seems to do the job :) */
int safeTx(struct xbee *xbee, struct xbee_con *con, int retryCount, struct xbee_pkt **pkt, char *format, ...) {
va_list ap;
sem_t *sem;
int ret;
if ((sem = xbee_conGetData(xbee, con)) == NULL) {
return 5;
}
/* send the request */
do {
va_start(ap, format);
ret = xbee_convTx(xbee, con, format, ap);
va_end(ap);
if (ret != 0) {
if (ret != 4) break;
} else {
/* if transmission succeeded, wait up to 5 seconds for the result (try again on timeout) */
struct timespec to;
clock_gettime(CLOCK_REALTIME, &to);
to.tv_sec += 5;
if (sem_timedwait(sem, &to)) {
printf("Timeout...\n");
ret = -1;
}
}
usleep(1000);
} while (ret && retryCount--);
*pkt = kPkt;
return ret;
}
开发者ID:TBR-Tech,项目名称:libxbee.libxbee-v2,代码行数:37,代码来源:main.c
示例8: return
bool CSem::Wait(int timeout)
{
#ifdef WIN32
if (timeout <= 0)
{
timeout = INFINITE;
}
if (WaitForSingleObject(m_hSem, timeout) != WAIT_OBJECT_0)
{
return false;
}
#else
if (timeout <= 0)
{
return (sem_wait(&m_semid) == 0);
}
else
{
timespec ts;
ts.tv_sec = time(NULL) + timeout/1000;
ts.tv_nsec = (timeout%1000)*1000000;
return (sem_timedwait(&m_semid, &ts) == 0);
}
#endif
return true;
}
开发者ID:kkiluya,项目名称:zsummerX,代码行数:26,代码来源:log4z.cpp
示例9: tf
static void *
tf (void *arg)
{
pthread_cleanup_push (cleanup, NULL);
int e = pthread_barrier_wait (&bar);
if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
{
puts ("tf: 1st barrier_wait failed");
exit (1);
}
struct timeval tv;
(void) gettimeofday (&tv, NULL);
struct timespec ts;
TIMEVAL_TO_TIMESPEC (&tv, &ts);
/* Timeout in 5 seconds. */
ts.tv_sec += 5;
/* This call should block and be cancelable. */
sem_timedwait (&sem, &ts);
pthread_cleanup_pop (0);
puts ("sem_timedwait returned");
return NULL;
}
开发者ID:czankel,项目名称:xtensa-glibc,代码行数:30,代码来源:tst-cancel14.c
示例10: sem_Lock
EXEC_RETURN sem_Lock(SEM sem,unsigned long msec){
#if defined(WIN32) || defined(_WIN64)
return WaitForSingleObject(sem,msec) == WAIT_OBJECT_0 ? EXEC_SUCCESS:EXEC_ERROR;
#else
int res;
if(!msec)
res = sem_trywait(sem);
else if(msec == ~0)
res = sem_wait(sem);
else{
#ifndef __APPLE__
struct timeval utc = {0};
struct timespec time_val = {0};
if(!gettimeofday(&utc,NULL)){
time_val.tv_sec = utc.tv_sec + msec / 1000;
msec %= 1000;
time_val.tv_nsec = utc.tv_usec * 1000 + msec * 1000000;
res = sem_timedwait(sem,&time_val);
}
#else
errno = EINVAL;
#endif
res = -1;
}
return res == 0 ? EXEC_SUCCESS:EXEC_ERROR;
#endif
}
开发者ID:mildrock,项目名称:server-cpp,代码行数:27,代码来源:ipc.c
示例11: WelsEventWaitWithTimeOut
WELS_THREAD_ERROR_CODE WelsEventWaitWithTimeOut (WELS_EVENT* event, uint32_t dwMilliseconds) {
if (dwMilliseconds != (uint32_t) - 1) {
return sem_wait (event);
} else {
#if defined(MACOS)
int32_t err = 0;
int32_t wait_count = 0;
do {
err = sem_trywait (event);
if (WELS_THREAD_ERROR_OK == err)
break;// WELS_THREAD_ERROR_OK;
else if (wait_count > 0)
break;
usleep (dwMilliseconds * 1000);
++ wait_count;
} while (1);
return err;
#else
struct timespec ts;
struct timeval tv;
gettimeofday (&tv, 0);
ts.tv_sec = tv.tv_sec + dwMilliseconds / 1000;
ts.tv_nsec = tv.tv_usec * 1000 + (dwMilliseconds % 1000) * 1000000;
return sem_timedwait (event, &ts);
#endif//MACOS
}
}
开发者ID:1and1get2,项目名称:openh264,代码行数:30,代码来源:WelsThreadLib.cpp
示例12: defined
bool uHTTP::Semaphore::wait(time_t timeoutSec) {
if (!this->isInitialized)
return false;
bool isSuccess = true;
#if defined(__APPLE__)
if (0 < timeoutSec) {
mach_timespec_t machTimeout;
machTimeout.tv_sec = (unsigned int)timeoutSec;
machTimeout.tv_nsec = 0;
isSuccess = (semaphore_timedwait(semId, machTimeout) == KERN_SUCCESS) ? true : false;
}
else {
isSuccess = (semaphore_wait(semId) == KERN_SUCCESS) ? true : false;
}
#else
if (0 < timeoutSec) {
timespec absTimeout;
absTimeout.tv_sec = timeoutSec;
absTimeout.tv_nsec = 0;
isSuccess = (sem_timedwait(&semId, &absTimeout) == 0) ? true : false;
}
else {
isSuccess = (sem_wait(&semId) == 0) ? true : false;
}
#endif
return isSuccess;
}
开发者ID:WilliamRen,项目名称:uhttp,代码行数:30,代码来源:Semaphore.cpp
示例13: reply_receive_in_buf
static cs_error_t
reply_receive_in_buf (
struct ipc_instance *ipc_instance,
void **res_msg)
{
#if _POSIX_THREAD_PROCESS_SHARED < 1
struct sembuf sop;
#else
struct timespec timeout;
struct pollfd pfd;
#endif
int res;
#if _POSIX_THREAD_PROCESS_SHARED > 0
retry_semwait:
timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
timeout.tv_nsec = 0;
res = sem_timedwait (&ipc_instance->control_buffer->sem1, &timeout);
if (res == -1 && errno == ETIMEDOUT) {
pfd.fd = ipc_instance->fd;
pfd.events = 0;
poll (&pfd, 1, 0);
if (pfd.revents == POLLERR || pfd.revents == POLLHUP) {
return (CS_ERR_LIBRARY);
}
goto retry_semwait;
}
if (res == -1 && errno == EINTR) {
goto retry_semwait;
}
#else
/*
* Wait for semaphore #1 indicating a new message from server
* to client in the response queue
*/
sop.sem_num = 1;
sop.sem_op = -1;
sop.sem_flg = 0;
retry_semop:
res = semop (ipc_instance->semid, &sop, 1);
if (res == -1 && errno == EINTR) {
return (CS_ERR_TRY_AGAIN);
} else
if (res == -1 && errno == EACCES) {
priv_change_send (ipc_instance);
goto retry_semop;
} else
if (res == -1) {
return (CS_ERR_LIBRARY);
}
#endif
*res_msg = (char *)ipc_instance->response_buffer;
return (CS_OK);
}
开发者ID:ystk,项目名称:debian-corosync,代码行数:60,代码来源:coroipcc.c
示例14: while
void *data_thread_function(void *arg) {
/* This thread reads data out of a ring buffer through a callback */
BufferSocket *bs = (BufferSocket *)arg;
RING_ITEM *this_slot;
struct timespec ts;
while (bs->run_threads) {
this_slot = bs->buf->read_ptr;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
fprintf(stderr, "Data: clock_gettime returned nonzero.\n");
bs->run_threads = 0;
continue;
}
ts.tv_nsec += 10000000; // 10 ms
// Wait for next buffer slot to fill up
if (sem_timedwait(&this_slot->read_mutex, &ts) == -1) continue;
//printf("Reading in a packet: size=%d slot=%d\n", this_slot->size, this_slot - bs->buf->list_ptr);
// Feed data from buffer slot to callback function
if (bs->callback((char *)this_slot->data,
this_slot->size, bs->userdata) != 0) {
fprintf(stderr, "Data: Callback returned nonzero.\n");
//bs->run_threads = 0;
} else {
// Release this slot for writing
sem_post(&this_slot->write_mutex);
bs->buf->read_ptr = this_slot->next;
}
}
return NULL;
}
开发者ID:HERA-Team,项目名称:PAPERCORR,代码行数:30,代码来源:buffer_socket.cpp
示例15: OMX_GetState
// Wait for a component to transition to the specified state
OMX_ERRORTYPE COpenMax::WaitForState(OMX_STATETYPE state)
{
OMX_ERRORTYPE omx_error = OMX_ErrorNone;
OMX_STATETYPE test_state;
int tries = 0;
struct timespec timeout;
omx_error = OMX_GetState(m_omx_decoder, &test_state);
#if defined(OMX_DEBUG_VERBOSE)
CLog::Log(LOGDEBUG, "%s::%s - waiting for state(%d)\n", CLASSNAME, __func__, state);
#endif
while ((omx_error == OMX_ErrorNone) && (test_state != state))
{
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 1;
sem_timedwait(m_omx_decoder_state_change, &timeout);
if (errno == ETIMEDOUT)
tries++;
if (tries > 5)
return OMX_ErrorUndefined;
omx_error = OMX_GetState(m_omx_decoder, &test_state);
}
return omx_error;
}
开发者ID:2BReality,项目名称:xbmc,代码行数:27,代码来源:OpenMax.cpp
示例16: sleep_until_start_request_or_inactivity
static int sleep_until_start_request_or_inactivity()
{
SHOW_TIME("fifo > sleep_until_start_request_or_inactivity > ENTER");
int a_start_is_required=0;
// Wait for the start request (my_sem_start_is_required).
// Besides this, if the audio stream is still busy,
// check from time to time its end.
// The end of the stream is confirmed by several checks
// for filtering underflow.
//
int i=0;
while((i<= MAX_INACTIVITY_CHECK) && !a_start_is_required)
{
if (wave_is_busy( NULL) )
{
i = 0;
}
else
{
i++;
}
int err=0;
struct timespec ts;
struct timeval tv;
clock_gettime2( &ts);
#ifdef DEBUG_ENABLED
struct timespec to;
to.tv_sec = ts.tv_sec;
to.tv_nsec = ts.tv_nsec;
#endif
add_time_in_ms( &ts, INACTIVITY_TIMEOUT);
SHOW("fifo > sleep_until_start_request_or_inactivity > start sem_timedwait (start_is_required) from %d.%09lu to %d.%09lu \n",
to.tv_sec, to.tv_nsec,
ts.tv_sec, ts.tv_nsec);
while ((err = sem_timedwait(&my_sem_start_is_required, &ts)) == -1
&& errno == EINTR)
{
continue;
}
assert (gettimeofday(&tv, NULL) != -1);
SHOW("fifo > sleep_until_start_request_or_inactivity > stop sem_timedwait (start_is_required, err=%d) %d.%09lu \n", err,
tv.tv_sec, tv.tv_usec*1000);
if (err==0)
{
a_start_is_required = 1;
}
}
SHOW_TIME("fifo > sleep_until_start_request_or_inactivity > LEAVE");
return a_start_is_required;
}
开发者ID:mondhs,项目名称:espeak,代码行数:59,代码来源:fifo.cpp
示例17: clock_gettime
bool Semaphore::wait(uint32_t timeout) {
timespec tsAbsTimeout;
clock_gettime(CLOCK_REALTIME, &tsAbsTimeout);
uint64_t absTimeout = ((uint64_t)tsAbsTimeout.tv_nsec) + (uint64_t)(timeout * 1000000LL);
tsAbsTimeout.tv_sec += absTimeout / 1000000000LL;
tsAbsTimeout.tv_nsec = absTimeout % 1000000000LL;
return (sem_timedwait(&mSemaphore, &tsAbsTimeout) == 0);
}
开发者ID:aditya7041,项目名称:My-Blog-Repository,代码行数:8,代码来源:Semaphore.cpp
示例18: main
int main(int argc, char *argv[])
{
int rc;
sem_t sem;
struct timespec tp;
rc = sem_init(&sem, PTHREAD_PROCESS_PRIVATE, 1);
assert(rc == 0);
printf("sem_init passed\n");
/*
rc = sem_wait(sem);
assert(rc == 0);
printf("sem_wait passed\n");
*/
rc = sem_trywait(sem);
assert(rc == 0);
printf("sem_trywait passed\n");
arch_time_in_timespec(&tp);
tp.tv_sec += 1;
tp.tv_nsec += 15625000;
if (tp.tv_nsec >= POW10_9) {
tp.tv_nsec -= POW10_9;
tp.tv_sec += 1;
}
rc = sem_timedwait(sem, &tp);
assert(rc == -1);
assert(errno == ETIMEDOUT);
printf("sem_timedwait passed\n");
rc = sem_post(sem);
assert(rc == 0);
printf("sem_post passed\n");
rc = sem_destroy(sem);
assert(rc == 0);
printf("sem_destroy passed\n");
sem = sem_open("MySem", 0, 0, 1);
assert(sem == NULL);
printf("sem_open passed\n");
sem = sem_open("MySem", O_CREAT, 0, 1);
assert(sem != NULL);
printf("sem_open with create passed\n");
rc = sem_close(sem);
assert(rc == 0);
printf("sem_close passed\n");
rc = sem_unlink("MySem");
assert(rc == 0);
printf("sem_unlink passed\n");
return 0;
}
开发者ID:DanielTillett,项目名称:libpthread,代码行数:58,代码来源:test_sem.c
示例19: do_test
static int
do_test (void)
{
sem_t s;
if (sem_init (&s, 0, 0) == -1)
{
puts ("sem_init failed");
return 1;
}
struct timeval tv;
if (gettimeofday (&tv, NULL) != 0)
{
puts ("gettimeofday failed");
return 1;
}
struct timespec ts;
TIMEVAL_TO_TIMESPEC (&tv, &ts);
/* Set ts to yesterday. */
ts.tv_sec -= 86400;
int type_before;
if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type_before) != 0)
{
puts ("first pthread_setcanceltype failed");
return 1;
}
errno = 0;
if (TEMP_FAILURE_RETRY (sem_timedwait (&s, &ts)) != -1)
{
puts ("sem_timedwait succeeded");
return 1;
}
if (errno != ETIMEDOUT)
{
printf ("sem_timedwait return errno = %d instead of ETIMEDOUT\n",
errno);
return 1;
}
int type_after;
if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type_after) != 0)
{
puts ("second pthread_setcanceltype failed");
return 1;
}
if (type_after != PTHREAD_CANCEL_DEFERRED)
{
puts ("sem_timedwait changed cancellation type");
return 1;
}
return 0;
}
开发者ID:Drakey83,项目名称:steamlink-sdk,代码行数:57,代码来源:tst-sem10.c
示例20: lxclock
int lxclock(struct lxc_lock *l, int timeout)
{
int ret = -1, saved_errno = errno;
struct flock lk;
switch(l->type) {
case LXC_LOCK_ANON_SEM:
if (!timeout) {
ret = sem_wait(l->u.sem);
if (ret == -1)
saved_errno = errno;
} else {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
ret = -2;
goto out;
}
ts.tv_sec += timeout;
ret = sem_timedwait(l->u.sem, &ts);
if (ret == -1)
saved_errno = errno;
}
break;
case LXC_LOCK_FLOCK:
ret = -2;
if (timeout) {
ERROR("Error: timeout not supported with flock");
ret = -2;
goto out;
}
if (!l->u.f.fname) {
ERROR("Error: filename not set for flock");
ret = -2;
goto out;
}
if (l->u.f.fd == -1) {
l->u.f.fd = open(l->u.f.fname, O_RDWR|O_CREAT,
S_IWUSR | S_IRUSR);
if (l->u.f.fd == -1) {
ERROR("Error opening %s", l->u.f.fname);
goto out;
}
}
lk.l_type = F_WRLCK;
lk.l_whence = SEEK_SET;
lk.l_start = 0;
lk.l_len = 0;
ret = fcntl(l->u.f.fd, F_SETLKW, &lk);
if (ret == -1)
saved_errno = errno;
break;
}
out:
errno = saved_errno;
return ret;
}
开发者ID:Kousalyakg,项目名称:lxc,代码行数:57,代码来源:lxclock.c
注:本文中的sem_timedwait函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论