English 中文(简体)
如何使工人准备等待工作?
原标题:What is most CPU efficient method to make the worker threads wait for tasks?

在我目前的C#/NET3.5申请中,我有一个任务点(已经安全),我有5个工作线,必须经常在座。 如果有任务,任何一名工人都将解职,并采取必要行动。

我的工人阅读班如下:

public class WorkerThread
{
    //ConcurrentQueue is my implementation of thread safe queue
    //Essentially just a wrapper around Queue<T> with synchronization locks
    readonly ConcurrentQueue<CheckPrimeTask> mQ; 
    readonly Thread mWorker;
    bool mStop;

    public WorkerThread (ConcurrentQueue<CheckPrimeTask> aQ) {
        mQ = aQ;
        mWorker = new Thread (Work) {IsBackground = true};
        mStop = false;
    }

    private void Work () {
        while (!mStop) {
            if (mQ.Count == 0) {
                Thread.Sleep (0);
                continue;
            }

            var task = mQ.Dequeue ();
            //Someone else might have been lucky in stealing
            //the task by the time we dequeued it!!
            if (task == null) 
                continue;

            task.IsPrime = IsPrime (task.Number);
            task.ExecutedBy = Thread.CurrentThread.ManagedThreadId;
            //Ask the threadpool to execute the task callback to 
            //notify completion
            ThreadPool.QueueUserWorkItem (task.CallBack, task);
        }
    }

    private bool IsPrime (int number) {
        int limit = Convert.ToInt32 (Math.Sqrt (number));
        for (int i = 2; i <= limit; i++) {
            if (number % i == 0)
                return false;
        }

        return true;
    }

    public void Start () {
        mStop = false;
        mWorker.Start ();
    }

    public void Stop () {
        mStop = true;
    }
}

问题是,当问题空洞时,它消耗的钱太大(将近98%)。 我试图向工人通报说,已经改变了解雇。 因此,他们有效地等待发出这一信号。 它把万国邮联缩减到将近0%,但我并不完全相信这是最佳方法。 您能否提出一种更好的方法,在不伤害万国邮联的使用的情况下,保持read子的闲 the?

最佳回答
问题回答

页: 1 没有定点的工程方法。 将这一改动改为任何thing,超过0,贵邮联的使用将减少。 Try 10

你们有我可以想象的几种选择。

一种办法是在你 lo期间安排一个小的睡觉。 这基本上将万国邮联的利用率降至零,并且是这样做的相当标准的方式。

另一种方式是采用Mitch Wheat在评论中建议的重新分类(汽车或手工)。

你们还可以设计某种IdleTask,在一定时间内可以睡觉,如果你是空洞的,那么伊塔克(会睡觉)。

如果贵方能安全,你就不需要这样做。

    //Someone else might have been lucky in stealing 
    //the task by the time we dequeued it!! 
    if (task == null)  
        continue; 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签