`
hqs7636
  • 浏览: 223750 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Multithreading Tutorial: Globals 4

阅读更多
June 23, 2009


Posted by Bartosz Milewski under C++, Concurrency, D Programming Language, Java, Multithreading, Programming, Type System
1 Comment

If it weren’t for the multitude of opportunities to shoot yourself in the foot, multithreaded programming would be easy. I’m going to discuss some of these “opportunities” in relation to global variables. I’ll talk about general issues and discuss the ways compilers can detect them. In particular, I’ll show the protections provided by my proposed extensions to the type system.
Global Variables

There are so many ways the sharing of global variables between threads can go wrong, it’s scary.

Let me start with the simplest example: the declaration of a global object of class Foo (in an unspecified language with Java-like syntax).
Foo TheFoo = new Foo;

In C++ or Java, TheFoo would immediately be visible to all threads, even if Foo provided no synchronization whatsoever (strictly speaking Java doesn’t have global variables, but static data members play the same role).

If the programmer doesn’t do anything to protect shared data, the default immediately exposes her to data races.

The D programming language (version 2.0, also known as D2) makes a better choice–global variables are, by default, thread local. That takes away the danger of accidental sharing. If the programmer wants to share a global variable, she has to declare it as such:
shared Foo TheFoo = new shared Foo;

It’s still up to the designer of the class Foo to provide appropriate synchronization.

Currently, the only multithreaded guarantee for shared objects in D2 is the absence of low-level data races on multiprocessors–and even that, only in the safe subset of D. What are low level data races? Those are the races that break some lock-free algorithms, like the infamous Double-Checked Locking Pattern. If I were to explain this to a Java programmer, I’d say that all data members in a shared object are volatile. This property propagates transitively to all objects the current object has access to.

Still, the following implementation of a shared object in D would most likely be incorrect even with the absence of low-level data races:
class Foo {
    private int[] _arr;
    public void append(int i) {
       _arr ~= i; // array append
    }
}

auto TheFoo = new shared Foo;

The problem is that an array in D has two fields: the length and the pointer to a buffer. In shared Foo, each of them would be updated atomically, but the duo would not. So two threads calling TheFoo.append could interleave their updates in an unpredictable way, possibly leading to loss of data.

My race-free type system goes further–it eliminates all data races, both low- and high-level. The same code would work differently in my scheme. When an object is declared shared, all its methods are automatically synchronized. TheFoo.append would take Foo’s lock and make the whole append operation atomic. (For the advanced programmer who wants to implement lock-free algorithms my scheme offers a special lockfree qualifier, which I’ll describe shortly.)

Now suppose that you were cautious enough to design your Java/D2 class Foo to be thread safe:
class Foo {
    private int [] _arr;
    public synchronized void append(int i) {
       _arr ~= i; // array append
    }
}

Does it mean your global variable, TheFoo, is safe to use? Not in Java. Consider this:
static Foo TheFoo; // static = global
// Thread 1
TheFoo = new Foo();
// Thread 2
while (TheFoo == null)
    continue;
TheFoo.append(1);

You won’t even know what hit you when your program fails. I will direct the reader to one of my older posts that explains the problems of publication safety on a multiprocessor machine. The bottom line is that, in order to make your program work correctly in Java, you have to declare TheFoo as volatile (or final, if you simply want to prevent such usage). Again, it looks like in Java the defaults are stacked against safe multithreading.

This is not a problem in D2, since shared implies volatile.

In my scheme, the default behavior of shared is different. It works like Java’s final. The code that tries to rebind the shared object (re-assign to the handle) would not compile. This is to prevent accidental lock-free programming. (If you haven’t noticed, the code that waits on the handle of TheFoo to switch from null to non-null is lock-free. The handle is not protected by any lock.) Unlike D2, I don’t want to make lock-free programming “easy,” because it isn’t easy. It’s almost like D2 is endorsing lock-free programming by giving the programmer a false sense of security.

So what do you do if you really want to spin on the handle? You declare your object lockfree.
lockfree Foo TheFoo;

lockfree implies shared (it doesn’t make sense otherwise), but it also makes the handle “volatile”. All accesses to it will be made sequentially consistent (on the x86, it means all stores will compile to xchg).

Note that lockfree is shallow–data members of TheFoo don’t inherit the lockfree qualification. Instead, they inherit the implied shared property of TheFoo.

It’s not only object handles that can be made lockfree. Other atomic data types like integers, Booleans, etc., can also be lockfree. A lockfree struct is also possible–it is treated as a tuple whose all elements are lockfree. There is no atomicity guarantee for the whole struct. Methods can be declared lockfree to turn off default synchronization.
Conclusion

Even the simplest case of sharing a global variable between threads is fraught with danger. My proposal inobtrusively eliminates most common traps. The defaults are carefully chosen to let the beginners avoid the pitfalls of multithreaded programming.
分享到:
评论

相关推荐

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    vue.js v2.5.17

    vue.js vue.min.js vue-router.js vue-router.min.js

    DM8-SQL语言详解及其数据管理和查询操作指南

    内容概要:本文档是关于DM8数据库系统的SQL语言使用手册,全面介绍了其SQL语言的基础特性、功能、语法规则及相关使用方法。手册首先概述了DM_SQL的特点和它支持的各种数据类型(例如:数值、字符串、日期时间类型等)及其对应的表达式。接下来深入探讨了一系列高级话题,涵盖数据定义语句-DDL、数据操纵语句-DML和数据控制语句,具体讲解了多种表类型(常规表、HUGE表、外部表)的创建与管理,以及索引机制(全文索引、位图连接索引等)。此外还提供了丰富的实例示范,确保读者能直观理解并应用于实际项目。同时,文档也阐述了各种系统级别的功能,如日志和检查点管理、MPP管理和统计信息生成等功能的使用方法。 适合人群:具有一定数据库基础知识并且有意深入了解DM8数据库系统特性的开发工程师、数据库管理人员或相关专业技术人员。 使用场景及目标:①指导开发人员掌握DM8中各类SQL命令的实际运用技巧;②帮助运维人员学会通过SQL来进行有效的数据维护与优化,从而提升数据库的整体性能。 其他说明:该手册不仅仅是SQL理论的讲述,而是通过大量的实例演示让使用者更加熟悉日常的工作任务。对于复杂的企业级应用场景尤其有

    1108_ba_open_report.pdf

    1108_ba_open_report

    anslow_02_0109.pdf

    anslow_02_0109

    以下是OpenCV在不同操作系统下的下载与安装教程

    opencv下载安装教程

    aronson_01_0707.pdf

    aronson_01_0707

    Designing Deep Learning Systems. A software engineer's guide - 2023.pdf

    Wang Chi, Szeto Donald - Designing Deep Learning Systems. A software engineer's guide

    基于豆瓣图书网站的图书数据分析与可视化

    使用Python语言对Django框架进行设计,选用豆瓣读书网站(https://book.douba n.com/)作为研究对象,基于用户的阅读行为数据,运用网络爬虫技术来抓取所需数据,随后对这些数据进行深度清理,存储到数据库中。借助ECharts的可视化工具,深入分析和直观展示,实现数据分析与可视化。

    barbieri_01_0108.pdf

    barbieri_01_0108

    brown_3ck_01_0718.pdf

    brown_3ck_01_0718

    基于Python的Django-vue学生选课系统实现源码-说明文档-演示视频.zip

    关键词:学生选课系统;Python语言;MySQL数据库 学生选课系统采用B/S架构,数据库是MySQL。网站的搭建与开发采用了先进的Python进行编写,使用了Django框架。该系统从三个对象:由管理员和学生、教师来对系统进行设计构建。主要功能包括:个人信息修改,对学生、教师信息、课程信息、课程分类、选择课程、班级、成绩通知、教室信息、系统管理等功能

    ganga_02_0909.pdf

    ganga_02_0909

    毕设-springboot大学生竞赛管理系统(免费领取)

    毕设-springboot大学生竞赛管理系统(免费领取)

    agenda_3cd_01_0716.pdf

    agenda_3cd_01_0716

    Swift语言教程:从入门到实践 Swift是苹果公司开发的一种多范式编程语言,用于iOS、macOS、watchOS和tvOS应用开发 它结合了C和Objective-C的优点,同时提供了现代编程语

    Swift语言教程:从入门到实践 Swift是苹果公司开发的一种多范式编程语言,用于iOS、macOS、watchOS和tvOS应用开发。它结合了C和Objective-C的优点,同时提供了现代编程语言的许多特性,如安全性、速度以及表达力。以下是从入门到实践的Swift语言教程。 一、Swift基础 1. Swift环境设置 Xcode安装:下载并安装最新版本的Xcode,这是开发Swift应用的集成开发环境(IDE)。 创建项目:在Xcode中创建一个新的Swift项目,了解项目结构。 2. 基本语法 变量与常量:使用var声明变量,使用let声明常量。 数据类型:整数(Int)、浮点数(DoubleFloat)、字符串(String)、布尔值(Bool)等。 类型安全:Swift是强类型语言,每个变量和常量在声明时都需要指定类型(尽管Swift也能自动推断类型)。 运算符:算术运算符、比较运算符、逻辑运算符等。 3. 控制流 条件语句:if、else if、else。 循环语句:for循环、while循环、repeat-while循环。 控制转移语句:break、continue

    【宝城期货-2025研报】钢材、铁矿石日报:关税扰动不断,钢矿弱势运行.pdf

    【宝城期货-2025研报】钢材、铁矿石日报:关税扰动不断,钢矿弱势运行.pdf

    anslow_05_0110.pdf

    anslow_05_0110

    c盘满了怎么清理.zip

    介绍了清理C盘空间的多种方法,包括使用系统工具、清理临时文件、卸载残留文件、移动用户文件夹、清理系统日志和虚拟内存等,旨在帮助用户有效释放C盘空间,提升电脑性能。

    达梦DM8分布式计算集群的技术架构与管理指南

    内容概要:本文档详细介绍达梦DM8分布计算集群(DMDPC)的设计理念、系统架构、关键技术及其实现。涵盖了系统原理、系统特性、关键技术、构建和配置步骤等内容。文档还提供了对计划生成、子任务划分、执行调度及链路通讯等机制的深入解析,确保使用者理解如何部署、管理和优化这个新型分布式数据库解决方案。 适合人群:本文档适用于熟悉数据库技术尤其是分布式数据库的专业人士,主要包括开发工程师、测试工程师、技术支持工程师、数据库管理员及相关管理人员。 使用场景及目标:①理解并掌握 DM8 分布计算集群的基本概念和技术原理;②指导如何正确配置、部署以及日常维护DM8分布式集群;③为解决复杂的大型企业级应用程序对高性能和高可靠的数据库需求提供技术支持。 其他说明:文档不仅讲述了技术层面的知识,还有详细的实例示范,便于用户根据自己的情况进行操作,有助于加快项目的实施进度。此外,文档还包括了丰富的配置参数和性能调优方面的内容,为优化系统性能提供了有价值的参考资料。

Global site tag (gtag.js) - Google Analytics