如何在C/c++中设置获取互斥锁的优先级

发布于 2024-11-10 04:06:47 字数 386 浏览 3 评论 0原文

我有3个进程(同等优先级)

  1. P1
  2. P2
  3. P3(定时器)

获取互斥锁的优先级如下:P1(1优先级),P2(2优先级),P3(定时器)(3优先级)

如果假设 p3 来并获取互斥锁 然后p2来并等待互斥体 之后 p1 到来,它也会等待互斥体,

如果 p3 释放互斥体,那么 p1 应该获得互斥体而不是 p2

如何在 C 或 C++ 中执行此操作。

注意:所有进程都在具有相同优先级的线程内运行。

操作系统-windows XP

I have 3 process (equal priority)

  1. P1
  2. P2
  3. P3(timer)

priority to get the mutex is as follows: P1(1 priority), P2(2 priority), P3(timer)(3 priority)

If suppose p3 comes and get the mutex
then p2 comes and wait for mutex
after that p1 comes and it also wait for mutex

if p3 release mutex then p1 should get the mutex not p2.

How to perform this in C or C++.

Note : all processes are running inside threads having same priority.

OS - windows Xp

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

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

发布评论

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

评论(4

不忘初心 2024-11-17 04:06:47

由于线程具有相同的优先级,因此哪个线程获得锁将是相当任意的。看来您想要等待条件变量而不是使用简单的互斥体。你仍然会有一个互斥锁;条件变量是互斥锁之上的一个概念。另一种可能性是使用同步屏障。

编辑:
使用 pthreads 接口(C 风格)使用条件变量的示例:
https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal

一个重要的你需要问自己一个问题:经过所有这些等待和同步,你买东西了吗?使用线程的目的是让一些事情并行运行。如果没有发生这种情况,则多线程应用程序的运行速度比应用程序根本不使用线程时要慢。

Since the threads have equal priority, which thread gets the lock will be rather arbitrary. It appears you want to wait on a condition variable rather than using a simple mutex. You will still have a mutex; condition variables are a concept on top of mutexes. Another possibility is to use synchronization barriers.

EDIT:
An example of using condition variables using the pthreads interface (C-style):
https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal

An important question you need to ask yourself: With all of this waiting and synchronization, are you buying anything? The purpose of using threads is to let some things run in parallel. If that is not happening, you have a multithreaded application that runs slower than if the application didn't use threads at all.

小女人ら 2024-11-17 04:06:47
SetThreadPriority(
   HANDLE hThread,
   int nPriority
);

这个函数将设置你的线程的优先级......创建线程时你将获得的HANDLE值..就像

:HANDLE hf=_beginthred(abc,0,NULL) 
SetThreadPriority(
   HANDLE hThread,
   int nPriority
);

this function will set the priority of your threads .... the HANDLE value you will get while creating thread.. like

:HANDLE hf=_beginthred(abc,0,NULL) 
情话墙 2024-11-17 04:06:47

基于优先级进行锁定获取会导致死锁锁定应始终以可预测的顺序完成,否则将出现经典的 (A,B)、(B,A) 死锁可能性

相反,您将希望使用优先级队列并让锁本身由内核管理。当然,您可以使用信号量而不是互斥锁来让内核知道有多少等待线程在排队的项目上唤醒。但是,您仍然希望在访问队列时锁定队列本身

Making lock acquisition based on priority is a recipe for deadlocks. Locking should always be done in predictable order, or you will have the classical (A,B), (B,A) deadlock possibility.

You will instead want to work with a priority queue and let the lock itself be kernel managed. You could of course use a semaphore instead of a mutex to let the kernel know how many waiting threads to awake on queued items. However, you'll still want to lock the queue itself when accessing it

鲜血染红嫁衣 2024-11-17 04:06:47

当您等待互斥体时,进程的线程将添加到互斥体等待队列(链接列表)中,并且您唯一的机会是可以更改队列中的选择行为。也许 Windows 提供了这种可能性,或者队列默认按优先级排序(这是最有可能的)。

事实上,您的进程具有相同的优先级并不是问题,因为线程时间片将是进程线程优先级的函数。

When you wait on a mutex, the process's thread is added in the mutex waiting queue (a linked list) and your only chance would be to have the possibility to change the selection behavior in the queue. Maybe Windows offers this possibility or maybe the queue is by default sorted by priority (which is the most likely).

The fact that your processes have the same priority is not a problem since a thread timeslice will be function of the process and thread priority.

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