如何设置优先获得在C / C互斥++ [英] how to set the priority to get the mutex in C/c++

查看:131
本文介绍了如何设置优先获得在C / C互斥++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个过程(相同的优先级)

I have 3 process (equal priority)


  1. P1

  2. P2

  3. P3(定时器)

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

如果假设 P3 来,并得到互斥
那么 P2 来,等待互斥
之后P1来得快,也等待互斥

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

如果 P3 释放互斥然后 P1 应该得到互斥不是 P2

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

如何用C或C ++,执行此

How to perform this in C or C++.

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

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

操作系统 - 视窗XP

OS - windows Xp

推荐答案

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

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.

编辑:
通过使用pthreads的接口(C式)的条件变量的一个例子:
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.

这篇关于如何设置优先获得在C / C互斥++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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