Multi-threaded app and Thread Safety. - DOTNET
This is a discussion on Multi-threaded app and Thread Safety. - DOTNET ; I have a windows service that spawns multiple threads running a thing that
does a bunch of processing. Here's the code that spawns the threads:
m_totalThreads=Convert.ToInt32(Settings.GetSetting("ProcessorThreads"));
m_executingThreads=new Thread[m_totalThreads];
for (int i=0;i < m_totalThreads;i++)
{
ProcessManager processor=new ProcessManager();
m_executingThreads[i]=new Thread(new ThreadStart(processor.Process));
...
-
Multi-threaded app and Thread Safety.
I have a windows service that spawns multiple threads running a thing that
does a bunch of processing. Here's the code that spawns the threads:
m_totalThreads=Convert.ToInt32(Settings.GetSetting("ProcessorThreads"));
m_executingThreads=new Thread[m_totalThreads];
for (int i=0;i < m_totalThreads;i++)
{
ProcessManager processor=new ProcessManager();
m_executingThreads[i]=new Thread(new ThreadStart(processor.Process));
m_executingThreads[i].Name=i.ToString();
m_executingThreads[i].Start();
}
I haven't done a lot of multi-threaded stuff in the past, so I want to make
sure that I get this right. 
My question is the following: Because I'm creating a new instance of the
ProcessManager for each thread, will I have problems with the classes that
the process manager spawns? Basically, do I need to worry about locking
instance variables in classes called by the processes manager that runs on
each thread?
Robert
-
Re: Multi-threaded app and Thread Safety.
As long as the ProcessManager only invokes methods on object instances it
creates (and these instances don't share any static data), then there's no
issue. Basically, as long as the object will ever be accessed on a single
thread, there's no need for locking.
Ken
"Robert May" <rakkerspamisevil91@hotmail.com> wrote in message
news:%23d59pbLgEHA.2020@TK2MSFTNGP10.phx.gbl...
> I have a windows service that spawns multiple threads running a thing that
> does a bunch of processing. Here's the code that spawns the threads:
>
> m_totalThreads=Convert.ToInt32(Settings.GetSetting("ProcessorThreads"));
>
> m_executingThreads=new Thread[m_totalThreads];
>
> for (int i=0;i < m_totalThreads;i++)
>
> {
>
> ProcessManager processor=new ProcessManager();
>
> m_executingThreads[i]=new Thread(new ThreadStart(processor.Process));
>
> m_executingThreads[i].Name=i.ToString();
>
> m_executingThreads[i].Start();
>
> }
>
> I haven't done a lot of multi-threaded stuff in the past, so I want to
make
> sure that I get this right. 
>
> My question is the following: Because I'm creating a new instance of the
> ProcessManager for each thread, will I have problems with the classes that
> the process manager spawns? Basically, do I need to worry about locking
> instance variables in classes called by the processes manager that runs on
> each thread?
>
> Robert
>
>
>
>
>
>
-
Re: Multi-threaded app and Thread Safety.
Robert May <rakkerspamisevil91@hotmail.com> wrote:
> I have a windows service that spawns multiple threads running a thing that
> does a bunch of processing. Here's the code that spawns the threads:
>
> m_totalThreads=Convert.ToInt32(Settings.GetSetting("ProcessorThreads"));
>
> m_executingThreads=new Thread[m_totalThreads];
>
> for (int i=0;i < m_totalThreads;i++)
>
> {
>
> ProcessManager processor=new ProcessManager();
>
> m_executingThreads[i]=new Thread(new ThreadStart(processor.Process));
>
> m_executingThreads[i].Name=i.ToString();
>
> m_executingThreads[i].Start();
>
> }
>
> I haven't done a lot of multi-threaded stuff in the past, so I want to make
> sure that I get this right. 
>
> My question is the following: Because I'm creating a new instance of the
> ProcessManager for each thread, will I have problems with the classes that
> the process manager spawns? Basically, do I need to worry about locking
> instance variables in classes called by the processes manager that runs on
> each thread?
It really depends on whether any of the data that they use is shared.
If two different process managers end up with references to the same
instances of other classes, you'll need to think about how that data is
shared. If they're dealing with entirely separate bits of data, you
don't need to worry.
See http://www.pobox.com/~skeet/csharp/multithreading.html for more
information. (It doesn't talk about this particular issue (yet!) but
you may find it handy anyway.)
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Similar Threads
-
By Application Development in forum Java
Replies: 4
Last Post: 11-29-2007, 06:38 AM
-
By Application Development in forum CSharp
Replies: 2
Last Post: 10-17-2007, 09:52 AM
-
By Application Development in forum CSharp
Replies: 5
Last Post: 10-17-2007, 05:26 AM
-
By Application Development in forum ASM x86 ASM 370
Replies: 5
Last Post: 07-15-2007, 11:39 AM
-
By Application Development in forum verilog
Replies: 1
Last Post: 05-18-2007, 09:26 AM