Do not use string/wstring in multi-threaded Environments

Source: Internet
Author: User
String/wstring are two specific applications of the basic_string template. They are the essence of the C ++ template library. As a C ++ programmer, I especially like these two classes. The C ++ standard requires that data sharing be supported for the implementation of string/wstring. That is to say, for the same data, different string/wstring objects must share it, as shown in:
+ ----------------------------- + PTR
| String/wstring object1 | ------------ +
+ ----------------------------- + | Heap
+ ----------------------------- + PTR | + ------------------ +
| String/wstring object2 | ------------ + -----> | "I like string" |
+ ----------------------------- + | + ----------------- +
+ ----------------------------- + PTR |
| String/wstring object3 | ------------ +
+ ----------------------------- +
When the data in the heap is released, the reference count is used. When the count is 0, delete [] is called to release the memory. Of course, if a string/wstring object needs to write data, the new memory object will be created and used, and the reference count of the old memory object will be reduced. In the Demo code below, we can see this situation.
This quote technology is very good for memory saving, but it also has some negative effects. For example, in a multi-threaded environment, if the increase or decrease of the referenced technology is not synchronized, the consequence is very serious. The program is light on Memory leakage, and the program crashes. Some netizens may ask why the C ++ standard does not provide a secure string/wstring? I think there may be two main reasons:
1) reduced portability, because the implementation methods or names of synchronization are different on different platforms. If the synchronization operation is forced, the portability of the C ++ program is sacrificed. The standard C ++ program is platform independent, which is contrary to it.
2) Performance Load Caused by synchronization. For example, for an int ++ operation, only several bus cycles are required. However, during synchronization, it is at least several hundred or even thousands of bus cycles. It may be intolerable for C ++, which has always been concerned about efficiency.

If the program is written in a multi-threaded environment and you do not want to use string/wstring in the code implementation process, do not use them. Let the original char and wchar recover. Although it brings a lot of trouble to the operation, it can make yourself sleep :-). Of course, in special cases, we may use STD: vector <Char/wchar> to replace string/wstring and automatically manage memory allocation and release. After all, this is what we need most, isn't it ?!

# Include <iostream>
# Include <string>

Using namespace STD;

Int main (void)
{
String A ("I like string ");
String B =;
String c = "I like string ";
String d = A. c_str ();

Cout <"See A and B :";
If (A. c_str () = B. c_str ())
Cout <"share memory" <Endl;
Else
Cout <"unshare memory" <Endl;

Cout <"See A and C :";
If (A. c_str () = C. c_str ())
Cout <"share memory" <Endl;
Else
Cout <"unshare memory" <Endl;

Cout <"See A and D :";
If (A. c_str () = D. c_str ())
Cout <"share memory" <Endl;
Else
Cout <"unshare memory" <Endl;

Cout <"See C and D :";
If (C. c_str () = D. c_str ())
Cout <"share memory" <Endl;
Else
Cout <"unshare memory" <Endl;

Cout <"Modify B" <Endl;
B = "I like string ";
Cout <"See A and B again :";
If (A. c_str () = B. c_str ())
Cout <"share memory" <Endl;
Else
Cout <"unshare memory" <Endl;

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.