我试图在一个小的C++程序上激发优先级倒置以进行演示,但我不能:持有互斥锁的低优先级线程没有被抢占并继续在关键部分运行.这就是我正在做的事情:
// let's declare a global mutex
pthread_mutex_t my_mutex;
...
int main(int argc, char **argv) {
...
pthread_t normal_thread;
pthread_t prio_thread;
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_NONE); // ! None !
pthread_mutex_init(&my_mutex, &attr);
// create first normal thread (L):
pthread_create(&normal_thread, NULL, the_locking_start_routine, NULL);
// just to help the normal thread enter in the critical section
sleep(2);
// now will launch:
// * (M) several CPU intensive SCHED_FIFO threads with priority < 99
// …Run Code Online (Sandbox Code Playgroud) 我看了一下Lua的书,并了解到Lua中的多线程是合作的.我找不到的是有关线程优先级的一些信息.我猜想具有相同优先级的线程会一直运行直到完成,因为多线程是合作的,或者是完成了收益.那个优先级高于另一个优先级的线程怎么样?
是否能够中断优先级较低的优先级,或者当优先级较低的线程运行到完成时它是否会运行?
据我所知,Thread.Sleep(0)只为具有相同或更高优先级的另一个线程提供时间片.我做了一些测试应用:
class Program
{
public static void ThreadMethod()
{
for (int i = 0; i < 300; i++)
{
Thread.Sleep(0);
Console.WriteLine("{0} ThreadProc: {1}, prio {2} ", Thread.CurrentThread.ManagedThreadId,
i, Thread.CurrentThread.Priority);
}
}
public static void Main()
{
Thread.CurrentThread.Priority = ThreadPriority.Normal;
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Priority = ThreadPriority.Highest;
Thread t2 = new Thread(new ThreadStart(ThreadMethod));
t2.Priority = ThreadPriority.Lowest;
Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)1;
t.Start();
t2.Start();
t2.Join();
t.Join();
}
}
Run Code Online (Sandbox Code Playgroud)
这是模拟单核CPU,据我所知这里应该只执行主要具有最高优先级的线程,但我看到:低prio线程不断更改高prio每个操作 
线程互相改变.(当然在开始和结束时有不同的情况,但最高线程应该总是得到更多的时间片)
我使用Indy 10的TIdCmdTCPServer组件来实现我的客户端 - 服务器应用程序.问题是来自客户端的请求可能相当复杂,并且我的服务器应用程序的GUI部分立即失去响应能力.从Indy文档中,我得到了Indy:
使用线程优先级tpHighest为Bindings创建和启动侦听器线程.
我可以改变这种行为吗?
我希望我的 Java 程序降低一些优先级,这样它就不会压垮系统。我最初的想法是使用Thread.currentThread().setPriority(5),但这似乎只是它在 JVM 中的优先级。
然后我想也许我会抓住它并调用系统命令,但Thread.getId()也只是 JVM 的 id,所以我什至不知道要传递给什么进程 id renice。
Java程序有办法做这样的事情吗?
我试图通过让一个线程写入链表而另一个线程处理链表来加快速度.
出于某种原因,如果写入链表的方法我将其作为一个任务,并且从链表中读取一个低优先级线程的方法,程序整体上完成的速度要快得多.换句话说,我在做的时候经历了紧固结果:
Task.Factory.StartNew( AddItems );
new Thread( startProcessingItems ) { Priority = ThreadPriority.Lowest }.Start();
while(completed==false)
Thread.Sleep(0);
Run Code Online (Sandbox Code Playgroud)
也许是因为第一个任务是做了比其他线程更多的额外工作,这就是为什么如果我将第二个方法设置为低优先级,整个事情将会更快完成.
无论如何,现在我的问题是startProcessingItems ThreadPriority =最低的运行.我怎么能把它的优先级改为最高?如果我在该方法中创建一个新任务,它将以低优先级运行吗?基本上,startProcessingItems以列表结束,一旦它具有该列表,我将开始以最高优先级执行.
我需要编写一个类,它将附加到某些Windows进程,监视和限制其CPU使用率.流程优先级的改变对我没有帮助,所以我真的需要编写一个与BES或ThreadMaster基本类似的程序.所以我需要创建一个类似这样的类(伪代码):
public void cpuLimiter(pid)
{
ProcessHandle handle = attachToProcess(pid);
while (cpuLimiting)
{
if (handle.cpuUsage > 30%)
{
handle.sleep(100miliseconds);
}
sleep(10miliseconds);
}
closeHandle(pid);
}
Run Code Online (Sandbox Code Playgroud)
我希望我说清楚我想要完成什么,只是我不知道如何.感谢每一位帮助.
我知道线程可以具有从MIN_PRIORITY(1)到MAX_PRIORITY(10)的优先级.但是,如果我要执行10个以上的线程,我将如何为它们分配优先级?优先级可以超过10吗?