Comments (8)
- Copying
fastring
is thread-safe like thestd::string
. copy-on-write
is not used infastring
.
from coost.
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 callrealloc(_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.
from coost.
@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.
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 separatefastring
with its own copy of the storage.
from coost.
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 separatefastring
with its own copy of the storage.
@snej OK. Thanks. I' ll do it later.
from coost.
I'll document it at the next release.
from coost.
@snej I have removed reference counting from fastring
. Now it has a similar copying behavior to std::string
.
from coost.
Related Issues (20)
- 请教关于hook内核sys_xxx系统调用函数
- 如何打印格式化的 log HOT 3
- fastream 在不同平台测试比 snprintf 快 10~30 倍左右, 但是如何快速格式化字符串为faststring 类呢 HOT 3
- Sched::loop 中co::array<Closure*>(s).swap(new_tasks); 是不是应该是 co::array<Coroutine*>(s).swap(ready_tasks); HOT 1
- co::lru_map insert 问题 HOT 1
- 浮点数转字符串没有四舍五入? HOT 3
- json模块功能增强 HOT 5
- 通过 conan 安装遇到问题 HOT 2
- 请求:hash接口新增sha1 HOT 1
- json是否支持多种编码? HOT 2
- 线程频繁创建销毁出现内存泄露 HOT 2
- mkconf 导出参数默认值带有“//”时的问题 HOT 2
- 用wait_group执行计算图 HOT 2
- 关于coost文档付费的声明 HOT 8
- IOS旧设备上存在加载库就内存申请奔溃现象 HOT 2
- Windows 10使用最新版代码,程序退出时Log crash HOT 2
- Memory allocation error using co/stl containers (co::vector, co::hash_map) HOT 6
- 在armv7下运行协程会coredump HOT 3
- hook失效 HOT 15
- windows下使用CMake GUI进行配置,没有BUILD_SHARED_LIBS 选项 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coost.