linux怎么管理线程,Linux 线程管理

while作用:因为可能有多个线程等待,所以 pthread_cond_wait 返回时,假如A线程会先获取mut,此时其他线程变阻塞,如果A修改了x,y使x<=y则其他线程就没必要再执行了,所以要加while判断。

和:       pthread_mutex_lock(&mut);

if (x > y)

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mut);

其实函数的执行过程非常简单,在第一个线程执行到pthread_cond_wait(&cond,&mut)时,此时如果X<=Y,则此函数就将mut互斥量解锁 ,再将cond条件变量加锁 ,此时第一个线程挂起 (不占用任何CPU周期)。

而在第二个线程中,本来因为mut被第一个线程锁住而阻塞,此时因为mut已经释放,所以可以获得锁mut,并且进行修改X和Y的值,在修改之后,一个IF语句判定是不是X>Y,如果是,则此时pthread_cond_signal()函数会唤醒第一个线程 ,并在下一句中释放互斥量mut。然后第一个线程开始从pthread_cond_wait()执行,首先要再次锁mut ,

如果锁成功,再进行条件的判断 (至于为什么用WHILE,即在被唤醒之后还要再判断,后面有原因分析),如果满足条件,则被唤醒 进行处理,最后释放互斥量mut 。

至于为什么在被唤醒之后还要再次进行条件判断(即为什么要使用while循环来判断条件),是因为可能有“惊群效应”。有人觉得此处既然是被唤醒的,肯定

是满足条件了,其实不然。如果是多个线程都在等待这个条件,而同时只能有一个线程进行处理,此时就必须要再次条件判断,以使只有一个线程进入临界区处理。

对此,转来一段:

引用下POSIX的RATIONALE:

Condition Wait Semantics

It is important to note that when pthread_cond_wait() and

pthread_cond_timedwait() return without error, the associated

predicate may still be false. Similarly, when

pthread_cond_timedwait() returns with the timeout error, the

associated predicate may be true due to an unavoidable race between

the expiration of the timeout and the predicate state

change.

The application needs to recheck the predicate on any return

because it cannot be sure there is another thread waiting on the

thread to handle the signal, and if there is not then the signal is

lost. The burden is on the application to check the

predicate.

Some implementations, particularly on a multi-processor, may

sometimes cause multiple threads to wake up when the condition

variable is signaled simultaneously on different

processors.

In general, whenever a condition wait returns, the thread has to

re-evaluate the predicate associated with the condition wait to

determine whether it can safely proceed, should wait again, or

should declare a timeout. A return from the wait does not imply

that the associated predicate is either true or

false.

It is thus recommended that a condition wait be enclosed in the

equivalent of a "while loop" that checks the

predicate.

从上文可以看出:

1,pthread_cond_signal在多处理器上可能同时唤醒多个线程,当你只能让一个线程处理某个任务时,其它被唤醒的线程就需要继续

wait,while循环的意义就体现在这里了,而且规范要求pthread_cond_signal至少唤醒一个pthread_cond_wait上

的线程,其实有些实现为了简单在单处理器上也会唤醒多个线程.

2,某些应用,如线程池,pthread_cond_broadcast唤醒全部线程,但我们通常只需要一部分线程去做执行任务,所以其它的线程需要继续wait.所以强烈推荐此处使用while循环.

其实说白了很简单,就是pthread_cond_signal()也可能唤醒多个线程,而如果你同时只允许一个线程访问的话,就必须要使用while来进行条件判断,以保证临界区内只有一个线程在处理。

解析2

2.使用条件变量的线程同步(推荐)

采用阻塞和消息方式可以极大程度上减少资源的浪费以及增加实时性

线程条件变量pthread_cond_t

线程等待某个条件

int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime);

int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);

通知函数

通知所有的线程

int pthread_cond_broadcast(pthread_cond_t *cond);

只通知一个线程

int pthread_cond_signal(pthread_cond_t *cond);

---------------------------------------------------------------------------------

正确的使用方法

pthread_cond_wait用法:

pthread_mutex_lock(&mutex);

while(condition_is_false)

{

pthread_cond_wait(&cond,&mutex);

}

condition_is_false=true;  //此操作是带锁的,也就是说只有一个线程同时进入这块

pthread_mutex_unlock(&mutex);

----------------------------------------------------

pthread_cond_signal用法:

pthread_mutex_lock(&mutex);

condition_is_false=false;

pthread_cond_signal(&cond)

pthread_mutex_unlock(&mutex)

--------------------------------------------------------

记住上面这种用法!!!可以避免误用pthread_cond_broadcast而释放了所有条件变量

参考 http://blog.csdn.net/mashang123456789/article/details/9792677

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值