gpt4 book ai didi

c# - 4 核 PC 上的 c# 程序中的线程激活顺序

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:18 26 4
gpt4 key购买 nike

我对以下 C# 代码的线程激活顺序感到困惑。它创建了 10 个线程,随机启动它们,每个线程模拟执行一个耗时的工作 10 次,如果你检查调试输出,线程似乎不是随机选择的,请看下面的输出示例,注意线程 #3 ,#5,#6一直被拾起,当#3#5#6完成后,#10#2#8总是被拾起,等等...(我知道设计不好,请关注现象)

我的电脑有 i7-7820HQ CPU,有 4 个内核,运行的是 Windows 10。

有人可以解释为什么这些线程不是随机选择的,而是它们似乎以某种方式分组。

非常感谢!

----调试输出----

Thread #10 acquired the lock and is working for task #0 Thread #5 acquired the lock and is working for task #0 Thread #3 acquired the lock and is working for task #0Thread #6 acquired the lock and is working for task #0Thread #5 acquired the lock and is working for task #1Thread #3 acquired the lock and is working for task #1Thread #6 acquired the lock and is working for task #1...Thread #5 acquired the lock and is working for task #9Thread #3 acquired the lock and is working for task #9Thread #6 acquired the lock and is working for task #9...Thread #8 acquired the lock and is working for task #0Thread #2 acquired the lock and is working for task #0Thread #8 acquired the lock and is working for task #1Thread #2 acquired the lock and is working for task #1Thread #10 acquired the lock and is working for task #1...Thread #8 acquired the lock and is working for task #9Thread #2 acquired the lock and is working for task #9Thread #10 acquired the lock and is working for task #9...
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
static Hashtable _sharedBetweenThreads = new Hashtable();

static void Main(string[] args)
{
Random random = new Random(DateTime.Now.Second);
var startOrders = new int[10];
for (int i = 0; i < startOrders.Length; i++)
{
startOrders[i] = i;
}

//shuffle the array
for (int i = startOrders.Length - 1; i >= 0; i--)
{
int j = random.Next(0, i);
int temp = startOrders[i];
startOrders[i] = startOrders[j];
startOrders[j] = temp;
}


Thread[] threads = new Thread[startOrders.Length];
for(int i = 0; i < startOrders.Length; i++)
{
threads[i] = new Thread(new ThreadStart(ThreadProc));
}


for (int i = 0; i < startOrders.Length; i++)
{
threads[startOrders[i]].Start();
}

Console.ReadLine();
}

static void ThreadProc()
{
// simulates there are 10 tasks needs to do.
for (int i = 0; i < 10; i++)
{
lock (_sharedBetweenThreads.SyncRoot)
{
Debug.Print(string.Format("Thread #{0} acquired the lock and is working for task #{1}", Thread.CurrentThread.ManagedThreadId, i));

// simulates a work.
Thread.Sleep(500);
}

}
}
}
}

The screenshot of debug output

--- 附加信息 ---

  • 该程序根本不是并行程序,因为有 10 个线程共享锁。
  • 如果循环次数从10变成无穷大,3个线程一直处于激活状态,其余7个线程没有机会激活,就永远“死锁”了。

最佳答案

我想您的困惑源于您正在使用 .NET 线程池这一事实 - 例如一个托管线程池——但您希望在系统级别处理线程,这并不完全相同。内部线程池给你它认为合适的线程。这与那个时刻哪个内核运行哪个线程的问题无关。

关于c# - 4 核 PC 上的 c# 程序中的线程激活顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47711938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com