IBM AS/400 manual to Avoid Deadlock, Multithreading Considerations, Modules

Models: AS/400

1 489
Download 489 pages 47.95 Kb
Page 183
Image 183
to Avoid Deadlock

Multithreading Considerations

*-----------------------------------------------------------------------------------

 

* .---------------

.

 

*

 

* some storage

<----------------

pointer to shared storage

*

(called MyPtr in module A)

* '---------------

'

(saved as a static variable in module B)

*Module A

*Global variables in Module A

D MyPtr

 

S

*

 

 

D SomeStorage

S

10A

based(MyPtr)

C

eval

 

SomeStorage =

'Init value'

 

C

callp

 

ProcB(MyPtr)

 

.2/

C

eval

 

SomeStorage =

*BLANKS

.3/

*-----------------------------------------------------------------------------------

 

 

 

 

*Module B

*Global variables in Module B

D SavedPtr

S

 

*

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D SomeStorage

S

 

10A

based(SavedPtr)

 

 

 

 

 

 

 

 

 

 

* ProcB in module B

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

P ProcB

 

B

 

 

export

 

 

 

 

 

 

 

 

 

 

 

 

 

D ProcB

 

PI

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D

PtrParm

 

 

*

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C

 

 

 

eval

SavedPtr = PtrParm

 

 

 

.6/

 

 

 

 

 

 

 

 

C

 

 

 

return

 

 

 

 

 

 

 

.7/

 

 

 

 

 

 

 

 

P

 

 

 

E

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

* ProcB2 in module B

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

P ProcB2

 

B

 

 

export

 

 

 

 

 

 

 

 

 

 

 

 

 

D ProcB2

 

PI

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D

PtrParm

 

 

*

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C

 

 

 

if

 

SomeStorage = 'Init value' .8/

 

 

 

 

 

 

 

 

C

 

 

 

....

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C

 

 

 

return

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

P

 

 

 

E

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure

73. Example of Sharing Data in a Multithreaded Environment

 

 

 

 

 

 

 

When

ProcA

calls

ProcB .2/(line),no

other

 

thread can access the storage

to

by

MyPtr, since

both module

A

and

and module

B

are

being

used

ProcB

saves

the pointer in module B's static.6/) andstoragereturns(line(line

 

 

.7/). Now, no

thread

is

active

in

module

B,

so another thread is fr

B.

If

another

thread

calls

ProcB2,

 

it

is

possible

that

the

first

line.3/ before,

at

the

same

time,

or

after

the second.8/. thread proc

The

 

order

of these events is not

defined;

the

code

used

to

tes

'Init

value'

could succeed one

time

 

and fail the next time.

 

 

You

 

can

synchronize

access

to

the

shared

data,

by

using logic

in

using synchronization techniques provided by C or by platform functio details, refer to the Multithreaded Applications document under the topic at the following URL:

http://www.as400.ibm.com/infocenter/

How

to Avoid Deadlock

Between

Modules

 

 

 

 

 

 

 

 

In

some

situations,

it may be necessary for you to control

the

sy

modules using facilities other than the THREAD(*SERIALIZE) control speci

keyword.

For

example, consider the situation where two procedures

a

called

at the

same

time:

PROC1 and

PROC3. Even

though there

is

no

a

sive

calling;

if

PROC1 calls PROC4,

it

will wait for MOD2 to

unlock;

an

calls

PROC2,

it

will

wait

for MOD1

to

unlock. The

procedures

will

not

Chapter 10. Calling Programs and Procedures159

Page 183
Image 183
IBM AS/400 manual to Avoid Deadlock, Multithreading Considerations, Modules