I have an application with a main thread (UI thread) and worker thread.
The main thread passes a large string to the worker thread which is then performing some work based on the passed string.
During the execution of the worker thread the contents of the passed string can possibly be changed.
Is there any way to copy the string (e.g. in the constructor of the worker thread) to avoid possible surprises and work on that string copy instead?
Would copy() or UniqueString() do the trick?
There is no need to call anything. Most frequently used Delphi strings (string, AnsiString, and UnicodeString) implement "copy on write" mechanism. If a thread modifies a shared string, a new copy of the string will automatically be created in the thread and this copy will be modified.
If you still want to have a string that is not "owned" by any other part of the code, you can call UniqueString(s).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With