Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

117.4.3 Volatile fields

2When a field-declaration includes a volatile modifier, the fields introduced by that declaration are

3volatile fields. For non-volatile fields, optimization techniques that reorder instructions can lead to

4unexpected and unpredictable results in multi-threaded programs that access fields without synchronization

5such as that provided by the lock-statement (§15.12). These optimizations can be performed by the compiler,

6by the runtime system, or by hardware. For volatile fields, such reordering optimizations are restricted:

7A read of a volatile field is called a volatile read. A volatile read has “acquire semantics”; that is, it is

8guaranteed to occur prior to any references to memory that occur after it in the instruction sequence.

9A write of a volatile field is called a volatile write. A volatile write has “release semantics”; that is, it is

10guaranteed to happen after any memory references prior to the write instruction in the instruction

11sequence.

12These restrictions ensure that all threads will observe volatile writes performed by any other thread in the

13order in which they were performed. A conforming implementation is not required to provide a single total

14ordering of volatile writes as seen from all threads of execution. The type of a volatile field shall be one of

15the following:

16A reference-type.

17A type-parameter that is known to be a reference type (§26.7).

18The type byte, sbyte, short, ushort, int, uint, char, float, or bool.

19An enum-type having an enum base type of byte, sbyte, short, ushort, int, or uint.

20[Example: The example

21using System;

22using System.Threading;

23class Test

24{

25

public static int result;

26

public static volatile bool finished;

27

static void Thread2() {

28

result = 143;

29

finished = true;

30

}

31

static void Main() {

32

finished = false;

33

// Run Thread2() in a new thread

34

new Thread(new ThreadStart(Thread2)).Start();

35

// Wait for Thread2 to signal that it has a result by setting

36

// finished to true.

37

for (;;) {

38

if (finished) {

39

Console.WriteLine("result = {0}", result);

40

return;

41

}

42

}

43}

44}

45produces the output:

46result = 143

47In this example, the method Main starts a new thread that runs the method Thread2. This method stores a

48value into a non-volatile field called result, then stores true in the volatile field finished. The main

49thread waits for the field finished to be set to true, then reads the field result. Since finished has

50been declared volatile, the main thread shall read the value 143 from the field result. If the field

51finished had not been declared volatile, then it would be permissible for the store to result to be

52visible to the main thread after the store to finished, and hence for the main thread to read the value 0

274

Соседние файлы в предмете Электротехника