我想完全了解函数调用参数如何交错的具体规定。在我看来,这有很多含义。举个例子:
void mad(cow_string a, cow_string b);
cow_string s("moo");
cow_string s1 = s;
cow_string s2 = s;
mad(s1+="haha",s2+="hahaha");
Sutter在cow_string
GotW 上描述的 Copy-On-Write 字符串容器在哪里:http ://www.gotw.ca/gotw/045.htm
s1+="haha"
如果对和的评估s2+="hahaha"
以非常精细的粒度进行交错,这是否意味着这会在 cow_strings 内部引用计数上创建竞争条件(取决于编译器)?如果我尝试使用互斥锁来防止竞争条件,那甚至不会导致单线程程序中的自锁(这让我很头疼)。例如,S1 制作内部副本并获取互斥体以减少引用计数上下文切换S2 也制作内部副本并运行到互斥体和 bam 自锁。
(仅当第一个为真时)如果我的团队的其他成员不是专家或不知道它是牛,是否有安全的方法使一个对象成为牛?
编辑:
为清楚起见,Herb Sutters 的示例动摇了我对表达方式不是很交错的图片:
// In some header file:
void f( T1*, T2* );
// In some implementation file:
f( new T1, new T2 );
这样做:
allocate memory for the T1
construct the T1
allocate memory for the T2
construct the T2
call f()
或这个:
allocate memory for the T1
allocate memory for the T2
construct the T1
construct the T2
call f()
在这里阅读:http: //flylib.com/books/en/3.259.1.55/1/
第二次编辑:我想我假设一个引用计数器更改函数cow_string
被内联,这是一个愚蠢的假设。没有那个愚蠢的假设,我的问题并没有多大意义。不过感谢您的回答!