Download as pdf or txt
Download as pdf or txt
You are on page 1of 219

Concurrency and

Networking
(or, in…)

From the Internet to the IoT - The Fundamentals of Modern


Computer Networking

ÉCOLE POLYTECHNIQUE –
CONCURRENCY AND 

(OR, IN...) NETWORKING

TODAY’S OBJECTIVES
‣ Recall the essence of last weeks lecture.

‣ Understand concurrency
‣ Principles
‣ Primitives
‣ And, how it works in Java

‣ Experiment two common “concurrent architectures” when writing


communicating programs

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !57
CONCURRENCY AND 

(OR, IN...) NETWORKING

AGENDA
‣ The Essence of Last Week:

Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives

(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)

‣ (Some) Threaded Architectures

École Polytechnique • INF557: Fromthomas@thomasclausen.org


the Internet to the IoT • The Fundamentals of Modern Computer Networking !58
http://www.thomasclausen.org
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

The
Big Bad
Internet

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

The
Big Bad
Internet

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

The IPv6 IPv4

Big Bad
Internet

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

The IPv6 IPv4

Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

… TCP UDP. …

The IPv6 IPv4

Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

… TCP UDP. …

The IPv6 IPv4 Protocol Number


ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

… IMAP HTTP SMTP IRC …

… TCP UDP. …

The IPv6 IPv4 Protocol Number


ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45

… IMAP HTTP SMTP IRC …

… TCP UDP. … Port Number


IMAP=143, HTTP=80, SMTP=25 ......
The IPv6 IPv4 Protocol Number
ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45
something.asp
script.cgi test.php filename.html
… IMAP HTTP SMTP IRC …

… TCP UDP. … Port Number


IMAP=143, HTTP=80, SMTP=25 ......
The IPv6 IPv4 Protocol Number
ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45
something.asp
script.cgi test.php filename.html
… IMAP HTTP SMTP IRC …
App specific
e.g. filenames, usernames, …
… TCP UDP. … Port Number
IMAP=143, HTTP=80, SMTP=25 ......
The IPv6 IPv4 Protocol Number
ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?

192.168.23.45
something.asp
script.cgi test.php filename.html
… IMAP HTTP SMTP IRC …
App specific
e.g. filenames, usernames, …
… TCP UDP. … Port Number
IMAP=143, HTTP=80, SMTP=25 ......
The IPv6 IPv4 Protocol Number
ICMP=1, TCP=6, UDP=17 ......
Big Bad
Internet Ethertype

To identify, unambiguously, one single resource on the Internet


École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !59
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?
To identify, unambiguously, one single resource on the Internet

The need to do this (identify, reach, ressources) efficiently remains a major design
challenge

(e.g., remains largely unresolved in IoT, or even in the Internet as per NAPT)

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?
To identify, unambiguously, one single resource on the Internet
something.asp
test.php
script.cgi filename.html
… IMAP HTTP SMTP IRC …

… TCP UDP. …

Network

The need to do this (identify, reach, ressources) efficiently remains a major design
challenge

(e.g., remains largely unresolved in IoT, or even in the Internet as per NAPT)

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?
To identify, unambiguously, one single resource on the Internet
• We usually stop before “application specific”

demultiplexing, when talking about Internet protocols

and “identify a single process on the Internet” as:
 … IMAP HTTP SMTP IRC …

… TCP UDP. …
• IP Address (implies IPv4/IPv6)
• Protocol number Network

• Port number

The need to do this (identify, reach, ressources) efficiently remains a major design
challenge

(e.g., remains largely unresolved in IoT, or even in the Internet as per NAPT)

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND 

(OR, IN...) NETWORKING

QUESTION (THE ESSENCE OF LAST WEEK)


‣ What was the purpose of a name/an identifier?
‣ What does http://www.example.com.:80/path/filename.html tell us?
To identify, unambiguously, one single resource on the Internet
• We usually stop before “application specific”

demultiplexing, when talking about Internet protocols

and “identify a single process on the Internet” as:
 … IMAP HTTP SMTP IRC …

… TCP UDP. …
• IP Address (implies IPv4/IPv6)
• Protocol number Network

• Port number

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !60
CONCURRENCY AND 

(OR, IN...) NETWORKING

AGENDA
‣ The Essence of Last Week:

Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives

(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)

‣ (Some) Threaded Architectures

École Polytechnique • INF557: Fromthomas@thomasclausen.org


the Internet to the IoT • The Fundamentals of Modern Computer Networking !61
http://www.thomasclausen.org
CONCURRENCY AND 

(OR, IN...) NETWORKING

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !62
CONCURRENCY AND 

(OR, IN...) NETWORKING

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !62
CONCURRENCY AND 

(OR, IN...) NETWORKING

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND 

(OR, IN...) NETWORKING

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system
‣ May execute his/her actions at the same time

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !63
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !64
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
?

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !64
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
‣ And, on the?order in which they are executed

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !64
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !65
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Is part of the same [stock exchange] system

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !65
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Each trader:
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !66
CONCURRENCY AND 

(OR, IN...) NETWORKING
Buy A Buy C
Sell A Sell D
Sell A Sell D
Sell B Buy C
Sell B Sell D
Sell B Buy C

Each trader:
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !66
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Sell A 2. Sell D
3. Sell A 3. Sell D
4. Sell B 4. Buy C
5. Sell B 5. Sell D
6. Sell B 6. Buy C

Each trader:
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !67
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Sell A 2. Sell D
3. Sell A 3. Sell D
4. Sell B 4. Buy C
5. Sell B 5. Sell D
6. Sell B 6. Buy C

Each trader:
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !67
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C
2. Sell D
2. Sell A 3. Sell D
3. Sell A 4. Buy C
5. Sell D
4. Sell B
5. Sell B 6. Buy C
6. Sell B

Each trader:
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !68
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

Coffee 2. Sell D
2. Sell A 3. Sell D
3. Sell A 4. Buy C
Buy Porche 5. Sell D
4. Sell B
Call mom
5. Sell B 6. Buy C
6. Sell B
Move bonus to tax-
Each trader: paradise
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !68
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-

Each trader: paradise
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !69
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-

Each trader: paradise
‣ Sequentially executes buy/sell actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !69
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-

Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !70
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-

Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !70
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-

Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions
‣ Trivially maintain local ordering of own actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !71
CONCURRENCY AND 

(OR, IN...) NETWORKING
1. Buy A 1. Buy C

2. Coffee 2. Sell D
3. Sell A 3. Sell D
4. Sell A 4. Buy C
5. Buy Porche 5. Sell D
6. Sell B
6. Call mom
7. Sell B 7. Buy C
8. Sell B
8. Move bonus to tax-

Each trader: paradise
‣ Sequentially executes buy/sell actions
‣ Possibly interleaved by other internal actions
‣ Trivially maintain local ordering of own actions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !71
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A Buy A Buy A


Buy A Buy A Buy A
Buy A Buy A Buy A
Sell A

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !72
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A Buy A Buy A


Buy A Buy A Buy A
Buy A Buy A Buy A
Sell A

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !72
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A
Buy A
Buy A
Buy A
Buy A
Buy A

Buy A
Buy A
Buy A

Sell A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !73
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A
Buy A
Buy A
Buy A
Buy A
Buy A

Buy A
Buy A
Buy A

Sell A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !73
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A
Buy A
Buy A
Buy A
Buy A
Buy A

Buy A
Buy A
Buy A

Sell A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !73
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A
Buy A
Buy A
Sell A

Buy A
Buy A
Buy A

Buy A
Buy A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking Buy A !74
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A
Buy A
Buy A
Sell A

Buy A
Buy A
Buy A

Buy A
Buy A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking Buy A !74
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A
Buy A
Buy A
Sell A

Buy A
Buy A
Buy A

Buy A
Buy A
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking Buy A !74
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A Buy A Buy A


Buy A Buy A Buy A
Buy A Buy A Buy A
Sell A

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !75
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A Buy A Buy A


Buy A Buy A Buy A
Buy A Buy A Buy A
Sell A

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
‣ And, on the order in which they are executed

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !75
CONCURRENCY AND 

(OR, IN...) NETWORKING

Buy A Buy A Buy A


Buy A Buy A Buy A
Buy A Buy A Buy A
Sell A

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
‣ And, on the order in which they are executed

Each trader:
‣ State - causality with relevant actions of other traders

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !75
CONCURRENCY AND 

(OR, IN...) NETWORKING

RECAPITULATING.....
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Possibly interleaved by other internal actions
‣ Is part of the same [stock exchange] system
‣ State - causality with relevant actions of other traders

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
‣ And, on the order in which they are executed

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !76
CONCURRENCY AND 

(OR, IN...) NETWORKING

RECAPITULATING.....
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Possibly interleaved by other internal actions
‣ Is part of the same [stock exchange] system
‣ State - causality with relevant actions of other traders

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
‣ And, on the order in which they are executed

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !76
CONCURRENCY AND 

(OR, IN...) NETWORKING

RECAPITULATING.....
Each trader:
‣ Sequentially executes buy/sell actions
‣ Independently executes these actions
‣ Possibly interleaved by other internal actions
‣ Is part of the same [stock exchange] system
‣ State - causality with relevant actions of other traders

Stock Exchange System:


‣ State depends on the sum of the actions of

each trader
‣ And, on the order in which they are executed

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !76
CONCURRENCY AND 

(OR, IN...) NETWORKING

AGENDA
‣ The Essence of Last Week:

Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives

(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)

‣ (Some) Threaded Architectures

École Polytechnique • INF557: Fromthomas@thomasclausen.org


the Internet to the IoT • The Fundamentals of Modern Computer Networking !77
http://www.thomasclausen.org
Trader CONCURRENCY AND 

(OR, IN...) NETWORKING

Sell A
Sequential
actions

Executed in
Buy car specified
order

Buy B

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !78
Trader CONCURRENCY AND 

(OR, IN...) NETWORKING

Sell A
Sequential
actions

Executed in
Buy car specified
order

Buy B

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !78
Trader Thread CONCURRENCY AND 

(OR, IN...) NETWORKING

Sell A send (a)


Sequential
actions

Executed in
Buy car specified a=b!
order

Buy B recv (b)

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !78
ThreadCONCURRENCY AND 

(OR, IN...) NETWORKING

class Test{

public static String foo(){ send (a)


}
return “Foo”;
Sequential
actions
public static String bar(){
return “Bar”;
}
}
Executed in
specified a=b!
order
class Main{
public static void main(String[] args){
System.out.println(Test.foo()+” “+
Test.bar() );
}
recv (b)

}
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !79
CONCURRENCY AND 

(OR, IN...) NETWORKING

Program
Address Space

Variables

Objects

foo()

bar()
Functions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !80
CONCURRENCY AND 

(OR, IN...) NETWORKING

Program
Address Space

Variables

Objects

foo()

bar()
Functions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !80
CONCURRENCY AND 

(OR, IN...) NETWORKING

Each Thread: Sequential


class Test{
actions
public static String foo(){
return “Foo”; Executed in
} specified
order
public static String bar(){
return “Bar”;
}
}
class Main{
public static void main(String[] args){
CreateThreadMagic( Test.foo()+” “+ Test.bar() )
CreateThreadMagic( Test.bar()+” “+ Test.foo() )
StartBothThreads()
}

Obviously, Pseudocode
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !81
CONCURRENCY AND 

(OR, IN...) NETWORKING

Program
Address Space

Variables

Objects

foo()

bar()
Functions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !82
CONCURRENCY AND 

(OR, IN...) NETWORKING

Program
Address Space

Variables

Objects

foo()

bar()
Functions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !82
CONCURRENCY AND 

(OR, IN...) NETWORKING

Shared Address Space

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !83
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

or

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

or or

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

or or or or

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling

or or or or or

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !84
CONCURRENCY AND 

(OR, IN...) NETWORKING

Scheduling
A Thread:
An list of tasks, to be executed in sequential order.

A CPU:
or or A collection of 1-n execution units, eachor or tasks
executing orperfectly
sequentially

Scheduling:
Mapping of tasks from threads onto execution units

Observation:
Cannot assume order of execution of tasks to be known or even
deterministic.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !85
CONCURRENCY AND 

(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND 

(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND 

(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running 

(e.g. it may not right now be mapped onto an
execution unit)

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND 

(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running 

(e.g. it may not right now be mapped onto an
execution unit)

Not Runnable
Waiting for ressources
Waiting for some specific time to pass
Not waiting for something to happen in other threads

Can not be scheduled for execution…

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND 

(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running 

(e.g. it may not right now be mapped onto an
execution unit)

Not Runnable
Waiting for ressources
Waiting for some specific time to pass
Not waiting for something to happen in other threads

Can not be scheduled for execution…

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
New CONCURRENCY AND 

(OR, IN...) NETWORKING
A Thread:
An list of tasks, to be executed in sequential order.
Runnable Dead
Not waiting for ressources
Not waiting for something to happen in other threads
Can be scheduled for execution…
…but may or may not be actually running 

(e.g. it may not right now be mapped onto an
execution unit)

Not Runnable
Waiting for ressources
Waiting for some specific time to pass
Not waiting for something to happen in other threads

Can not be scheduled for execution…

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !86
CONCURRENCY AND 

(OR, IN...) NETWORKING

PRIMITIVE:
THREAD AND THREAD STATES
Thread t = new Thread (…)
t.start()
t.run()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !87
New
A Thread: CONCURRENCY AND 

An list of tasks, to be executed in sequential order. (OR, IN...) NETWORKING

Runnable Dead

Not Runnable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !88
New
Thread t = new Thread (...) CONCURRENCY AND 

(OR, IN...) NETWORKING

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !89
New
CONCURRENCY AND 

Thread t = new Thread (...) (OR, IN...) NETWORKING

t.start()

Runnable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !90
New
CONCURRENCY AND 

Thread t = new Thread (...) (OR, IN...) NETWORKING

t.start()
t.run() exits...
Runnable Dead

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !91
New
CONCURRENCY AND 

Thread t = new Thread (...) (OR, IN...) NETWORKING

t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !92
New
CONCURRENCY AND 

Thread t = new Thread (...) (OR, IN...) NETWORKING

t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running

Not Runnable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 93
New
CONCURRENCY AND 

Thread t = new Thread (...) (OR, IN...) NETWORKING

t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running

sleep(timeout)

wait(timeout)
Not Runnable join(timeout)
wait()
join()

“Synchronization”

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 94
New
CONCURRENCY AND 

Thread t = new Thread (...) (OR, IN...) NETWORKING

t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running

sleep(timeout)

wait(timeout)
Not Runnable join(timeout)
Block wait()
join()

Timed “Synchronization”
Wait

Wait

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 95
New
Thread t = new Thread (...) CONCURRENCY AND 

(OR, IN...) NETWORKING

t.start()
t.run() exits...
Runnable Dead
Scheduler
Ready yield() Running

timeout expiration
join() returns
Not Runnable
Block notify() received
“Synchronization”

Timed
Wait

Wait

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking 96
CONCURRENCY AND 

(OR, IN...) NETWORKING

CREATE, START, RUN THREADS - JAVA


class ThreadFromRunnable implements Runnable{

String s;
public ThreadFromRunnable(String t){
s = t;
}
public void run(){
while (true){ System.out.print(s); }
}
}

class RunnableExample{

public static void main(String[] args){


Thread t1 = new Thread(new ThreadFromRunnable("a"));
Thread t2 = new Thread(new ThreadFromRunnable("b"));

t1.start();
t2.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !97
CONCURRENCY AND 

(OR, IN...) NETWORKING

PRIMITIVE:
BARRIER SYNCHRONISATION
s.join()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !98
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !99
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !99
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !100
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !100
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !101
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !102
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join() s.run()
! exits

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !102
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join() s.run()
exits

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !103
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join() s.run()
exits

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !104
CONCURRENCY AND 

(OR, IN...) NETWORKING

Runnable Dead Dead Runnable

t s

Not Runnable Not Runnable

Waiting Waiting
s.join() s.run()
exits

Primitive:
“barrier synchronization”

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !104
CONCURRENCY AND 

(OR, IN...) NETWORKING

BARRIER SYNCHRONIZATION - JAVA


import java.lang.Thread;
class BarrierSync implements Runnable{
String s; Thread joinWith; int count;
public BarrierSync(String t, Thread jw, int c){
s = t; joinWith = jw ; count = c;
}
public void run(){
for (int i=0 ; i< count ; i++) {System.out.print(s);}
if (joinWith !=null){
try{ joinWith.join(); }
catch (InterruptedException e){}
System.out.println("\nAll Done");
}
}
}
class BarrierSyncSimple{
public static void main(String[] args){
Thread t1 = new Thread(new BarrierSync("a", null, 1000));
Thread t2 = new Thread(new BarrierSync("b", t1, 10));
t1.start(); t2.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !105
CONCURRENCY AND 

(OR, IN...) NETWORKING

PRIMITIVE:
MUTUAL EXCLUSION (MUTEX)
synchronised {…}

École Polytechnique • INF557: Fromthomas@thomasclausen.org


the Internet to the IoT • The Fundamentals of Modern Computer Networking !106
http://www.thomasclausen.org
CONCURRENCY AND 

(OR, IN...) NETWORKING

Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND 

(OR, IN...) NETWORKING

Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66
B: 24

x = 42 x = 0
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND 

(OR, IN...) NETWORKING

Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66
B: 24 B: 66

x = 42 x = 0 x = 0
a = x+24 b = x+24 x = 42
x = 0 x = 42 a = x+24
b = x+24 a = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND 

(OR, IN...) NETWORKING

Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
x = 0 x = 42 a = x+24 a = x+24
b = x+24 a = x+24 b = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !107
CONCURRENCY AND 

(OR, IN...) NETWORKING

Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
x = 0 x = 42 a = x+24 a = x+24
b = x+24 a = x+24 b = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !108
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66
B: 24

x = 42 x = 0
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
A: 66 A: 24
B: 66 B: 24

x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
A: 66 A: 24
B: 66 B: 24

x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A: 66 A: 24 A set of instructions, accessing one (or more) shared
B: 66 B: 24 variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A: 66 A: 24 A set of instructions, accessing one (or more) shared
B: 66 B: 24 variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].
x = 0 x = 42
x = 42 x = 0
a = x+24 a = x+24
b = x+24 b = x+24 “Synchronisation”
The act of ensuring the order of execution so as that
the performed computations produce a deterministic
result.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A set of instructions, accessing one (or more) shared
variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].

“Synchronisation”
The act of ensuring the order of execution so as that
the performed computations produce a deterministic
result.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !109
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66
B: 24

x = 42 x = 0
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A set of instructions, accessing one (or more) shared
variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND 

(OR, IN...) NETWORKING

A: 66 “Race Condition”
B: 24 Result of execution may not be deterministic when one
(or more) variable(s) is shared between two or more
threads AND at least one of the threads modifies said
x = 42 x = 0 variable(s).
a = x+24 b = x+24
x = 0 x = 42
b = x+24 a = x+24
“Critical Section”
A set of instructions, accessing one (or more) shared
variable(s), whose execution should be rendered
indivisible [in order to ensure that the performed
computations produce a deterministic result].

“Synchronisation”
The act of ensuring the order of execution so as that
the performed computations produce a deterministic
result.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !110
CONCURRENCY AND 

(OR, IN...) NETWORKING

Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
x = 0 x = 42 a = x+24 a = x+24
b = x+24 a = x+24 b = x+24 b = x+24

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !111
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !112
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !113
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
x = 42 x = 0
a = x+24 b = x+24
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !113
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false

x = 42 x = 0
a = x+24 b = x+24

b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
if (!lock){ if (!lock){
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
} }
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
if (!lock){ if (!lock){
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
}

?
}
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
if (!lock){ if (!lock){
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false
}

?
}
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !114
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false

x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false

?
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !115
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
while (!lock){} while (!lock){}
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false

?
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !115
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
while (!lock){} while (!lock){}
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false

?
b.join() }
print (“A: “+a) A: 66
print (“B: “+b) B:
}
A:
B: 24

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !115
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

(OR, IN...) NETWORKING

boolean lock = false


while (!lock){} while (!lock){}
lock = true; lock = true;
Didn’t solve the problem

Just introduced an additional critical region …

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !116
CONCURRENCY AND 

(OR, IN...) NETWORKING

boolean lock = false


while (!lock){} while (!lock){}
lock = true; lock = true;
Didn’t solve the problem

Just introduced an additional critical region …

… if this was a concurrency “theory” course, we would first 



introduce the notion of a “spinlock” here …

… but, as this is “concurrency for networking”, 



we’ll go straight to the goal …

… and introduce the notion of a MUTEX, and its Java instantiation, directly.

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !116
CONCURRENCY AND 

(OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
boolean lock = false
while (!lock){} while (!lock){}
lock = true; lock = true;
x = 42 x = 0
a = x+24 b = x+24
lock = false lock = false

?
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !117
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
while (!lock){} while (!lock){}
lock = true; Special Property of Mutex: lock = true;
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock = false time I.e., no “both read true at the same time” lock = false
problem.

?
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !118
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.

?
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !119
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.

?
b.join() }
print (“A: “+a)
print (“B: “+b)
}

A: 66 A: 66 A: 24
B: 24 B: 66 B: 24

x = 42 x = 0 x = 0 x = 42
a = x+24 b = x+24 x = 42 x = 0
École Polytechnique x = 0 From the xInternet
• INF557: = 42 a = x+24
to the IoT • The Fundamentals of Modern Computer Networking a = x+24 !119
b = x+24 a = x+24 b = x+24 b = x+24
CONCURRENCY AND 

Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.
b.join() }
print (“A: “+a)
print (“B: “+b)
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND 

Btw this is NOT actual Java (OR, IN...) NETWORKING
Thread A: Thread B:
.... ....
public void run(){ public void run(){
Mutex lock = false
lock.take() lock.take()
Special Property of Mutex:
x = 42 For any given Mutex, one and only one thread will x = 0
a = x+24 be able to execute take() and release(), at any given b = x+24
lock.release() time I.e., no “both read true at the same time” lock.release()
problem.
b.join() }
print (“A: “+a) Only one thread executes
print (“B: “+b)
}
critical region at a time
Mutual Exclusion (MUTEX)
Mechanism for ensuring proper
order-of-execution of instructions
Obviously:
Critical Regions must be identified
And, protected by take()/release()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND 

(OR, IN...) NETWORKING

Th Val
Thread A: import java.lang.Thread; (b
ut is id,
.... class ThreadA extends Thread{ ab is, bu
,w
& e in
us ho t..
illu cl
str ud
public void run(){ Thread joinWith; ate ed
lock.take() public aThread (Thread t){ ing we
s “ it a
cla s it

Ja ver
joinWith = t; ss sh
ic ow
x = 42 } MU s
a = x+24 va ,
 TE pre
Xe cis
s” el
lock.release()
b.join()
public void run(){
synchronized(MT.mutex){ MT.x = 42; MT.a = MT.x+24;}

 ak y th
in
to e pr
in evi
pt ou
print (“A: “+a) try{ joinWith.join(); } catch (InterruptedException e){} hr s
ea sl
System.out.println("A: "+MT.a); ds ide
print (“B: “+b) ) s

System.out.println("B: "+MT.b);
} }
}
Thread B:
class ThreadB extends Thread{
.... public void run(){
public void run(){ synchronized(MT.mutex){ MT.x = 0; MT.b = MT.x+24;}
}
lock.take() }
x = 0
b = x+24 class MT{
lock.release() static int x, a, b; static Object mutex = new Object();
} public static void main(String[] args){
Thread t1 = new Thread(new ThreadB());
Thread t2 = new Thread(new ThreadA(t1));
t1.start(); t2.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

synchronized(s){ synchronized(t){

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

synchronized(s){ synchronized(t){

synchronized(t){ synchronized(s){

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

synchronized(s){ synchronized(t){

Deadlock! synchronized(t){ synchronized(s){

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

synchronized(s){ synchronized(t){

Deadlock! synchronized(t){ synchronized(s){

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

synchronized(s){ synchronized(t){

Deadlock! synchronized(t){ synchronized(s){

} // releases t } // releases s
} // releases s } // release t

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

CONCURRENCY LEADS TO THREADS, THREADS LEAD TO


MUTEXES...AND MUTEXES LEAD TO…SUFFFFFFFERING….?
Object s = new Object()
Object t = new Object()

synchronized(s){ synchronized(t){

Deadlock! synchronized(t){ synchronized(s){


Executed
Never

} // releases t } // releases s
} // releases s } // release t

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !122
CONCURRENCY AND 

(OR, IN...) NETWORKING

Th Val
Thread A: import java.lang.Thread; (b
ut is id,
.... class ThreadA extends Thread{ ab is, bu
,w
& e in
us ho t..
illu cl
str ud
public void run(){ Thread joinWith; ate ed
lock.take() public aThread (Thread t){ ing we
s “ it a
cla s it

Ja ver
joinWith = t; ss sh
ic ow
x = 42 } MU s
a = x+24 va ,
 TE pre
Xe cis
s” el
lock.release()
b.join()
public void run(){
synchronized(MT.mutex){ MT.x = 42; MT.a = MT.x+24;}

 ak y th
in
to e pr
in evi
pt ou
print (“A: “+a) try{ joinWith.join(); } catch (InterruptedException e){} hr s
ea sl
System.out.println("A: "+MT.a); ds ide
print (“B: “+b) ) s

System.out.println("B: "+MT.b);
} }
}
Thread B:
class ThreadB extends Thread{
.... public void run(){
public void run(){ synchronized(MT.mutex){ MT.x = 0; MT.b = MT.x+24;}
}
lock.take() }
x = 0
b = x+24 class MT{
lock.release() static int x, a, b; static Object mutex = new Object();
} public static void main(String[] args){
Thread t1 = new Thread(new ThreadB());
Thread t2 = new Thread(new ThreadA(t1));
t1.start(); t2.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND 

(OR, IN...) NETWORKING

Th Val
import java.lang.Thread; (b
ut is id,
class ThreadA extends Thread{
,w
& e in ab is, bu
us ho t..
illu cl
str ud
Thread joinWith; ate ed
public aThread (Thread t){
s “ it a
cla s it ing we
Ja ver
ss sh
joinWith = t; ic ow
MU s
} TE pre
Xe cisva ,

s” el
public void run(){ 

ak y th
in
to e pr
synchronized(MT.mutex){ MT.x = 42; MT.a = MT.x+24;} in evi
pt ou
try{ joinWith.join(); } catch (InterruptedException e){} hr s
ea sl
System.out.println("A: "+MT.a); ds ide
) s

System.out.println("B: "+MT.b);
}
}

class ThreadB extends Thread{


public void run(){
synchronized(MT.mutex){ MT.x = 0; MT.b = MT.x+24;}
}
}

class MT{
static int x, a, b; static Object mutex = new Object();
public static void main(String[] args){
Thread t1 = new Thread(new ThreadB());
Thread t2 = new Thread(new ThreadA(t1));
t1.start(); t2.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND 

(OR, IN...) NETWORKING

Mutex / Condition
Variable/Semaphore

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND 

(OR, IN...) NETWORKING

Mutex / Condition
Variable/Semaphore
Shared Variable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND 

(OR, IN...) NETWORKING

Mutex / Condition
Variable/Semaphore
Shared Variable

Critical Regions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND 

(OR, IN...) NETWORKING

Mutex / Condition
Variable/Semaphore
Shared Variable

Disjoint from

Critical Regions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND 

(OR, IN...) NETWORKING

Mutex / Condition Object Orientation


Variable/Semaphore
Shared Variable
Data

Disjoint from

Methods
Critical Regions

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !125
CONCURRENCY AND 

(OR, IN...) NETWORKING

Monitor

mutex

Object

Condition Variable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND 

(OR, IN...) NETWORKING

Data accessible only by Monitor


method invocations (private…)
mutex

Object

Condition Variable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND 

(OR, IN...) NETWORKING

Data accessible only by Monitor


method invocations (private…)
mutex
All method invocations:
• start by lock(mutex)
• end by unlock(mutex) Object

Condition Variable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND 

(OR, IN...) NETWORKING

Data accessible only by Monitor


method invocations (private…)
mutex
All method invocations:
• start by lock(mutex)
• end by unlock(mutex) Object

At any point in time, at most one


threads “occupies” the monitor

Condition Variable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND 

(OR, IN...) NETWORKING

Data accessible only by Monitor


method invocations (private…)
mutex
All method invocations:
• start by lock(mutex)
• end by unlock(mutex) Object

At any point in time, at most one


threads “occupies” the monitor
Condition variables implicitly and

exclusively reference single mutex
Condition Variable

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !126
CONCURRENCY AND 

(OR, IN...) NETWORKING

Data accessible only by Monitor


method invocations (private…)
mutex
All method invocations:
• start by lock(mutex)
• end by unlock(mutex) Object

class ExampleOfMonitor{
private int mySharedValue;
public synchronized void set (int i){
mySharedValue=i;
}
public synchronized int get(){
return mySharedValue;
}
public synchronized void reset(){
mySharedValue=0;
} Condition Variable
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !127
CONCURRENCY AND 

(OR, IN...) NETWORKING

PRIMITIVE:
CONDITION VARIABLES
wait()
notify()
notifyAll()

École Polytechnique • INF557: Fromthomas@thomasclausen.org


the Internet to the IoT • The Fundamentals of Modern Computer Networking !128
http://www.thomasclausen.org
CONCURRENCY AND 

(OR, IN...) NETWORKING

Queue

Producer Consumer

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND 

(OR, IN...) NETWORKING

Queue

put(...) get()
Producer Consumer

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND 

(OR, IN...) NETWORKING

Critical section
MUTEX?

Queue

put(...) get()
Producer Consumer

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND 

(OR, IN...) NETWORKING

put(...) if queue full? Critical section


MUTEX?

Queue

put(...) get()
Producer Consumer

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND 

(OR, IN...) NETWORKING

put(...) if queue full? Critical section


get(...) if queue empty? MUTEX?

Queue

put(...) get()
Producer Consumer

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
CONCURRENCY AND 

(OR, IN...) NETWORKING

put(...) if queue full? Critical section


get(...) if queue empty? MUTEX?

Queue

put(...) get()
Producer Consumer

Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !129
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
queue.put(...)
lock.release()
}
Queue

put(...) get()
Producer Consumer

if queue is full? Block? Error ?


Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !130
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
if (!queue.full()){
queue.put(...)
}
lock.release() Queue
}

put(...) get()
Producer Consumer

if queue is full? Block? Error ?


Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !131
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
while (!queue.full()){
queue.put(...)
}
lock.release() Queue
}

put(...) get()
Producer Consumer

if queue is full? Block? Error ?


Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !132
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){ Queue will never be emptied, Critical section
lock.take() lock never released
while (!queue.full()){
queue.put(...)
}
lock.release() Queue
}

put(...) get()
Producer Consumer

if queue is full? Block? Error ?


Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !132
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){ Critical section
lock.take()
while (queue.full()){
queueNotFull.wait(lock)
}
queue.put(...) Queue
lock.release()
}

put(...) get()
Producer Consumer

if queue is full? Block? Error ?


Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !133
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){ “Temporarily” relinquish lock Critical section
lock.take() until a get() has happened?
while (queue.full()){
queueNotFull.wait(lock)
}
queue.put(...) Queue
lock.release()
}

put(...) get()
Producer Consumer

if queue is full? Block? Error ?


Not enough to protect against concurrent access ....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !133
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !134
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

What do we expect here?

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

What do we expect here?


‣ Relinquish lock on s

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

What do we expect here?


‣ Relinquish lock on s
‣ Producer suspended

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

What do we expect here?


‣ Relinquish lock on s
‣ Producer suspended
‣ Producer awakened when condition is
signalled satisfied

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex() ....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

What do we expect here?


‣ Relinquish lock on s
‣ Producer suspended
‣ Producer awakened when condition is
signalled satisfied
Solution: Condition Variable
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !135
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer:
.... Mutex lock = new Mutex()
public void run(){
lock.take()
while (queue.full()){ class ConditionVariable{
queueNotFull.wait(lock) private Queue suspendedThreads = new Queue()
}
queue.put(...) public void wait(Mutex s){
// add calling thread to suspendedThreads
lock.release() s.unlock()
} // tell scheduler that thread is blocked
s.lock()
}
Consumer:
.... public void notify(){
Thread t = suspendedThreads.first();
public void run(){ // tell scheduler that t is unblocked
lock.take() }
queue.get()
queueNotFull.notify(lock)
lock.release()
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !136
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock) queueNotFull.notify(lock)
} lock.release()
queue.put(...) }
lock.release()
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !137
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ queue.get()
queueNotFull.wait(lock)} queueNotFull.notify(lock)
queue.put(...) lock.release()
lock.release() }
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !138
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){
queueNotFull.wait(lock)}
queue.put(...)
queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
• Must ensure that shared data is consistent !

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
• Must ensure that shared data is consistent !
• wait() instructs scheduler to ignore waiting thread

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain:
• Condition Variables associated with a lock
• Permit temporarily relinquishing lock - to be regained
• Must ensure that shared data is consistent !
• wait() instructs scheduler to ignore waiting thread
• Note that notify() when no waiting threads are lost

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
Btw this is NOT real Java CONCURRENCY AND 

(OR, IN...) NETWORKING

Producer: Consumer:
.... Mutex lock = new Mutex()
....
public void run(){ public void run(){
lock.take() lock.take()
while (queue.full()){ while(queue.empty()){
queueNotFull.wait(lock)} queueNotEmpty.wait(s)
queue.put(...) }
queueNotEmpty.signal(s) queue.get()
lock.release() queueNotFull.notify(lock)
} lock.release()
}
To Retain: Te
no rm
ab rma ino
• Condition Variables associated with a lock ou lly l o
t “ , i gy
• Permit temporarily relinquishing lock - to be regained In
wa java
it/ , t
co t’s A
nd w le
iti ait rt:
on /s
• Must ensure that shared data is consistent ! no he
tif eq
y uiv
va ign
ria al
ble w h
• wait() instructs scheduler to ignore waiting thread ale
nt
s” e n
tal
• Note that notify() when no waiting threads are lost co
nc
ep
ts
kin
g
ar
ec
all
ed

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !139
CONCURRENCY AND 

(OR, IN...) NETWORKING

CONDITION VARIABLES - A JAVA EXAMPLE

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND 

(OR, IN...) NETWORKING

CONDITION VARIABLES - A JAVA EXAMPLE


class PizzaPlace extends Thread{
House deliveryLocation;
public PizzaPlace(House h){
deliveryLocation = h;
}

public void run(){


deliveryLocation.deliverPizza();
}

class PizzaEater extends Thread{


House theHouse;
public PizzaEater(House h){
theHouse = h;
}

public void run(){


theHouse.eatPizza();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND 

(OR, IN...) NETWORKING

CONDITION VARIABLES - A JAVA EXAMPLE


class PizzaPlace extends Thread{
House deliveryLocation;
public PizzaPlace(House h){
deliveryLocation = h;
}

public void run(){


deliveryLocation.deliverPizza();
}

class PizzaEater extends Thread{


House theHouse;
public PizzaEater(House h){
theHouse = h;
}

public void run(){


theHouse.eatPizza();
}
}

class PizzaTest{
public static void main (String[] args) throws Exception{
House ThomasHouseFridayNight = new House();
Thread Thomas = new Thread(new PizzaEater(ThomasHouseFridayNight));
Thread PizzaHut = new Thread(new PizzaPlace(ThomasHouseFridayNight));
Thomas.start() ; PizzaHut.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND 

(OR, IN...) NETWORKING

CONDITION VARIABLES - A JAVA EXAMPLE


class PizzaPlace extends Thread{ import java.lang.Thread;
House deliveryLocation;
public PizzaPlace(House h){ class House{
deliveryLocation = h;
} private boolean pizzaAvailable = false;

public void run(){ public synchronized void eatPizza(){


deliveryLocation.deliverPizza(); while (!pizzaAvailable){
} System.out.println("Oh dear, no pizza, 

better call the Pizza Place!");
} try{ wait(); } 

catch (InterruptedException e){};
class PizzaEater extends Thread{ }
House theHouse; System.out.println("Woo-hoo, I'm eating pizza....");
public PizzaEater(House h){ }
theHouse = h; public synchronized void deliverPizza(){
} pizzaAvailable = true;
System.out.println("Pizza delivered!");
public void run(){ notify();
theHouse.eatPizza(); }
} }
}

class PizzaTest{
public static void main (String[] args) throws Exception{
House ThomasHouseFridayNight = new House();
Thread Thomas = new Thread(new PizzaEater(ThomasHouseFridayNight));
Thread PizzaHut = new Thread(new PizzaPlace(ThomasHouseFridayNight));
Thomas.start() ; PizzaHut.start();
}
}

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !140
CONCURRENCY AND 

(OR, IN...) NETWORKING

AGENDA
‣ The Essence of Last Week:

Identifiers, naming: layers, multiplexing, and demultiplexing
‣ Concurrency - the principle
‣ Concurrency - the primitives

(Threads, Thread States, Barrier Synchronisation, Mutex, condition variables)

‣ (Some) Threaded Architectures

École Polytechnique • INF557: Fromthomas@thomasclausen.org


the Internet to the IoT • The Fundamentals of Modern Computer Networking !141
http://www.thomasclausen.org
ARCHITECTURE,
PROTOCOLS & THE
APPLICATION LAYER

WHY THREADS?
WHY CONCURRENT PROGRAMMING?
‣ Exploit multiple execution units

(CPUs, cores)
‣ Software Engineering:
‣ Blocking System Calls

e.g. java.io.InputStream.read()
‣ Modularity & Architecture

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !142
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE PIPELINING


Program

Stage 1 Stage 2 Stage n


Input Output

Files Files Files

Databases Databases Databases

Disks Disks Disks

Special Devices Special Devices Special Devices

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !143
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE PIPELINING


Program

Stage 1 Stage 2 Stage n


Input Output

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !143
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE PIPELINING


Program

Stage 1 Stage 2 Stage n


Input Output

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !144
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE PIPELINING


Program

Stage 1 Stage 2 Stage n


Input Output

“batch” of input for


similar processing

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !144
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE PIPELINING


Program

Stage 1 Stage 2 Stage n


Input Output

Specialised “single-task” stages


“batch” of input for Similar “processing time” / stage
similar processing Communication between stages?

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !144
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE PIPELINING


Program

Stage 1 Stage 2 Stage n


Input Output

cat /etc/shadow |
grep "werner" |
john - |
mailx -s "BW’s password" tout-le-monde-sur-site@polytechnique.fr

You may already know this concept


École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !145
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE MASTER-WORKER


Program Worker 1

Files
Worker 2
Master Databases

Disks

Worker n
Special Devices

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !146
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE MASTER-WORKER


Worker 1
Program Worker 1

Files
Worker 2
Master Databases

Disks

Worker n
Special Devices

Request Dispatch
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !146
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE MASTER-WORKER


Program Worker 1

Files
Worker 2
Master Databases

Disks

Worker n
Special Devices

Request Dispatch
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !146
CONCURRENCY AND 

(OR, IN...) NETWORKING

SOFTWARE ARCHITECTURE MASTER-WORKER


Identical
Program Worker 1

Files
Worker 2
Master Databases

Disks

Worker n
Special Devices

Request Dispatch Same


Resources !146
École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking
CONCURRENCY AND 

(OR, IN...) NETWORKING
Stage 1 Stage 2 Stage n

“Interfaces” by way of Synchronised Queue

Worker 1

Worker 2 Files
Master Databases

Disks

Worker n
Special Devices

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !147
CONCURRENCY AND 

(OR, IN...) NETWORKING
Stage 1 Stage 2 Stage n

“Interfaces” by way of Synchronised Queue

Worker 1

Worker 2 Files
Master Databases

Disks

Worker n
Special Devices

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !147
CONCURRENCY AND 

(OR, IN...) NETWORKING
Stage 1 Stage 2 Stage n

“Interfaces” by way of Synchronised Queue

Worker 1

Worker 2 Files
Master Databases

Disks

Worker n
Special Devices

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !147
CONCURRENCY AND 

(OR, IN...) NETWORKING

LAB TODAY: XURL -> XCRAWLER


Worker 1
URL
URL
URL

Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded

(if not, mark that it will be)

3. Download file on ORL


Worker n 4. Save to disk
5. If file is html, parse for URLs

Add found URLs to the queue

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND 

(OR, IN...) NETWORKING

LAB TODAY: XURL -> XCRAWLER


Worker 1
URL
URL
URL

Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded

(if not, mark that it will be)

3. Download file on ORL


Worker n 4. Save to disk
5. If file is html, parse for URLs

Add found URLs to the queue

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND 

(OR, IN...) NETWORKING

LAB TODAY: XURL -> XCRAWLER


Worker 1
URL
URL
URL

Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded

(if not, mark that it will be)

3. Download file on ORL


Worker n 4. Save to disk
5. If file is html, parse for URLs

Add found URLs to the queue
Synchronized

Queue of URLs to download

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND 

(OR, IN...) NETWORKING

LAB TODAY: XURL -> XCRAWLER


Synchronised

List of URLs already downloaded, or
in the process of being downloaded

Worker 1
URL
URL
URL

Worker 2
Master
User Interaction 1. Get URL from queue
2. Check if already downloaded

(if not, mark that it will be)

3. Download file on ORL


Worker n 4. Save to disk
5. If file is html, parse for URLs

Add found URLs to the queue
Synchronized

Queue of URLs to download

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !148
CONCURRENCY AND 

(OR, IN...) NETWORKING

TODAY’S KEY POINTS


‣ Applications inherently concurrent
‣ GUI updating while downloading, while GPS tracking, …
‣ Actually makes SW architecture design easier
‣ Requires care - synchronisation
‣ One approach: one thread per module + synchronised queues between modules

‣ Primitives
‣ Threads
‣ MUTEXes (synchronised …)
‣ Condition Variables (wait/notify)
‣ Monitors (Object w/ one mutex + one condition variable)
‣ Barrier Synchronisation (join)

‣ Architectures
‣ Pipelining
‣ Master-Worker

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !149
CONCURRENCY AND 

(OR, IN...) NETWORKING

Off to the salt mines salles d’info....

École Polytechnique • INF557: From the Internet to the IoT • The Fundamentals of Modern Computer Networking !150
https://www.polytechnique.edu/

You might also like