<legend id='RM3Nh'><style id='RM3Nh'><dir id='RM3Nh'><q id='RM3Nh'></q></dir></style></legend>
  • <i id='RM3Nh'><tr id='RM3Nh'><dt id='RM3Nh'><q id='RM3Nh'><span id='RM3Nh'><b id='RM3Nh'><form id='RM3Nh'><ins id='RM3Nh'></ins><ul id='RM3Nh'></ul><sub id='RM3Nh'></sub></form><legend id='RM3Nh'></legend><bdo id='RM3Nh'><pre id='RM3Nh'><center id='RM3Nh'></center></pre></bdo></b><th id='RM3Nh'></th></span></q></dt></tr></i><div id='RM3Nh'><tfoot id='RM3Nh'></tfoot><dl id='RM3Nh'><fieldset id='RM3Nh'></fieldset></dl></div>

      <tfoot id='RM3Nh'></tfoot>

      1. <small id='RM3Nh'></small><noframes id='RM3Nh'>

          <bdo id='RM3Nh'></bdo><ul id='RM3Nh'></ul>
      2. 多线程程序中的 std::string

        时间:2024-05-11
        <legend id='ty7e4'><style id='ty7e4'><dir id='ty7e4'><q id='ty7e4'></q></dir></style></legend>
      3. <tfoot id='ty7e4'></tfoot>
        <i id='ty7e4'><tr id='ty7e4'><dt id='ty7e4'><q id='ty7e4'><span id='ty7e4'><b id='ty7e4'><form id='ty7e4'><ins id='ty7e4'></ins><ul id='ty7e4'></ul><sub id='ty7e4'></sub></form><legend id='ty7e4'></legend><bdo id='ty7e4'><pre id='ty7e4'><center id='ty7e4'></center></pre></bdo></b><th id='ty7e4'></th></span></q></dt></tr></i><div id='ty7e4'><tfoot id='ty7e4'></tfoot><dl id='ty7e4'><fieldset id='ty7e4'></fieldset></dl></div>
        • <bdo id='ty7e4'></bdo><ul id='ty7e4'></ul>

            <small id='ty7e4'></small><noframes id='ty7e4'>

                  <tbody id='ty7e4'></tbody>
                • 本文介绍了多线程程序中的 std::string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  鉴于:

                  1) C++03 标准没有以任何方式解决线程的存在

                  1) The C++03 standard does not address the existence of threads in any way

                  2) C++03 标准让实现来决定 std::string 是否应该在其复制构造函数中使用 Copy-on-Write 语义

                  2) The C++03 standard leaves it up to implementations to decide whether std::string should use Copy-on-Write semantics in its copy-constructor

                  3) Copy-on-Write 语义在多线程程序中经常导致不可预测的行为

                  3) Copy-on-Write semantics often lead to unpredictable behavior in a multi-threaded program

                  我得出以下看似有争议的结论:

                  I come to the following, seemingly controversial, conclusion:

                  您根本无法在多线程程序中安全且可移植地使用 std::string

                  显然,没有任何 STL 数据结构是线程安全的.但至少,以 std::vector 为例,您可以简单地使用互斥锁来保护对向量的访问.使用使用 COW 的 std::string 实现,如果不编辑供应商实现深处的引用计数语义,您甚至无法可靠地做到这一点.

                  Obviously, no STL data structure is thread-safe. But at least, with std::vector for example, you can simply use mutexes to protect access to the vector. With an std::string implementation that uses COW, you can't even reliably do that without editing the reference counting semantics deep within the vendor implementation.

                  现实世界的例子:

                  在我的公司,我们有一个多线程应用程序,它已经过彻底的单元测试并无数次通过 Valgrind 运行.该应用程序运行了几个月没有任何问题.有一天,我在另一个版本的 gcc 上重新编译了该应用程序,突然间我总是遇到随机段错误.Valgrind 现在在 std::string 复制构造函数中报告 libstdc++ 深处的无效内存访问.

                  In my company, we have a multi-threaded application which has been thoroughly unit-tested and run through Valgrind countless times. The application ran for months with no problems whatsoever. One day, I recompile the application on another version of gcc, and all of a sudden I get random segfaults all the time. Valgrind is now reporting invalid memory accesses deep within libstdc++, in the std::string copy constructor.

                  那么解决方案是什么?嗯,当然,我可以将 std::vector<char> 定义为字符串类 - 但实际上,这很糟糕.我也可以等待 C++0x,我祈祷这将要求实现者放弃 COW.或者,(不寒而栗),我可以使用自定义字符串类.我个人总是反对那些实现自己的类的开发人员,当预先存在的库可以正常工作时,但老实说,我需要一个字符串类,我可以肯定它没有使用 COW 语义;而 std::string 根本不能保证这一点.

                  So what is the solution? Well, of course, I could typedef std::vector<char> as a string class - but really, that sucks. I could also wait for C++0x, which I pray will require implementors to forgo COW. Or, (shudder), I could use a custom string class. I personally always rail against developers who implement their own classes when a preexisting library will do fine, but honestly, I need a string class which I can be sure is not using COW semantics; and std::string simply doesn't guarantee that.

                  std::string 根本不能可靠地使用在可移植的多线程程序中是对的吗?什么是好的解决方法?

                  Am I right that std::string simply cannot be used reliably at all in portable, multi-threaded programs? And what is a good workaround?

                  推荐答案

                  鉴于标准没有提及内存模型并且完全不知道线程,我想说你不能肯定地假设每个实现都会非牛所以不,你不能

                  Given that the standard doesn't say a word about memory models and is completely thread unaware, I'd say you can't definitely assume every implementation will be non-cow so no, you can't

                  除此之外,如果您了解自己的工具,大多数实现将使用非母牛字符串来允许多线程.

                  Apart from that, if you know your tools, most of the implementations will use non-cow strings to allow multi-threading.

                  这篇关于多线程程序中的 std::string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::copy 如何与流迭代器一起工作 下一篇:C++/STL 是否支持按属性对对象进行排序?

                  相关文章

                  <small id='ToYyc'></small><noframes id='ToYyc'>

                • <i id='ToYyc'><tr id='ToYyc'><dt id='ToYyc'><q id='ToYyc'><span id='ToYyc'><b id='ToYyc'><form id='ToYyc'><ins id='ToYyc'></ins><ul id='ToYyc'></ul><sub id='ToYyc'></sub></form><legend id='ToYyc'></legend><bdo id='ToYyc'><pre id='ToYyc'><center id='ToYyc'></center></pre></bdo></b><th id='ToYyc'></th></span></q></dt></tr></i><div id='ToYyc'><tfoot id='ToYyc'></tfoot><dl id='ToYyc'><fieldset id='ToYyc'></fieldset></dl></div>
                • <tfoot id='ToYyc'></tfoot>
                  1. <legend id='ToYyc'><style id='ToYyc'><dir id='ToYyc'><q id='ToYyc'></q></dir></style></legend>

                      <bdo id='ToYyc'></bdo><ul id='ToYyc'></ul>