关键部分在线程或主程序中更好?

发布于 2024-10-17 14:59:06 字数 138 浏览 6 评论 0原文

我过去常常在访问共享数据时使用临界区(在 C++ 中)来阻止 thead 执行,但是要工作它们必须等到数据不被使用后再阻塞,也许最好在主线程或线程中使用它们。 那么,如果我希望我的主程序具有优先级并且不被阻塞,我是否必须使用其中的临界区来阻塞其他线程,或者相反?

I use to use critical section (in c++) to block theads execution whilel accessing shared data, but as to work them must need to wait until data is not used before blocking, maybe it's better to use them in main or thread.
Then if I want my main program to have priority and not be blocked must I use critical sections inside it to block other thread or the contrary ?

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

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

发布评论

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

评论(2

枫林﹌晚霞¤ 2024-10-24 14:59:06

您似乎对关键部分是什么以及它们如何工作有相当的误解。

一般来说,临界区 (CS) 是一段需要“独占”运行的代码——即,您需要确保在任何给定时间只有一个线程正在执行该代码段。

由于该术语在大多数环境中使用,CS 实际上是互斥体——互斥信号量(也称为二进制信号量)。它是一种数据结构(和一组函数),用于确保一段代码以独占方式执行(而不是引用代码本身)。

无论如何,只有当/如果您有一些代码将在多个线程中执行,并且您需要确保它在任何给定时间仅在一个线程中执行时,CS 才有意义。这通常是当您有一些共享数据时,如果多个线程同时尝试操作这些数据,这些数据可能会被损坏。当/如果出现这种情况,您需要为操作该数据的每个线程“使用”关键部分,以确保共享数据不被损坏。

确保特定线程保持响应是一个完全独立的问题。在大多数情况下,这意味着使用队列(作为一种可能性)允许线程将任务快速“移交”给其他线程,并且争用最少(即,在处理数据期间不使用 CS, CS 仅持续足够长的时间以将数据结构放入队列中,并且其他一些线程从那里进行处理)。

You seem to have rather a misconception over what critical sections are and how they work.

Speaking generically, a critical section (CS) is a piece of code that needs to run "exclusively" -- i.e., you need to ensure that only one thread is executing that piece of code at any given time.

As the term is used in most environments, a CS is really a mutex -- a mutual exclusion semaphore (aka binary semaphore). It's a data structure (and set of functions) you use to ensure that a section of code gets executed exclusively (rather than referring to the code itself).

In any case, a CS only makes sense at all when/if you have some code that will execute in more than one thread, and you need to ensure that it only ever executes in one thread at any given time. This is typically when you have some shared data that could and would be corrupted if more than one thread tried to manipulate it at one time. When/if that arises, you need to "use" the critical section for every thread that manipulates that data to assure that the shared data isn't corrupted.

Assuring that a particular thread remains responsive is a whole separate question. In most cases, this means using a queue (for one possibility) to allow the thread to "hand off" a task to some other thread quickly, with minimal contention (i.e., instead of using a CS for the duration of processing the data, the CS only lasts long enough to put a data structure into a queue, and some other thread takes the processing from there).

兔小萌 2024-10-24 14:59:06

你不能说“我在线程 A 中使用临界区,但不在线程 B 中”。关键部分是访问共享资源的一段代码。当从并行运行的两个线程执行此代码时,共享资源可能会损坏,因此您需要同步对其的访问:您需要使用一些同步对象(互斥体、信号量、事件...取决于平台和您正在使用的 API)。 ThreadA 锁定了临界区,因此 ThreadB 需要等待 ThreadA 释放它。

如果您希望主线程阻塞(等待)少于工作线程,请将工作线程优先级设置为低于主线程的优先级。

You cannot say "I am using critical section in thread A but not in thread B". Critical section is a piece of code that accesses shared resource. When this code is executed from two threads that run in parallel, shared resource might get corrupted so therefore you need to synchronise access to it: you need to use some of synchronisation objects (mutexes, semaphores, events...depending on the platform and API you are using). ThreadA locks the critical section so ThreadB needs to wait till ThreadA releases it.

If you want your main thread to block (wait) less than working thread, set working thread priority to be lower than priority of the main thread.

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