Big problem with LifeStlePerThread. Please help.

27 views
Skip to first unread message

mynkow

unread,
Nov 26, 2012, 12:35:42 PM11/26/12
to castle-pro...@googlegroups.com
Hi,

imagine a class which holds some state. This state should be unique for every thread. So I register the component with LifeStylePerThread. This state is set only once when the object is created.

The problem is that some threads are reused and when I call

Task.Factory.StartNew(...) I get an existing thread with already initialized state. I tried to force creating a new thread every time a new Task is started but it is impossible.

Is there any workaround this problem?


Mauricio Scheffer

unread,
Nov 26, 2012, 12:53:01 PM11/26/12
to castle-pro...@googlegroups.com
It sounds like you actually want to *recreate* that state for each StartNew instead of having a constant state for each thread as LifeStylePerThread does.
When using System.Threading.Tasks you probably shouldn't assume any particular thread distribution or assignment most of the time. For example, it's possible to run an entire Task computation synchronously. One of the points of these abstractions is that you don't have to mess with threads yourself.





--
Mauricio





--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/tFMByj4aRWwJ.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

mynkow

unread,
Nov 26, 2012, 3:04:09 PM11/26/12
to castle-pro...@googlegroups.com
10x Mauricio,
I will try to explain this in more details:


consider the code below. Each task has components with lifestle per thread. For example ISessionFactory. At some point a thread is reused and if I resolve ISessionFactory or other object with style per thread I will get OLD and obsolete instances. May be Windsor returns object instances based on thread ID which in this case is wrong. The code line where "Thread reused" is written to the console can be container.Resolve<>().

static void Main(string[] args)
        {
            ConcurrentDictionary<int, int> startedThreads = new ConcurrentDictionary<int, int>();

            for (int i = 0; i < 10; i++)
            {
                for (int x = 0; x < 10; x++)
                    Task.Factory.StartNew(() =>
                    {
                        startedThreads.AddOrUpdate(Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.ManagedThreadId, (a, b) => b);
                    }, TaskCreationOptions.LongRunning);

                for (int j = 0; j < 100; j++)
                {
                    Task.Factory.StartNew(() =>
                    {
                        while (true)
                        {
                            Thread.Sleep(10);
                            if (startedThreads.ContainsKey(Thread.CurrentThread.ManagedThreadId))
                                Console.WriteLine("Thread reused");
                        }
                    }, TaskCreationOptions.LongRunning);
                }
            }

            Console.Read();
        }


Reply all
Reply to author
Forward
0 new messages