Giter Site home page Giter Site logo

Comments (8)

idealvin avatar idealvin commented on May 4, 2024

@snej

  • Copying fastring is thread-safe like the std::string.
  • copy-on-write is not used in fastring.

from coost.

snej avatar snej commented on May 4, 2024

Atomically updating the ref-count is not enough to make the implementation thread-safe! The copy constructor and assignment operator create multiple instances that point to the same memory, so modifying those instances on multiple threads will cause problems.

For example:

  • fastring a = "......";
  • fastring b = a;
  • Pass b to a second thread
  • On the first thread, call a.find(...) -- this will access bytes pointed to by _p->s
  • On the second thread, call b.append(...) -- this may call realloc(_p->s), which will free the bytes being used by the first string

This would work fine with std::string because the copy constructor allocates a new heap block for the bytes, making the new object fully independent from the first.

It appears fastring has a clone method that creates a fully independent copy, and that could be used for thread-safety.

I don't mean to say there's anything wrong with fastring; but it has very different behavior than std::string, and using it without knowing that will cause very nasty bugs in multi-threaded code. So I'm just suggesting that you document this.

from coost.

idealvin avatar idealvin commented on May 4, 2024

from coost.

idealvin avatar idealvin commented on May 4, 2024

@snej
Hi, Jens, the document for fastring already said about it. See Special Notes in the document:

fastring s = "xx";
fastring t = s;   // s, t points to the same string
t + = 'x';        // s, t-> "xxx"
t = "888";        // s, t-> "888"

fastring r = "r";
t = r;            // t-> "r", s is still "888", only the reference count is decremented by 1

fastring x;       // empty string, no memory allocated
fastring y = x;   // x and y do not share memory since no memory is allocated
y = "yy";         // y -> "yy", x is still empty

I'm not sure if it is clear enough. You may help to improve it?

from coost.

snej avatar snej commented on May 4, 2024

That section helps, but it only tells half the story. If the shared string storage were thread-safe [the way std::string was long ago] there would not be concurrency issues. So the other half of the story is to point out that it's not thread-safe.

I suggest adding a section like:

Warning for multi-threaded use: Strings should never be shared between threads that might modify them, because the operations that mutate string storage are not thread-safe. To safely pass a string from one thread to another, call its clone method, which creates an entirely separate fastring with its own copy of the storage.

from coost.

idealvin avatar idealvin commented on May 4, 2024

That section helps, but it only tells half the story. If the shared string storage were thread-safe [the way std::string was long ago] there would not be concurrency issues. So the other half of the story is to point out that it's not thread-safe.

I suggest adding a section like:

Warning for multi-threaded use: Strings should never be shared between threads that might modify them, because the operations that mutate string storage are not thread-safe. To safely pass a string from one thread to another, call its clone method, which creates an entirely separate fastring with its own copy of the storage.

@snej OK. Thanks. I' ll do it later.

from coost.

idealvin avatar idealvin commented on May 4, 2024

I'll document it at the next release.

from coost.

idealvin avatar idealvin commented on May 4, 2024

@snej I have removed reference counting from fastring. Now it has a similar copying behavior to std::string.

from coost.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.