1
2
3
4
|
XBYTE[2]=0x55;
XBYTE[2]=0x56;
XBYTE[2]=0x57;
XBYTE[2]=0x58;
|
1
2
3
4
|
int
square(
volatile
int
*ptr)
{
return
((*ptr) * (*ptr));
}
|
1
2
3
4
5
6
7
|
int
square(
volatile
int
* &ptr)
//這裏參數應該申明爲引用,否則函數體裏只會使用副本,外部無法更改
{
int
a,b;
a = *ptr;
b = *ptr;
return
a*b;
}
|
1
2
3
4
5
6
|
long
square(
volatile
int
*ptr)
{
int
a;
a = *ptr;
return
a*a;
}
|
1
|
volatile
int
vint;
|
1
2
3
|
volatile
int
i=10;
int
a=i;
//...
|
1
|
int
b=i;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include<stdio.h>
void
main(
int
argc,
char
*argv[])
{
int
i = 10;
int
a = i;
printf
(
"i=%d"
,a);
//下面彙編語句的做用就是改變內存中i的值,可是又不讓編譯器知道
__asm
{
mov dword ptr[ebp-4],20h
}
int
b = i;
printf
(
"i=%d"
,b);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include<stdio.h>
void
main(
int
argc,
char
*argv[])
{
volatile
int
i = 10;
int
a = i;
printf
(
"i=%d"
,a);
__asm
{
` mov dword ptr[ebp-4],20h
}
int
b = i;
printf
(
"i=%d"
,b);
}
|
1
|
for
(
int
i=0; i<100000; i++);
|
1
|
for
(
volatile
int
i=0; i<100000; i++);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
static
int
i = 0;
int
main(
void
)
{
//...
while
(1)
{
if
(i)
dosomething();
}
}
/*Interruptserviceroutine.*/
void
ISR_2(
void
)
{
i=1;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
classGadget
{
public
:
void
Wait()
{
while
(!flag_)
{
Sleep(1000);
//sleeps for 1000milli seconds
}
}
void
Wakeup()
{
flag_=
true
;
}
//...
private
:
bool
flag_;
};
|
1
2
3
4
5
6
7
|
class
Gadget
{
public
:
//...as above...
private
:
volatile
bool
flag_;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@NotThreadSafe
public
class
NumberRange{
private
int
lower,upper;
public
int
getLower(){
return
lower;
}
public
int
getUpper(){
return
upper;
}
public
void
setLower(
int
value){
if
(value > upper)
throw
new
IllegalArgumentException(...);
lower = value;
}
public
void
setUpper(
int
value){
if
(value < lower)
throw
new
IllegalArgumentException(...);
upper = value;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
volatile
boolean shutdownRequested;
...
public
void
shutdown()
{
shutdownRequested=
true
;
}
public
void
doWork()
{
while
(!shutdownRequested)
{
//dostuff
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
class
BackgroundFloobleLoader{
public
volatile
Flooble theFlooble;
public
void
initInBackground(){
//dolotsofstuff
theFlooble = newFlooble();
//this is the only write to theFlooble
}
}
public
class
SomeOtherClass{
public
void
doWork(){
while
(
true
){
//dosomestuff...
//usetheFlooble,butonlyifitisready
if
(floobleLoader.theFlooble!=
null
)doSomething(floobleLoader.theFlooble);
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
UserManager{
public
volatile
String lastUser;
public
boolean
authenticate(String user, String password){
boolean
valid = passwordIsValid(user, password);
if
(valid){
User u =
new
User();
activeUsers.add(u);
lastUser = user;
}
return
valid;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@ThreadSafe
public
class
Person{
private
volatile
String firstName;
private
volatile
String lastName;
private
volatile
intage;
public
String getFirstName(){
return
firstName;
}
public
String getLastName(){
return
lastName;
}
public
int
getAge(){
return
age;
}
public
void
setFirstName(String firstName){
this
.firstName = firstName;
}
public
void
setLastName(String lastName){
this
.lastName = lastName;
}
public
void
setAge(
int
age){
this
.age = age;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@ThreadSafe
public
class
CheesyCounter{
//Employs the cheap read-write lock trick
//All mutative operations MUST be done with the 'this' lock held
@GuardedBy
(
"this"
)
private
volatile
int
value;
public
int
getValue(){
return
value;
}
public
synchronized
int
increment(){
return
value++;
}
}
|