临界区到底是什么?

发布于 2024-10-31 12:10:42 字数 435 浏览 1 评论 0原文

只是想澄清一下这一点。 想象一下我使用 EnterCriticalSection 的 Windows api。我用 EnterCriticalSection(&riticsection); 来调用所有这些,

这是多线程的线程函数

void thread (){

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

}

一旦一个线程进入关键部分(第 1 部分),其他线程就无法进入该部分,无论是否有更多线程data 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2024-11-07 12:10:42

关键部分是一个代码块。如果任何线程进入它,则在它空闲之前没有其他线程可以进入。如果1和2是不同的临界区(即由不同的信号量处理),如果1被占用,有人可以输入2。

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.

抱猫软卧 2024-11-07 12:10:42

规则很简单:只有一个线程可以执行特定关键部分内的代码(在同一实例上调用 EnterCriticalSectionLeaveCriticalSection 之间执行的任何代码部分)。从操作系统的角度来看,诸如代码部分、函数之类的东西在这里是无关紧要的。唯一重要的是对上述例程的调用次数。每当出现某个线程在特定临界区对象上调用 EnterCriticalSection 的次数多于 LeaveCriticalSection 的情况时,就称其“位于该临界区内部”。

也就是说,您可以创建多个关键部分,并且它们是独立执行的。因此一个关键部分永远不会影响另一个关键部分。不同的临界区是通过单独调用构造函数来创建的。

The rule is simple: only one thread can execute code inside a particular critical section (any portion of code executed between calls to EnterCriticalSection and LeaveCriticalSection on the same instance). From the point of view of the operating system things like portions of code, functions are irrelevant here. The only thing that matters is the number of calls to the mentioned routines. Whenever a situation happens that some thread called EnterCriticalSection more times than LeaveCriticalSection on a particular critical section object, it is said to be "inside that critical section".

That said you can have multiple critical sections created and they are enforced independently. So one critical section is never affecting another critical section. Different critical sections are created using separate calls to the constructor.

瑶笙 2024-11-07 12:10:42

看这个:

考虑一个变量,

int k

两个线程都用这个语句对 k 进行操作

k+=100;

现在假设 k 等于 0。第一个线程开始读取 k,找到 k=0,然后将 k 加 100。然后第二个线程开始读取之前的 k第一个线程写回 k=100。然后第二个线程将假定 k=0 并将其添加 100,最后在两个线程加入 k=100 后,k=100 而不是预期的 200。这就是我们将 k+=100 设置为关键部分的原因。

See this:

Consider a variable

int k

two threads are operating on k with this statement

k+=100;

Now assume k equals to 0. The first thread starts to read k, find k=0, then add k by 100. Then the second thread starts to read k before the 1st thread write k=100 back. Then the second thread will assume k=0 and add it by 100 and finally after two threads join k=100 not expected 200. This is the reason we set k+=100 a critical section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文