Code Monkey home page Code Monkey logo

cpp_concurrency_in_action's Issues

[第一章]1.4开始入门 翻译有少了个字

ok!现在你有一个很棒的与C++11标准兼容的编译器。接下来呢?一个多线程C++程序是什么样子的?它看上去和其他C++程序差多 ==> 这里应该少了一个字吧?

[第二章]这里的例子语法有错

使用lambda表达式也能避免这个问题。lambda表达式是C++11的一个性特性, 它允许使用一个可以捕获局部变量的局部函数(可以避免传递参数,参见2.2节)。想要具体的了解lambda表达式,可以阅读附录A的A.5节。之前的例子可以改写为lambda表达式的类型,如下:

std::thread my_thread([](
  do_something();
  do_something_else();
});

上面的例子括号没对齐,第一行最后一个应该为{,这里的代码应该为:

std::thread my_thread([]{
  do_something();
  do_something_else();
});

第三章 3.2 有错字

这时四核处理系统的性能就和单核处理 四通 的四倍差不多了。

BTW. 翻译的很好!

清单3.4 线程安全的堆栈类定义错误

  1. 第三章的例子3.4有错误

    struct empty_stack: std::exception
    {
    const char* what() const throw() {};
    };
    

    不应该有{}

  2. 清单3.5 扩充(线程安全)堆栈

    threadsafe_stack()
      : data(std::stack<int>()){}                                     
    

    应为

    threadsafe_stack() {}
    

    或者

    threadsafe_stack()
      : data(std::stack<T>()){} 
    

3.2.3节问题

3.2.3节 Option1:
大多数情况下,这种方式还不错,但有明显的缺点:需要临时构造出一个堆中类型的实例,用于接收目标值。
应该为:
大多数情况下,这种方式还不错,但有明显的缺点:需要调用者在调用前构造出一个栈中元素类型的实例,用于接收目标值。

3.2.3 节 选项2

第二段的翻译, 个人看法:
虽然是安全的, 但并不理想。即使可以通过使用std::is_nothrow_copy_constructible 和 std::is_nothrow_move_constructible 类型特性在编译期检测到不抛出异常的拷贝构造函数或移动构造函数是否存在, 这种方法局限性也太强。因为在用户自定义的类型中,相比有着不抛出异常的拷贝构造函数或移动构造函数的类型, 那些拷贝构造函数抛出异常并且没有移动构造函数的类型往往更多(尽管这种情况可能会随着人们习惯于C++11中的右值引用而有所改变)。然而如果这些类型不能被存储在你的线程安全的栈中,想想是多么的不幸。

少了一个字符

2.1.2 等待线程完成
如果需要等待线程,相关的std::tread实例需要使用join()

这里std::tread应该是std::thread

第二章有句话感觉没有体现原文意思

2.1.1 "It’s therefore essential that the copy behave equivalently to the original, or the result may not be what’s expected."
这里应该是说:会从原来的函数对象中拷贝构造一个给新线程,因此拷贝出来的对象应该和原来的对象相同,否则新线程里执行的结果可能跟预期不一样。还是我理解错了?

请教第五章里面的一个问题

#include
#include
#include <assert.h>

std::atomic x,y;
std::atomic z;

void write_x_then_y()
{
x.store(true,std::memory_order_relaxed); // 1
y.store(true,std::memory_order_relaxed); // 2
}
void read_y_then_x()
{
while(!y.load(std::memory_order_relaxed)); // 3
if(x.load(std::memory_order_relaxed)) // 4
++z;
}
int main()
{
x=false;
y=false;
z=0;
std::thread a(write_x_then_y);
std::thread b(read_y_then_x);
a.join();
b.join();
assert(z.load()!=0); // 5
}
这次assert⑤可能会触发,因为加载x的操作④可能读取到false,即使加载y的操作③读取到true,并且存储x的操作①先发与存储y的操作②。x和y是两个不同的变量,所以这里没有顺序去保证每个操作产生相关值的可见性。

以上的翻译来至于#include
#include
#include <assert.h>

std::atomic x,y;
std::atomic z;

void write_x_then_y()
{
x.store(true,std::memory_order_relaxed); // 1
y.store(true,std::memory_order_relaxed); // 2
}
void read_y_then_x()
{
while(!y.load(std::memory_order_relaxed)); // 3
if(x.load(std::memory_order_relaxed)) // 4
++z;
}
int main()
{
x=false;
y=false;
z=0;
std::thread a(write_x_then_y);
std::thread b(read_y_then_x);
a.join();
b.join();
assert(z.load()!=0); // 5
}
这次assert⑤可能会触发,因为加载x的操作④可能读取到false,即使加载y的操作③读取到true,并且存储x的操作①先发与存储y的操作②。x和y是两个不同的变量,所以这里没有顺序去保证每个操作产生相关值的可见性。

以上的翻译来至于Cpp_Concurrency_In_Action 原书的128页,
1,2位于一个函数里面
3,4位于另外一个函数里面
个人理解
read_y_then_x只能在write_x_then_y执行完成后,才能执行。这时x,y都是解锁的,也就是都是true,所以++z必然会执行.
但是作者说,存在着z=0的情况,请教一下,z=0的情况是怎么一种执行顺序呢,谢谢

如果程序死锁,那么主函数是不是就一直不能退出,那么这种情况下,z=0就没有意义了.

4.2.2 翻译的根本看不懂···

4.2.2 第一段,翻译的也太差了吧,最后一句“调度器仅处理 std::packaged_task<> 实例,要比处理独立的函数高效的多。”, 原文是“This abstracts out the details of the tasks; the scheduler just deals with std::packaged_task<> instances rather than individual functions”, 哪里有高效这个词了?作者也没有隐含表达高效这一特点。
2. 第二句“这可以用在构建线程池的建筑块(可见第9章),”,翻译成“构造块 结构单元”也可以啊。
3. Partial class definition竟然翻译为局部类定义!这里最起码也写成模板偏特化,或者偏特化定义。我一开始以为是嵌套类。

Now the font in the book becomes bigger, how to change it to be small ?

Hi Xiaowei,
Thanks for your hard work.
Two days ago, I downloaded pdf file, and it was 368 pages long. And Today I found you update the preface, and I download pdf file, and found it now it is 569 pages.
I wonder where to make the change so that people can download the file with small number of pages (so that it can be printed out) ?
Thanks.

请教一下有关atomic的执行顺序(5.3.3 原子操作的内存顺序 一节)

hi all:
在如下的代码中
std::atomic x,y;
std::atomic z;

void write_x_then_y()
{
x.store(true,std::memory_order_relaxed); // 1
y.store(true,std::memory_order_relaxed); // 2
}
void read_y_then_x()
{
while(!y.load(std::memory_order_relaxed)); // 3
if(x.load(std::memory_order_relaxed)) // 4
++z;
}
int main()
{
x=false;
y=false;
z=0;
std::thread a(write_x_then_y); //6
std::thread b(read_y_then_x); //7
a.join();
b.join();
assert(z.load()!=0); // 5
}
为什么 5会被执行呢?
6,7是两个独立的线程。 在同一个线程内,执行的顺序是一定的。
如果7先执行, 那么会阻塞在步骤3. y=false.
如果是6先执行,那么x,y两个读取的值,也是先x,后y。
请问,再什么样的执行情况下,会满足z=0,
谢谢

第五章 <C++内存模型与原子操作> 有句话读不通

第五章 C++内存模型与原子操作‘了解自由排序’这一小节的第二段的这这句话
“如果你让他谢谢一个值,并且随后再问他要一个值,他要不就给你你刚告诉他的那个值,要不就是一个列表下端的值。”

这句的里面的“谢谢”应该打错了吧?

中文字体

请问作者pdf中文翻译中 正文中的中文作者使用的是什么字体?

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.