13,052,342 members (57,521 online)
Rate this:
See more:
Hi Everyone,
I'm a student who is creating a basic multi-threading program. It has two threads, each running simple loops. One is counting up from 0 to 10 and the other from 10 to 0.
All I am after is get the program to know when both threads get to the same number, I know it will be five (or at least should be) but I don't know how to pull out this data each time so that it can be checked. I've tried using return but this obviously exits the loop and therefore will only run each loop one iteration. Below is the code I currently have.

Thanks for any help or advice.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
object iValue = 0;
object iValue2 = 10;
});

{
});
th.Start();
th2.Start();
Console.WriteLine("The two numbers are identical: " + iValue.ToString() + " and " + iValue2.ToString());
}
{
for (int i = 0; i < 10; i++)
{
int iValueLocal = (int)iV;
iValueLocal++;
iV = iValueLocal;

}
object iB = (object)iV;
return iB;
}

{
for (int i = 10; i > 0; i--)
{
int iValueLocal = (int)iV;
iValueLocal--;
iV = iValueLocal;
}
object iB = (object)iV;
return iB;
}
}
}```
Posted 13-Apr-12 9:46am

Rate this:

## Solution 1

look at each thread that you are running.

Ask yourself: do I see any logic that will stop the thread when iValue == iValue2?

Also ask yourself: what happens in my code if the threads start, they have not stopped and my if statement is false?

You need to figure out a way for your main program to continue looping while the threads have not stopped.

I hope this puts you in the proper direction for debugging your code.
v2
pows1_09 14-Apr-12 7:33am

I appreciate the comment, however the values never change and therefore the program stops straight away if I put in this statement.
Rate this:

## Solution 2

It looks like the purpose of your code is to demonstrate the effect of race condition :-). Am I right?

No matter, there is nothing wrong the idea of data exchange with a running thread; and knowing good techniques for that is very important.

More than that, it helps to encapsulate the data accessible by both the wrapped thread and other threads. Making some members private guarantees that the data is used only in a wrapped thread. Other members can be done internal/private and used by more then one thread. If is advisable to provide such access only to the methods and properties. This way, access to the shared data can be synchronized with some thread synchronization primitives, such as `lock` statement, `System.Threading.EventWaitHandle`, `System.Threading.ReaderWriterLockSlim`, `System.Threading.Monitor` and the like — please see:
http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.100%29.aspx[^],

What is really important, the use of the thread synchronization objects is hidden inside the wrapper; the objects cannot be misused by the code using the wrapper. Such misuse is a usual problem, so this is yet another considerable benefit.

How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

The code sample in the second answer demonstrates the use of `lock` statement.

—SA
pows1_09 14-Apr-12 5:18am

Thanks for the reply, this seems very useful and your code used on:
looks good, I seem to understand the logic but why is it internal?
The reason I ask is because I've created a new class called ThreadWrapper which I made internal, and added your code to it, but it comes up with lots of errors. Is the class already created? and rather than doing all the internal bits, is it just possible to do a get and set method within my code as currently I am not aiming for efficiency or organisation, purely something that works.
SAKryukov 20-Apr-12 16:49pm

It is internal because it's the best to give minimum access, in order to avoid programming mistakes. Internal, as you need to know, only limits access to the same assembly, in all other respects it is exactly the same as public. In this case, I consider it reasonable: you can have different wrappers for different assemblies. If you want to re-use some wrapper across assemblies, make it public, make Body "protected abstract" and override in descendant classes.
--SA

Top Experts
Last 24hrsThis month
 OriginalGriff 313 David_Wimbley 100 Jochen Arndt 95 Richard MacCutchan 95 ppolymorphe 90
 OriginalGriff 5,536 RickZeeland 1,944 ppolymorphe 1,848 F-ES Sitecore 1,636 Dave Kreskowiak 1,409