Accessing the same string (StringBuilder) using multi-threaded

My problem is that I am using multithreading on the same line somewhere

the string will not be replaced. (I wrote this in notepad, so the syntax could be

wrong)

using System.Thread ... Others, of course,

class .... { private static StringBuild container = new StringBuilder(); static void Main(...) { container.Append(Read From File(Kind of long)); Thread thread1 = new Thread(Function1); Thread thread2 = new Thread(Function2); thread1.Start(); thread2.Start(); //Print out container } static void Function1 { //Do calculation and stuff to get the Array for the foreach foreach (.......Long loop........) { container.Replace("this", "With this") } } //Same goes for function but replacing different things. static void Function2 { //Do calculation and stuff to get the Array for the foreach foreach (.......Long loop........) { container.Replace("this", "With this") } } } 

Now some item is not getting a replacement. So my solution to this is calling the container. Replace it with another

and do a "lock" that works, but is this the right way?

 private class ModiflyString { public void Do(string x, string y) { lock (this) { fileInput.Replace(x, y); } } } 
0
source share
3 answers

You must lock the StringBuilder object itself (inside the replacement functions):

 lock (container) { container.Replace("this", "With this"); } 

or create a separate lock object:

 static object _stringLock = new object(); ... lock(stringLock) { container.Replace("this", "With this"); } 
+5
source

Your lock will not work if you create more than one ModifyString object, and I assume that you do.

Simple version:

  public void Do(string x, string y) { lock (fileInput) { fileInput.Replace(x, y); } } 

It is better to create a separate object for blocking, but the above shows the principle better: all competing threads must block the same object.

A standard approach would look like this:

 private static StringBuild container = new StringBuilder(); private static object syncLock = new object(); // simple object, 1-1 with container 

and then you can (thread-) use safely:

  lock(syncLock) { container.Replace(...); } 
+3
source

This will work fine as long as both threads have the same instance of the ModifyString class. In other words, this will work because the lock on "this" must be a lock on one instance:

 class Blah { private static StringBuild container = new StringBuilder(); private static ModifyString modifyString = new ModifyString(); static void Main(...) { container.Append(Read From File(Kind of long)); Thread thread1 = new Thread(Function1); Thread thread2 = new Thread(Function2); thread1.Start(); thread2.Start(); //Print out container } static void Function1 { //Do calculation and stuff to get the Array for the foreach foreach (.......Long loop........) { modifyString.Do("this", "With this") } } //Same goes for function but replacing different things. static void Function2 { //Do calculation and stuff to get the Array for the foreach foreach (.......Long loop........) { modifyString.Do("this", "With this") } } } 

It will NOT work if you did it below, because locking (this) will not work, they are two separate instances:

 class Blah { private static StringBuild container = new StringBuilder(); static void Main(...) { container.Append(Read From File(Kind of long)); Thread thread1 = new Thread(Function1); Thread thread2 = new Thread(Function2); thread1.Start(); thread2.Start(); //Print out container } static void Function1 { ModifyString modifyString = new ModifyString(); //Do calculation and stuff to get the Array for the foreach foreach (.......Long loop........) { modifyString.Do("this", "With this") } } //Same goes for function but replacing different things. static void Function2 { ModifyString modifyString = new ModifyString(); //Do calculation and stuff to get the Array for the foreach foreach (.......Long loop........) { modifyString.Do("this", "With this") } } } 

Some people will actually create a β€œdummy” object to perform a lock instead of using β€œthis” (you cannot lock a row, as it is a value type).

+2
source

All Articles