然而,如果互斥对象已锁定,这个调用也不会阻塞。
如果互斥锁被持有(锁定),那么就会返回1;否则,返回0。
If the mutex is held (locked), then one is returned; otherwise, zero.
调用 pthread_mutex_trylock() 时将尝试锁定互斥对象。
When you call pthread_mutex_trylock() you'll attempt to lock the mutex.
一旦 pthread_cond_wait()锁定了互斥对象,那么它将返回并允许1 号线程继续执行。
Once pthread_cond_wait() has the lock, it will then return and allow thread 1 to continue execution.
如果线程2要运行,它就要锁定count _ mutex,而这个互斥量已经被线程1占有了。
If thread two runs, it'll want to lock count_mutex, which is held by thread one.
如果线程1要运行,它就要锁定rand _ mutex,可是这个互斥量已经被线程2阻塞了。
If thread one runs, it'll want to lock rand_mutex, which is held by thread two.
如果可能,在设计程序时决不要锁定一个已经锁定的互斥量。
If possible, just design your program never to re-lock a mutex it already has.
如果可能,在设计程序时决不要锁定一个已经锁定的互斥量。
If possible, just design your program never to re-lock a mutex it already has.
应用推荐