[资料] Multithreading with D by Bartosz Milewski(转)

Sam1860 2009-04-28
http://bartoszmilewski.wordpress.com/2009/04/26/multithreading-with-d/

因为要爬墙才能访问,就直接转过来了

引用

Most languages were caught unaware by the multicore revolution. C++, predictably, developed a portable assembly language for multiprocessors (C++0x atomics). Out of sheer desperation some programmers turned to Erlang. Many still try to ignore the elephant in the room, while others try to shoot it with BB guns provided by their favorite languages.

The D programming language has the unique opportunity to build multithreading model from the ground up. If you’ve been following my blog, you might have noticed that I’d been reading a lot of literature describing various systematic approaches to safe multithreading using the type system. These ware my preparations for the task of designing a threading model for D–with the help of Walter Bright (the creator of D), Andrei Alexandrescu (the lead architect of D2), and Sean Kelly (contributor to various D libraries).
Goal 1: Making concurrent programming in D safer

I propose to do this by substantially extending the type system. I realize that there is strong resistance to extending a language’s type system (and D had it’s own share of struggle when introducing deep const and immutable). The main problem is increased complexity. The programmers are reluctant to study and use complex annotation schemes. I will argue that, in the case of multithreading, the complexity argument is reversed. Shared-memory multithreading is hard! Figuring out how to avoid all the pitfalls of sharing is harder than learning a type system that prevents them.

Would you let somebody who cannot read safety instructions operate a nuclear reactor?
Goal 2: Limiting complexity

To limit the complexity of the type system, I came up with several simple principles. The most important one is that multithreaded extensions should not require modifications to single-threaded programs. That means coming up with reasonable defaults for all new annotations.

If necessary, the compiler should be able to derive some of the defaults from program analysis. Because modularity is very important in D, such analysis should not creep across module boundaries. Whole-program analysis is out of the question.
Goal 3: Keeping goals reasonable

There are two major challenges in multithreaded programming:

   1. Avoiding races
   2. Preventing deadlocks

The first problem is approachable, whereas the second one seems to be a pie in the sky. I will therefore concentrate on making D race-free.

Since D is closely related to C++ (it has pointers, unchecked casts, and the option to use manual memory management), even that goal has to be limited in scope. There is a memory-safe subset of D called SafeD, for which race freedom seems to be within reach, so this will be my starting point. (Hopefully, ne day somebody might even come up with operational semantics for SafeD and prove its race freedom.)

The system would be incomplete without some support for lock-free programming. There is a consensus that D should enforce sequential consistency. For comparison, C++ committee, after a long struggle, decided to allow non-SC primitives (weak atomics). Java only enforces SC for volatile variables.
Lessons learned

There are many commonalities in various approaches to race freedom.

    * Ownership should be built into the type system. In OO languages each object must have an owner. In C-like languages each piece of data must have an associated lock.
    * There should be efficient mechanisms for passing data between threads
          o By value
          o By reference. (The object being passed must be a monitor.)
          o As immutable data, by const reference
          o By unique reference using move semantics (hard to do in C-like languages)

Most of those ideas are expressible through type qualifiers. D already has its share of type qualifiers, such as const or immutable. There is also a proposal to introduce shared, for non-thread-local objects. Large number of qualifiers often leads to code duplication (like having two versions of vector: one shared, one thread-local).

The problem of code duplication can be solved by qualifier polymorphism–the ability to use qualifiers as template parameters.

I will discuss each issue separately and in more detail in my future posts.
Higher-level models

Explicit synchronization is hard, no doubt about it. In D, the type system is supposed to make it safe. There are however other models of concurrency that, although more restrictive, are often easier to use.

One such model is based on message passing. D’s support for message passing will be library-based. The race-free type system will make it safe and flexible. It will, for instance, support passing messages not only by value but also by reference–without sacrificing safety. In traditional type systems there is no way to express the requirement that a message passed by reference must either be a monitor itself or behave like a C++ unique_ptr (an object that leaves no aliases behind). This requirement should be expressible in D, allowing the compiler to check for its violations.

I’ve been paying a lot of attention to software transactional memory (STM). I believe that STM could be built into D–again, with the support of the type system. It seems like the hardest problem with STM is how it should interoperate with other types of multithreaded access (both locked and lock-free).

The simplest solution is to enforce full isolation–no STM object can ever be accessed outside of a transaction. It’s not clear how practical such approach is, but it definitely simplifies things to the point that STM becomes orthogonal to other types of synchronization. And that means that it will be possible to tackle it at a later time, after the race-free multithreading model is firmly established.

In the next installment, I will describe the ownership scheme that is the foundation of the race-free type system for D.
betty_betty2008 2009-04-28
三大牛之一! 谢谢!

Bartosz Milewski,C++ In Action 的作者。是Reliable Software公司的总裁,Reliable Software公司是一家为程序员制造高质量开发工具的公司。在过去几年间,Bartosz Milewski在多家知名杂志发表了大量技术文章。在微软工程工作的8年期间,他担任Windows2000中Content Index组件的开发主管,他曾经在波兰的Wroclaw大学讲授C++编程课程,而且他获得了Wroclaw大学的理论物理学博士学位。
http://www.bartosz.com/
http://www.relisoft.com/
个人简历:
Bartosz Milewski's Resume
Education
1971-77--Physics, University of Warsaw and University of Wroclaw, Poland
1977-80--Graduate Study at the Institute for Theoretical Physics, University of Wroclaw
1990-91--Non-matriculated courses in Electrical Engineering and Computer Science at the University of Washington in Seattle
Employment
1987-95--Software Design Engineer at Microsoft, Redmond, WA
1997-2006--President of Reliable Software, Seattle, WA
Skills
Expertise in C++
Software design and architecture
Familiarity with a number of programming languages (Lisp, Smalltalk, Java, C++/CLI)
Windows API programming--library design and implementation
Academic History
1981--Ph.D. degree in theoretical physics from the University of Wroclaw, Poland
1981-87--Various post-doctoral research positions in physics
Max-Planck-Institute, Munich, Germany
CERN, Geneva, Switzerland
University of Utrecht, The Netherlands
Brendais University, Waltham, MA, USA
University of California, Davis
Teaching Experience
1980-84--Undergraduate and graduate courses in physics, University of Wroclaw, Poland
1994--Programming courses: Conversational C++, Industrial Strength Programming, and Programming Workshop--as a visiting professor at the University of Wroclaw, Poland (on leave of absence from Microsoft)
1996--Advanced C++--as a visiting professor at the University of Wroclaw.
1998--Computer Graphics at the DigiPen Institute of Technology, Redmond, WA
Programming Experience
Microsoft
Porting the Mach operating system to x86 and rewriting it in C++ (responsible for the virtual memory subsystem and the kernel debugger)
Work on an object-oriented Windows shell
Design and implementation of the content index, later used as a search engine at MSN and now the basis of Desktop Search in Windows Vista
Reliable Software
Design and implementation of a distributed P2P version control system, Code Co-op
Management Experience
Microsoft: Development lead of the Content Index team
Reliable Software: President
Publications
Supersymmetry and Supergravity 1983, Proceedings of the XIXth Winter School and Workshop of Theoretical Physics, Karpacz, Poland--editor
C++ In Action, Industrial Strength Programming in C++, Addison-Wesley, 2001
Articles
Over 20 publications in physics journals, including Nuclear Physics B, Physics Letters, and Classical and Quantum Gravity.
C++ articles in Journal of Object-Oriented Programming, C++ Report, Dr Dobb's Journal, and C/C++ Users Journal (with Andrei Alexandrescu)
Conferences
The Director of the XIXth Winters School and Workshop of Theoretical Physics, Karpacz, Poland, 1983
Distributed Source Control System, in Raidar Conradi, ed., Software Configuration Management, ICSE'97 SCM-7 Workshop, Proceedings, Springer 1997
Series of talks at SIGS conferences (C++ World, Object Expo) on Resource Management in C++
Interests
Software Complexity
Programming Methodologies
Complexity Theory
Quantum Physics and Astrophysics



csdxmlnet 2009-04-28
我英文不好,看不懂,可以用中文大概说下一下吗?
night_stalker 2009-04-28
未来: immutable 和 STM ?
hqs7636 2009-04-28
csdxmlnet 写道
我英文不好,看不懂,可以用中文大概说下一下吗?


多核并发

只是没看到这位老兄提交过什么东西啊,还是他自己另搞了一套?
betty_betty2008 2009-04-28
hqs7636 写道
csdxmlnet 写道
我英文不好,看不懂,可以用中文大概说下一下吗?


多核并发

只是没看到这位老兄提交过什么东西啊,还是他自己另搞了一套?

他们现在是三人帮,算上Sean就是四人了。
http://www.bartosz.com/D/STM_pptx_files/frame.htm
http://www.relisoft.com/forums/index.php?showforum=13
csdxmlnet 2009-04-29
hqs7636 写道
csdxmlnet 写道
我英文不好,看不懂,可以用中文大概说下一下吗?


多核并发

只是没看到这位老兄提交过什么东西啊,还是他自己另搞了一套?


谢谢!
hqs7636 2009-05-03
源码在哪???

是属于语言部分还是库部分?

找到一点介绍:
http://www.relisoft.com/forums/index.php?s=fceb037d0650aa96b5a9f1c4f306da23&showtopic=1137
Global site tag (gtag.js) - Google Analytics