创建线程时的NullReferenceException [英] NullReferenceException when creating a thread

查看:119
本文介绍了创建线程时的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看着这线程创建一个简单的线程池。

$ B:在那里,我遇到 @ MilanGardian对.NET 3.5 回应这是优雅,并担任我的目的就
$ b

 使用系统; 
使用System.Collections.Generic;
使用的System.Threading;

命名空间SimpleThreadPool
{
公共密封类游泳池:IDisposable的
{
邪乎(INT大小)
{
本._workers =新的LinkedList<螺纹>();
为(VAR I = 0; I<大小; ++ I)
{
变种工人=新主题(this.Worker){名称= string.Concat(工人,一世) };
worker.Start();
this._workers.AddLast(工人);
}
}

公共无效的Dispose()
{
VAR waitForThreads = FALSE;
锁(this._tasks)
{
如果(this._disposed!)
{
GC.SuppressFinalize(本);

this._disallowAdd = TRUE; //等待所有任务完成处理,而不是让更多的新任务
,而(this._tasks.Count大于0)
{
Monitor.Wait(this._tasks);
}

this._disposed = TRUE;
Monitor.PulseAll(this._tasks); //唤醒所有工人(他们都将在这一点上活跃;设置标志将导致然后来完成,这样我们就可以加入他们的行列)
waitForThreads = TRUE;
}
}
如果(waitForThreads)
{
的foreach(VAR在this._workers工人)
{
worker.Join() ;
}
}
}

公共无效QueueTask(行动任务)
{
锁(this._tasks)
{
如果(this._disallowAdd){抛出新的InvalidOperationException异常(这池实例是在布置,不能再增加的过程); }
如果(this._disposed){抛出(这池实例已被释放)新的ObjectDisposedException; }
this._tasks.AddLast(任务);
Monitor.PulseAll(this._tasks); //因为脉冲计数任务改为
}
}

私人无效工人()
{
行动任务= NULL;
,而(真)//循环,直到线程池设
{
锁(this._tasks)//找到一个任务需要是原子
{
,而(真)//等待轮到我们在_workers队列和可用的任务
{
如果(this._disposed)
{
的回报;
}
如果(零= this._workers.First&放大器;!&放大器; object.ReferenceEquals(Thread.CurrentThread,this._workers.First.Value)及&放大器; this._tasks.Count> 0)//我们只能声明一个任务如果轮到我们了(这工作者线程处于_worker队列中的第一项),并有一个任务可以
{
=任务this._tasks.First.Value ;
this._tasks.RemoveFirst();
this._workers.RemoveFirst();
Monitor.PulseAll(this._tasks); //脉冲电流,因为(一)劳动者改变(让下一个可用的工人睡会拿起它的任务)
中断; //我们发现了一个任务来处理,从上面的突破,而(真)循环
}
Monitor.Wait(this._tasks); //去睡觉,要么不轮到我们甚至没有任务来处理
}
}

任务(); //过程中发现任务
this._workers.AddLast(Thread.CurrentThread);
任务= NULL;
}
}

私人只读的LinkedList<螺纹> _工人; //工作者线程队列准备好处理动作
私人只读LinkedList的<作用> _tasks =新的LinkedList<作用>(); //行动由工作线程
私人布尔_disallowAdd进行处理; //处理队列时设置为true但仍存在悬而未决
私人布尔_disposed任务; //处理队列时设置为true,并没有更多的任务正在等待
}


公共静态类节目
{
静态无效的主要()
{
使用(VAR池=新池(5))
{
变种随机=新的随机();
动作< INT>随机数发生器=(指数=>
{
Console.WriteLine({0}:索引工作{1},Thread.CurrentThread.Name,指数);
Thread.sleep代码( random.Next(20,400));
Console.WriteLine({0}结束{1},Thread.CurrentThread.Name,指数);
});

为(VAR I = 0; I< 40; ++ I)
{
变种I1 = I;
pool.QueueTask(()=>随机化器(I1));
}
}
}
}
}

我使用这个如下:

 静态无效的主要(字串[] args)
{
...

在(keepRunning)
{
...
pool.QueueTask(()=> DoTask(eventObject)传递;
}

}

私有静态无效DoTask(的EventObject E)
{
//做一些计算

pool.QueueTask(()=> DoAnotherTask(eventObject)传递); //这是一个相对较小的计算
}
 <$ C $:p> 

我运行的代码,两天后得到以下异常C>未处理的异常:System.NullReferenceException:对象不设置到对象的实例
在System.Collections.Generic.LinkedList`1.InternalInsertNodeBefore(LinkedListNode`1节点,LinkedListNode`1 newNode)$ b $。 b。在System.Collections.Generic.LinkedList`1.AddLast(T值)
在MyProg.Pool.Worker()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
。在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()

我无法弄清楚是什么原因造成这是我无法再得到这个错误。如何解决这个问题有什么建议?


解决方案

好像进入 _workers 链表未正确同步。考虑这种情况:



让我们假设,在某些时候 this._workets 列表中包含了一个项目



首先线程调用 this._workers.AddLast(Thread.CurrentThread); ,但在一个非常特殊的地方被中断 - 在 addlast仅()方法:

 公共无效addlast仅(一个LinkedListNode< T>节点) 
{
this.ValidateNewNode(节点);
如果(this.head == NULL)
{
this.InternalInsertNodeToEmptyList(节点);
}
,否则
{
//这里我们被中断 - 这样的名单不为空,
//但它会很快被和this.head变空
// InternalInsertNodeBefore()没有想到,
this.InternalInsertNodeBefore(this.head,节点);
}
node.list =(LinkedList的< T>)这一点;
}

其他线程调用 this._workers.RemoveFirst() ; 。没有锁()周围的语句,以便它完成,现在列表是空的。 addlast仅()现在应该呼叫 InternalInsertNodeToEmptyList(节点); ,但它不能作为条件已经评价



把一个简单的锁(this._tasks)单左右 this._workers.AddLast( )行应该防止这样的场景。



其他不良情况包括添加项目到同一个列表,由两个线程同时进行。


I was looking at this thread on creating a simple thread pool. There, I came across @MilanGardian's response for .NET 3.5 which was elegant and served my purpose:

using System;
using System.Collections.Generic;
using System.Threading;

namespace SimpleThreadPool
{
    public sealed class Pool : IDisposable
    {
        public Pool(int size)
        {
            this._workers = new LinkedList<Thread>();
            for (var i = 0; i < size; ++i)
            {
                var worker = new Thread(this.Worker) { Name = string.Concat("Worker ", i) };
                worker.Start();
                this._workers.AddLast(worker);
            }
        }

        public void Dispose()
        {
            var waitForThreads = false;
            lock (this._tasks)
            {
                if (!this._disposed)
                {
                    GC.SuppressFinalize(this);

                    this._disallowAdd = true; // wait for all tasks to finish processing while not allowing any more new tasks
                    while (this._tasks.Count > 0)
                    {
                        Monitor.Wait(this._tasks);
                    }

                    this._disposed = true;
                    Monitor.PulseAll(this._tasks); // wake all workers (none of them will be active at this point; disposed flag will cause then to finish so that we can join them)
                    waitForThreads = true;
                }
            }
            if (waitForThreads)
            {
                foreach (var worker in this._workers)
                {
                    worker.Join();
                }
            }
        }

        public void QueueTask(Action task)
        {
            lock (this._tasks)
            {
                if (this._disallowAdd) { throw new InvalidOperationException("This Pool instance is in the process of being disposed, can't add anymore"); }
                if (this._disposed) { throw new ObjectDisposedException("This Pool instance has already been disposed"); }
                this._tasks.AddLast(task);
                Monitor.PulseAll(this._tasks); // pulse because tasks count changed
            }
        }

        private void Worker()
        {
            Action task = null;
            while (true) // loop until threadpool is disposed
            {
                lock (this._tasks) // finding a task needs to be atomic
                {
                    while (true) // wait for our turn in _workers queue and an available task
                    {
                        if (this._disposed)
                        {
                            return;
                        }
                        if (null != this._workers.First && object.ReferenceEquals(Thread.CurrentThread, this._workers.First.Value) && this._tasks.Count > 0) // we can only claim a task if its our turn (this worker thread is the first entry in _worker queue) and there is a task available
                        {
                            task = this._tasks.First.Value;
                            this._tasks.RemoveFirst();
                            this._workers.RemoveFirst();
                            Monitor.PulseAll(this._tasks); // pulse because current (First) worker changed (so that next available sleeping worker will pick up its task)
                            break; // we found a task to process, break out from the above 'while (true)' loop
                        }
                        Monitor.Wait(this._tasks); // go to sleep, either not our turn or no task to process
                    }
                }

                task(); // process the found task
                this._workers.AddLast(Thread.CurrentThread);
                task = null;
            }
        }

        private readonly LinkedList<Thread> _workers; // queue of worker threads ready to process actions
        private readonly LinkedList<Action> _tasks = new LinkedList<Action>(); // actions to be processed by worker threads
        private bool _disallowAdd; // set to true when disposing queue but there are still tasks pending
        private bool _disposed; // set to true when disposing queue and no more tasks are pending
    }


    public static class Program
    {
        static void Main()
        {
            using (var pool = new Pool(5))
            {
                var random = new Random();
                Action<int> randomizer = (index =>
                {
                    Console.WriteLine("{0}: Working on index {1}", Thread.CurrentThread.Name, index);
                    Thread.Sleep(random.Next(20, 400));
                    Console.WriteLine("{0}: Ending {1}", Thread.CurrentThread.Name, index);
                });

                for (var i = 0; i < 40; ++i)
                {
                    var i1 = i;
                    pool.QueueTask(() => randomizer(i1));
                }
            }
        }
    }
}

I am using this as follows:

static void Main(string[] args)
{
   ...
   ...
      while(keepRunning)
      {
         ...
         pool.QueueTask(() => DoTask(eventObject);
      }
   ...
}

private static void DoTask(EventObject e)
{
   // Do some computations

   pool.QueueTask(() => DoAnotherTask(eventObject)); // this is a relatively smaller computation
}

I am getting the following exception after running the code for about two days:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Collections.Generic.LinkedList`1.InternalInsertNodeBefore(LinkedListNode`1 node, LinkedListNode`1 newNode)
   at System.Collections.Generic.LinkedList`1.AddLast(T value)
   at MyProg.Pool.Worker()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I am unable to figure out what is causing this as I am unable to get this error again. Any suggestions on how to fix this?

解决方案

Seems like access to _workers linked list is not properly synchronized. Consider this scenario:

Lets assume that at some point this._workets list contains one item.

First thread calls this._workers.AddLast(Thread.CurrentThread); but gets interrupted at a very special place - inside AddLast() method:

public void AddLast(LinkedListNode<T> node)
{
    this.ValidateNewNode(node);
    if (this.head == null)
    {
        this.InternalInsertNodeToEmptyList(node);
    }
    else
    {
        // here we got interrupted - the list was not empty,
        // but it would be pretty soon, and this.head becomes null
        // InternalInsertNodeBefore() does not expect that
        this.InternalInsertNodeBefore(this.head, node);
    }
    node.list = (LinkedList<T>) this;
}

Other thread calls this._workers.RemoveFirst();. There is no lock() around that statement so it completes and now list is empty. AddLast() now should call InternalInsertNodeToEmptyList(node); but it can't as the condition was already evaluated.

Putting a simple lock(this._tasks) around single this._workers.AddLast() line should prevent such scenario.

Other bad scenarios include adding item to the same list at the same time by two threads.

这篇关于创建线程时的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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