|
List Info
Thread: C-Sharp (C#) Group: Threading help
|
|
| C-Sharp (C#) Group: Threading help |

|
2006-08-24 12:47:11 |
Hello:
I Have the followin code:
using System;
using System.Threading;
namespace ThreadJoinTest {
public class Program {
public static void Main() {
Program p = new Program();
p.Run();
}
internal void Run() {
Thread t1 = new Thread(new
ThreadStart(Method1));
Thread t2 = new Thread(new
ThreadStart(Method2));
t1.Name = "T1";
t2.Name = "T2";
t1.Start();
t2.Start();
}
private void Method1() {
while (true) {
MethodContainer.Instance.Method();
}
}
private void Method2() {
while (true) {
MethodContainer.Instance.Method();
}
}
internal class MethodContainer {
private static object syncObj = new object();
internal static readonly MethodContainer
Instance = new
MethodContainer();
internal void Method() {
lock (syncObj) {
Console.Out.WriteLine("Thread Id:
" +
Thread.CurrentThread.Name);
}
}
}
}
}
A few questions about it:
If i run this code, the methods does not run in an ordered
manner, i
get many times T1 and many times T2 as output. What is the
best way to
ensure that both methods consecutively executes the method,
so output
will be:
T1
T2
T1
T2
.....
Maybe using a Mutex?
And second, if the class where the method resides is static,
is
necesary to declare the lock block in order to make it
accesible just
for one thread at the same time?
Thanks in advance
Oscar Acosta
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharp googlegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribe googlegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---
|
|
| C-Sharp (C#) Group: Re: Threading help |

|
2006-08-25 09:09:37 |
Answering your second question first : If you need to ensure
that a
block of code does not run simultaneously from two threads,
use a lock.
What you've done with the MethodContainer class is right.
Ensuring your code outputs T1, T2, T1, T2 can be done in a
few ways.
The easiest would be to keep two Monitors (mutex) and
Pulse(signal)
them.
i.e.
public class Program {
private static object m1 = new object();
private static object m2 = new object();
public static void Main() {
Program p = new Program();
p.Run();
}
internal void Run() {
Thread t1 = new Thread(new
ThreadStart(Method1));
Thread t2 = new Thread(new
ThreadStart(Method2));
t1.Name = "T1";
t2.Name = "T2";
t1.Start();
t2.Start();
Monitor.Pulse(m1);
}
private void Method1() {
while (true) {
Monitor.Wait(m1);
MethodContainer.Instance.Method();
Monitor.Pulse(m2);
}
}
private void Method2() {
while (true) {
Monitor.Wait(m2);
MethodContainer.Instance.Method();
Monitor.Pulse(m1);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharp googlegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribe googlegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---
|
|
| C-Sharp (C#) Group: Re: Threading help |

|
2006-08-25 09:09:37 |
Answering your second question first : If you need to ensure
that a
block of code does not run simultaneously from two threads,
use a lock.
What you've done with the MethodContainer class is right.
Ensuring your code outputs T1, T2, T1, T2 can be done in a
few ways.
The easiest would be to keep two Monitors (mutex) and
Pulse(signal)
them.
i.e.
public class Program {
private static object m1 = new object();
private static object m2 = new object();
public static void Main() {
Program p = new Program();
p.Run();
}
internal void Run() {
Thread t1 = new Thread(new
ThreadStart(Method1));
Thread t2 = new Thread(new
ThreadStart(Method2));
t1.Name = "T1";
t2.Name = "T2";
t1.Start();
t2.Start();
Monitor.Pulse(m1);
}
private void Method1() {
while (true) {
Monitor.Wait(m1);
MethodContainer.Instance.Method();
Monitor.Pulse(m2);
}
}
private void Method2() {
while (true) {
Monitor.Wait(m2);
MethodContainer.Instance.Method();
Monitor.Pulse(m1);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharp googlegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribe googlegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---
|
|
| C-Sharp (C#) Group: Re: Threading help |

|
2006-08-30 15:43:57 |
Your help was usefull.
Thanks Praving for taking your time to answer!
Pravin Paratey wrote:
> Answering your second question first : If you need to
ensure that a
> block of code does not run simultaneously from two
threads, use a lock.
> What you've done with the MethodContainer class is
right.
>
> Ensuring your code outputs T1, T2, T1, T2 can be done
in a few ways.
> The easiest would be to keep two Monitors (mutex) and
Pulse(signal)
> them.
>
> i.e.
>
> public class Program {
> private static object m1 = new object();
> private static object m2 = new object();
> public static void Main() {
> Program p = new Program();
> p.Run();
> }
>
> internal void Run() {
> Thread t1 = new Thread(new
ThreadStart(Method1));
> Thread t2 = new Thread(new
ThreadStart(Method2));
> t1.Name = "T1";
> t2.Name = "T2";
> t1.Start();
> t2.Start();
> Monitor.Pulse(m1);
> }
>
> private void Method1() {
> while (true) {
> Monitor.Wait(m1);
> MethodContainer.Instance.Method();
> Monitor.Pulse(m2);
> }
> }
>
> private void Method2() {
> while (true) {
> Monitor.Wait(m2);
> MethodContainer.Instance.Method();
> Monitor.Pulse(m1);
> }
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharp googlegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribe googlegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---
|
|
[1-4]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|