Click here to Skip to main content
15,915,603 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I wanted to know if EACH thread is using it's on method in this code:

List<thread> threadArray= new List<thread>();
int numberOfWorkers = 10;
EventWaitHandle ewh = AutoResetEvent(false);
Queue<string> container = new Queue<string>();
object sync = new object();

for(int i = 0; i < numberOfWorkers; i++)
{
    Thread t = new Thread(Worker);
    threadArray.Add(t);
    threadArray[i].Start();
}


public void Producer(string task)
{
   lock(sync)
   {
      container.Enqueue(task);
   }
   ewh.Set();
}

public void Worker()
{
    lock(sync)
    {
       string temp = container.Dequeue();
    }

    // do some work
    // after work is complete wait...
    ewh.WaitOne();
}</string></string></thread></thread>


So basically what I'm asking is if each thread is using it's "own" Worker method or are they (threads) all sharing this single Worker method?
Posted
Updated 4-Nov-11 12:48pm
v3
Comments
[no name] 4-Nov-11 18:52pm    
Format code
d.allen101 4-Nov-11 19:01pm    
so what would be the purpose of using multiple threads if they're all sharing the the same method? there's no concurrent processing going.
Sergey Alexandrovich Kryukov 4-Nov-11 19:41pm    
Why do you think it can be a problem for thread to use the same method? It happens all the time; what is important is that they use the separate stacks.
--SA
d.allen101 4-Nov-11 19:47pm    
because in the in my app the queue is building at such a fast rate that it's consuming all of my system resources (cpu usage and memory).

Each thread will share the method
 
Share this answer
 
There is only one method but there are 10 threads.
The line you need is perhaps this one:
C#
System.Diagnostics.Trace.WriteLine(string.Format("I am in thread #{0}", System.Threading.Thread.CurrentThread.ManagedThreadId));


Place this line in various places and it will tell you what thread run what.
 
Share this answer
 
Hello,


Threads create there own copy of method they call. But its us who decide the data members/variables used in that function are inside the scope of function or from outside... For eg:


C#
Class A
{

    int inta;
    public void someOperation()
    {

    .... Code to do some operations on variable inta
    }

}


In above case , Threads will have difference copy of function but the variable used by all copy of function is common. so you need to maintain concurrency of value for variable inta.


another example :

<pre lang="c#">
class b
{
  public voidSomeOperation()
  {
     int intb;
     .... code performing some operation on intb;
  }

}


In above case there isn't any need to maintain concurrency as there will be separate copy of whole function and function is using local variable.

Let me know if answer was useful for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900