什么是关键部分? [英] What exactly is a critical section?

查看:225
本文介绍了什么是关键部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想清楚一点。
想象一下,我使用EnterCriticalSection的windows api。我用 EnterCriticalSection(& criticalsection);


调用所有的这些线程这是多线程的线程函数

  void thread(){

//输入关键部分(第1部分)
data
// leave critical section
/// more data 1
// entercritical section(part 2)
//更多数据2
// leave critical section

}

一旦线程进入临界(第1部分)线程无法输入该部分,无论更多的数据1实际上是否有任何共享数据或不正确?此外,其他线程也不能进入临界区的第2部分。

解决方案

Critical部分是一个代码块。如果任何线程输入它,没有其他线程可以进入,直到它是免费的。如果1和2是不同的关键部分(即由不同的信号量处理),如果1被占用,有人可以输入2。


Just want a little clarity on the this. Imagine I use the windows api of EnterCriticalSection. I call all of them with EnterCriticalSection(&criticalsection);

This is the thread function that is multi threaded

void thread (){

//enter critical section  (part 1)
data
//leave critical section
///more data 1
//entercritical section  (part 2)
//more data 2
//leave critical section 

}

Once a thread enters the critical (part 1), other threads cannot enter that section regardless of whether more data 1 actually has any shared data or not right? Also during that time other threads also cannot enter part 2 of the critical section either.

解决方案

Critical section is a code chunk. If any thread entered it, no other thread can enter until it's free. If 1 and 2 are different critical sections (i.e. handled by a different semaphore), someone can enter 2 if 1 is occupied.

这篇关于什么是关键部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆