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


C++ pthread_mutex_timedlock函数代码示例

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


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

示例1: pthread_rwlock_timedrdlock

int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock_, const struct timespec *ts)
{
  rwlock_t *rwlock;
  int ret;

  pthread_testcancel();

  ret = rwl_ref(rwlock_,0);
  if(ret != 0) return ret;

  rwlock = (rwlock_t *)*rwlock_;
  if ((ret = pthread_mutex_timedlock (&rwlock->mex, ts)) != 0)
      return rwl_unref(rwlock_, ret);
  InterlockedIncrement(&rwlock->nsh_count);
  if (rwlock->nsh_count == INT_MAX)
  {
    ret = pthread_mutex_timedlock(&rwlock->mcomplete, ts);
    if (ret != 0)
    {
      if (ret == ETIMEDOUT)
	InterlockedIncrement(&rwlock->ncomplete);
      pthread_mutex_unlock(&rwlock->mex);
      return rwl_unref(rwlock_, ret);
    }
    rwlock->nsh_count -= rwlock->ncomplete;
    rwlock->ncomplete = 0;
    ret = rwlock_free_both_locks(rwlock, 0);
    return rwl_unref(rwlock_, ret);
  }
  ret = pthread_mutex_unlock(&rwlock->mex);
  return rwl_unref(rwlock_, ret);
}
开发者ID:AndrewVos,项目名称:rubinius,代码行数:32,代码来源:rwlock.c

示例2: mono_mutex_timedlock

int
mono_mutex_timedlock (mono_mutex_t *mutex, const struct timespec *timeout)
{
	pthread_t id;
	
	switch (mutex->type) {
	case MONO_MUTEX_NORMAL:
		return pthread_mutex_timedlock (&mutex->mutex, timeout);
	case MONO_MUTEX_RECURSIVE:
		id = pthread_self ();
		
		if (pthread_mutex_timedlock (&mutex->mutex, timeout) != 0)
			return ETIMEDOUT;
		
		while (1) {
			if (pthread_equal (mutex->owner, MONO_THREAD_NONE)) {
				mutex->owner = id;
				mutex->depth = 1;
				break;
			} else if (pthread_equal (mutex->owner, id)) {
				mutex->depth++;
				break;
			} else {
				mutex->waiters++;
				if (pthread_cond_timedwait (&mutex->cond, &mutex->mutex, timeout) != 0)
					return ETIMEDOUT;
				mutex->waiters--;
			}
		}
		
		return pthread_mutex_unlock (&mutex->mutex);
	}
	
	return EINVAL;
}
开发者ID:AveProjVstm,项目名称:MonoVstm,代码行数:35,代码来源:mono-mutex.c

示例3: do_test

static int
do_test (void)
{
  struct timespec tms = { 0 };
  pthread_mutex_t mutex;
  pthread_mutexattr_t mutexattr;
  int ret = 0;

  if (pthread_mutexattr_init (&mutexattr) != 0)
    return 1;
  if (pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_ERRORCHECK) != 0)
    return 1;

  if (pthread_mutex_init (&mutex, &mutexattr) != 0)
    return 1;
  if (pthread_mutexattr_destroy (&mutexattr) != 0)
    return 1;

  /* The call to pthread_mutex_timedlock erroneously enabled lock elision
     on the mutex, which then triggered an assertion failure in
     pthread_mutex_unlock.  It would also defeat the error checking nature
     of the mutex.  */
  if (pthread_mutex_timedlock (&mutex, &tms) != 0)
    return 1;
  if (pthread_mutex_timedlock (&mutex, &tms) != EDEADLK)
    {
      printf ("Failed error checking on locked mutex\n");
      ret = 1;
    }

  if (pthread_mutex_unlock (&mutex) != 0)
    ret = 1;

  return ret;
}
开发者ID:andigena,项目名称:glibc-2.23-0ubuntu3,代码行数:35,代码来源:tst-mutex-errorcheck.c

示例4: acquire_locklock

/** 
 * Obtain lock on debug lock structure. This could be a deadlock by the caller.
 * The debug code itself does not deadlock. Anyway, check with timeouts. 
 * @param lock: on what to acquire lock.
 * @param func: user level caller identification.
 * @param file: user level caller identification.
 * @param line: user level caller identification.
 */
static void
acquire_locklock(struct checked_lock* lock, 
	const char* func, const char* file, int line)
{
	struct timespec to;
	int err;
	int contend = 0;
	/* first try; inc contention counter if not immediately */
	if((err = pthread_mutex_trylock(&lock->lock))) {
		if(err==EBUSY)
			contend++;
		else fatal_exit("error in mutex_trylock: %s", strerror(err));
	}
	if(!err)
		return; /* immediate success */
	to.tv_sec = time(NULL) + CHECK_LOCK_TIMEOUT;
	to.tv_nsec = 0;
	err = pthread_mutex_timedlock(&lock->lock, &to);
	if(err) {
		log_err("in acquiring locklock: %s", strerror(err));
		lock_error(lock, func, file, line, "acquire locklock");
	}
	/* since we hold the lock, we can edit the contention_count */
	lock->contention_count += contend;
}
开发者ID:Coder420,项目名称:bitmonero,代码行数:33,代码来源:checklocks.c

示例5: mutexTryLock

/*----------------------------------------------------------------------------*/
bool mutexTryLock(struct Mutex *mutex, unsigned int interval)
{
  int res;

  if (interval)
  {
    struct timespec timestamp;

    clock_gettime(CLOCK_REALTIME, &timestamp);
    timestamp.tv_sec += interval / 1000;
    timestamp.tv_nsec += (interval % 1000) * 1000000;

    if (timestamp.tv_nsec >= 1000000000)
    {
      timestamp.tv_nsec -= 1000000000;
      ++timestamp.tv_sec;
    }

    res = pthread_mutex_timedlock(mutex->handle, &timestamp);
  }
  else
    res = pthread_mutex_trylock(mutex->handle);

  return res ? false : true;
}
开发者ID:stxent,项目名称:osw,代码行数:26,代码来源:mutex.c

示例6: main

int main(void)
{
    int err;
    struct timespec tout;
    struct tm *tmp;
    char buf[64];
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

    pthread_mutex_lock(&lock);
    printf("mutex is locked\n");
    clock_gettime(CLOCK_REALTIME, &tout);
    tmp = localtime(&tout.tv_sec);
    strftime(buf, sizeof(buf), "%r", tmp);
    printf("current time is %s\n", buf);

    tout.tv_sec += 10;
    err = pthread_mutex_timedlock(&lock, &tout);
    tmp = localtime(&tout.tv_sec);
    strftime(buf, sizeof(buf), "%r", tmp);
    printf("the time is now %s\n", buf);

    if(err == 0)
        printf("mutex locked again!\n");
    else 
        printf("can not lock mvtex agin:%s\n",strerror(err));
    return 0;
}
开发者ID:Huangtuzhi,项目名称:APUE,代码行数:27,代码来源:mutex_timedlock.cpp

示例7: almtx_timedlock

int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
{
    int ret;

#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
    ret = pthread_mutex_timedlock(mtx, ts);
    switch(ret)
    {
        case 0: return althrd_success;
        case ETIMEDOUT: return althrd_timedout;
        case EBUSY: return althrd_busy;
    }
    return althrd_error;
#else
    if(!mtx || !ts)
        return althrd_error;

    while((ret=almtx_trylock(mtx)) == althrd_busy)
    {
        struct timespec now;

        if(ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000 ||
           altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
            return althrd_error;
        if(now.tv_sec > ts->tv_sec || (now.tv_sec == ts->tv_sec && now.tv_nsec >= ts->tv_nsec))
            return althrd_timedout;

        althrd_yield();
    }

    return ret;
#endif
}
开发者ID:Kingsquee,项目名称:crown,代码行数:33,代码来源:threads.c

示例8: gettimeofday

/****************************
 *
 * Thread's start routine.
 * f1()
 *
 * *************************/
void *f1(void *parm)
{
	struct timespec timeout;

	/* Get the current time before the mutex locked. */
	gettimeofday(&currsec1, NULL);

	/* Absolute time, not relative. */
	timeout.tv_sec = currsec1.tv_sec + TIMEOUT;
	timeout.tv_nsec = currsec1.tv_usec * 1000;

	printf
	    ("Timed mutex lock will block for %d seconds starting from: %ld.%06ld\n",
	     TIMEOUT, (long)currsec1.tv_sec, (long)currsec1.tv_usec);
	if (pthread_mutex_timedlock(&mutex, &timeout) != ETIMEDOUT) {
		perror("Error in pthread_mutex_timedlock().\n");
		pthread_exit((void *)PTS_UNRESOLVED);
		return (void *)PTS_UNRESOLVED;
	}

	/* Get time after the mutex timed out in locking. */
	gettimeofday(&currsec2, NULL);

	pthread_exit(0);
	return (void *)(0);
}
开发者ID:1587,项目名称:ltp,代码行数:32,代码来源:1-1.c

示例9: timed_lock

 bool timed_lock(system_time const & abs_time)
 {
     struct timespec const timeout=detail::get_timespec(abs_time);
     int const res=pthread_mutex_timedlock(&m,&timeout);
     BOOST_ASSERT(!res || res==ETIMEDOUT);
     return !res;
 }
开发者ID:3DJ,项目名称:ofxOgre,代码行数:7,代码来源:recursive_mutex.hpp

示例10: main

int main(int argc, char *argv[]) {

  int retcode;

  use_timeout = (argc >= 2 && strcmp(argv[1], "-t") == 0);
  if (is_help_requested(argc, argv)) {
    usage(argv[0], "");
  }

  timeout.tv_sec  = (time_t) 200;
  timeout.tv_nsec = (long) 0;
  if (use_timeout && argc >= 3) {
    int t;
    sscanf(argv[2], "%d", &t);
    timeout.tv_sec = (time_t) t;
  }
  printf("timout(%ld sec %ld msec)\n", (long) timeout.tv_sec, (long) timeout.tv_nsec);
  
  pthread_t thread[N_THREADS];
  int tidx[N_THREADS];
  for (size_t i = 0; i < N_THREADS; i++) {
    tidx[i] = (int) i;
    retcode = pthread_create(thread + i, NULL, run, (void *) (tidx + i)); // equivalent: ((void *) tidx) + sizeof(int)*i
    handle_thread_error(retcode, "creating thread failed", PROCESS_EXIT);
  }
  printf("in parent: setting up\n");

  pthread_mutex_init(&mutex, NULL);
  sleep(2);
  printf("in parent: getting mutex\n");
  if (use_timeout) {
    while (TRUE) {
      retcode = pthread_mutex_timedlock(&mutex, &timeout);
      if (retcode == 0) {
        break;
      } else if (retcode == ETIMEDOUT) {
        printf("timed out in parent\n");
      } else {
        handle_thread_error(retcode, "parent failed (timed)lock", PROCESS_EXIT);
      }
    }
  } else {
    retcode = pthread_mutex_lock(&mutex);
    handle_thread_error(retcode, "parent failed lock", PROCESS_EXIT);
  }
  printf("parent got mutex\n");
  sleep(5);
  printf("parent releases mutex\n");
  pthread_mutex_unlock(&mutex);
  printf("parent released mutex\n");
  printf("parent waiting for child to terminate\n");
  for (size_t i = 0; i < N_THREADS; i++) {
    retcode = pthread_join(thread[i], NULL);
    handle_thread_error(retcode, "join failed", PROCESS_EXIT);
    printf("joined thread %d\n", (int) i);
  }
  pthread_mutex_destroy(&mutex);
  printf("done\n");
  exit(0);
}
开发者ID:bk1,项目名称:sysprogramming-examples,代码行数:60,代码来源:mutex-many-threads-read-stdin.c

示例11: ShbIpcEnterAtomicSection

//------------------------------------------------------------------------------
// Function:    ShbIpcEnterAtomicSection
//
// Description: Enter atomic section for Shared Buffer access
//
// Parameters:  pShbInstance_p          pointer to shared buffer instance
//
// Return:      tShbError      = error code
//------------------------------------------------------------------------------
tShbError  ShbIpcEnterAtomicSection (tShbInstance pShbInstance_p)
{
    tShbMemInst         *pShbMemInst;
    tShbMemHeader       *pShbMemHeader;
    tShbError           ShbError;
    struct timespec     curTime, timeout;

    if (pShbInstance_p == NULL)
    {
        return (kShbInvalidArg);
    }

    pShbMemInst = ShbIpcGetShbMemInst (pShbInstance_p);
    pShbMemHeader = ShbIpcGetShbMemHeader (pShbMemInst);

    clock_gettime(CLOCK_REALTIME, &curTime);
    timeout.tv_sec = 0;
    timeout.tv_nsec = TIMEOUT_ENTER_ATOMIC * 1000;
    timespecadd(&timeout, &curTime);

    if (pthread_mutex_timedlock(&pShbMemHeader->m_mutexBuffAccess, &timeout) == 0)
    {
        ShbError = kShbOk;
    }
    else
    {
        ShbError = kShbBufferInvalid;
    }

    return (ShbError);
}
开发者ID:cheeryguo,项目名称:BnR_MC,代码行数:40,代码来源:ShbIpc-LinuxPthreads.c

示例12: pthread_mutex_lock

int pthread_mutex_lock(pthread_mutex_t* m) {
    if ((m->_m_type & PTHREAD_MUTEX_MASK) == PTHREAD_MUTEX_NORMAL &&
        !a_cas_shim(&m->_m_lock, 0, EBUSY))
        return 0;

    return pthread_mutex_timedlock(m, 0);
}
开发者ID:saltstar,项目名称:smartnix,代码行数:7,代码来源:pthread_mutex_lock.c

示例13: printf

void *run(void *arg) {
  int *argi = (int *) arg;
  int my_tidx = *argi;
  
  int retcode;
  printf("in child %d: sleeping\n", my_tidx);
  sleep(2);
  if (use_timeout) {
    while (TRUE) {
      retcode = pthread_mutex_timedlock(&mutex, &timeout);
      if (retcode == 0) {
        break;
      } else if (retcode == ETIMEDOUT) {
        printf("timed out in child %d\n", my_tidx);
        sleep(1);
      } else {
        handle_thread_error(retcode, "child failed timed lock", PROCESS_EXIT);
      }
    }
  } else {
    retcode = pthread_mutex_lock(&mutex);
    handle_thread_error(retcode, "child failed lock", PROCESS_EXIT);
  }
  printf("child %d got mutex\n", my_tidx);
  char line[1025];
  fgets(line, 1024, stdin);
  printf("[%d]: %s\n", my_tidx, line);
  sleep(1);
  printf("child %d releases mutex\n", my_tidx);
  pthread_mutex_unlock(&mutex);
  printf("child %d released mutex\n", my_tidx);
  sleep(10);
  printf("child %d exiting\n", my_tidx);
  return NULL;
}
开发者ID:bk1,项目名称:sysprogramming-examples,代码行数:35,代码来源:mutex-many-threads-read-stdin.c

示例14: timedlock

  bool timedlock(int64_t milliseconds) const {
#if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 200112L
    PROFILE_MUTEX_START_LOCK();

    struct timespec ts;
    Util::toTimespec(ts, milliseconds + Util::currentTime());
    int ret = pthread_mutex_timedlock(&pthread_mutex_, &ts);
    if (ret == 0) {
      PROFILE_MUTEX_LOCKED();
      return true;
    }

    PROFILE_MUTEX_NOT_LOCKED();
    return false;
#else
    /* Otherwise follow solution used by Mono for Android */
    struct timespec sleepytime, now, to;

    /* This is just to avoid a completely busy wait */
    sleepytime.tv_sec = 0;
    sleepytime.tv_nsec = 10000000L; /* 10ms */

    Util::toTimespec(to, milliseconds + Util::currentTime());

    while ((trylock()) == false) {
      Util::toTimespec(now, Util::currentTime());
      if (now.tv_sec >= to.tv_sec && now.tv_nsec >= to.tv_nsec) {
        return false;
      }
      nanosleep(&sleepytime, NULL);
    }
 
    return true;
#endif
  }
开发者ID:MisterTea,项目名称:MAMEHub,代码行数:35,代码来源:Mutex.cpp

示例15: f

static void f()
{
	pthread_t td;
	int r;
	pthread_mutexattr_t mtx_a;
	pthread_mutex_t mtx;
	struct timespec ts;

	T(r, pthread_barrier_init(&barrier2, 0, 2));
	T(r, pthread_mutexattr_init(&mtx_a));
	T(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST));
	if (pshared)
		T(r, pthread_mutexattr_setpshared(&mtx_a, PTHREAD_PROCESS_SHARED));
	T(r, pthread_mutex_init(&mtx, &mtx_a));
	T(r, pthread_create(&td, 0, start_lock, &mtx));
	T(r, pthread_detach(td));
	pthread_barrier_wait(&barrier2);
	pthread_barrier_destroy(&barrier2);

	// enough time to ensure that the detached thread is dead
	clock_gettime(CLOCK_REALTIME, &ts);
	ts.tv_nsec += 100*1000*1000;
	if (ts.tv_nsec >= 1000*1000*1000) {
		ts.tv_sec++;
		ts.tv_nsec -= 1000*1000*1000;
	}

	TX(r, pthread_mutex_timedlock(&mtx, &ts), EOWNERDEAD);
}
开发者ID:andrey-gvrd,项目名称:rusl,代码行数:29,代码来源:pthread-robust-detach.c


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