Days ago, as a c# newbie, I wrote a class and thank it was good to put common variables (but different values) used by different threads in it, each one accessing a different postgres database. Recently, knowing a bit more about c# I was wondering if this was really correct. Given what I read everywhere, I was expecting that fields initialized by thread "one" would be overwritten by thread "two", in other words both threads would finally get the same values for all fields.
In fact, it does not appear to work like that, each thread seems to keep its own values, thread one is still with db1 value and thread two with db2 value. How come is this possible ? Am I wrong when I thank I understood that fields from different instances of the same class were sharing the same values ?
I'm using Visual Studio 2013, which requires to set [STAThread] at the beginning of the code, could that be an explanation ?
Thanks for your kind and skilled explanations
Olivier
public class Context
{
public string host;
public string port;
public string user;
public string pwd;
public string dbname;
...
public Context(string db) {// Each thread is launched with a different database name
try {
var rep = string.Format(@"D:\Bd\{0}\params\config.txt",db); // one file for each thread
if (File.Exists(rep)) {
var dic = File.ReadAllLines(rep)
.Select(l => l.Split(new[] { '=' }))
.ToDictionary(s => s[0].Trim(), s => s[1].Trim());
host = dic["host"];
port = dic["port"];
user = dic["user"];
pwd = dic["pwd"];
dbname = db;
...
Thread thThd1 = new Thread(startthd1);
thThd1.Start();
public static void startthd1() {
Context ncontext = new Context("db1");
Chkthd gth = new Chk(ncontext);
}
Thread thThd2 = new Thread(startthd2);
thThd2.Start();
public static void startthd2() {
Context ncontext = new Context("db2");
Chkthd gth = new Chk(ncontext);
}
Given the example code in the question there is no reason to expect one thread to overwrite the other thread's fields. When that code executes two new threads are created and these thread independently and concurrently perform file I-O reading the "db1" and "dn2" file paths.
If you place a breakpoint on the "Chkthd gth = new Chk(ncontext);" statements in both startthd1() and startthd2() you should expect both threads to halt at these statements together and the local variables in each method to hold different data as read from the two files.
I suspect threads need not be used in the example code and that there is a simpler solution.
Does the following code, which will run slightly slower, provide the outcome required?
Chk gth1 = ReadFileData( "db1" );
Chk gth2 = ReadFileData( "db2" );
public static Chkthd ReadFileData( string dbName ) {
Context ncontext = new Context("db2");
return new Chk(ncontext);
}