-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5045 from prakanth97/fork
Add bbe for fork statement
- Loading branch information
Showing
5 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import ballerina/io; | ||
|
||
public function main(int? userInput) { | ||
if userInput is int { | ||
int num = userInput; | ||
fork { | ||
worker A { | ||
num -> B; | ||
string value = <- B; | ||
io:println(string `Received string '${value}' from worker B`); | ||
} | ||
|
||
worker B { | ||
int value = <- A; | ||
io:println(string `Received int '${value}' from worker A`); | ||
"a" -> A; | ||
} | ||
} | ||
} else { | ||
io:println("Not forked"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Fork | ||
|
||
The fork statement starts one or more named workers, which run in parallel with each other, each in its own new strand. Variables and parameters in the scope of the fork statement remain within the scope of the workers. Message passing can be done only between the workers created within the fork statement and cannot be done with the function's default worker and its named workers. Unlike named workers outside a `fork` statement, with a `fork` statement, workers can be defined anywhere within the function body (e.g., within a conditional block). | ||
|
||
::: code fork.bal ::: | ||
|
||
::: out fork.out ::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates how the `fork` statement can be used for concurrency. | ||
keywords: ballerina, ballerina by example, bbe, fork, concurrency, named workers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
$ bal run fork.bal -- 5 | ||
Received int '5' from worker A | ||
Received string 'a' from worker B |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters