当前位置: 首页>>代码示例>>C++>>正文


C++ sem_timedwait函数代码示例

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


在下文中一共展示了sem_timedwait函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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


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