How to resolve the algorithm Synchronous concurrency step by step in the Go programming language
How to resolve the algorithm Synchronous concurrency step by step in the Go programming language
Table of Contents
Problem Statement
The goal of this task is to create two concurrent activities ("Threads" or "Tasks", not processes.) that share data synchronously. Your language may provide syntax or libraries to perform concurrency. Different languages provide different implementations of concurrency, often with different names. Some languages use the term threads, others use the term tasks, while others use co-processes. This task should not be implemented using fork, spawn, or the Linux/UNIX/Win32 pipe command, as communication should be between threads, not processes. One of the concurrent units will read from a file named "input.txt" and send the contents of that file, one line at a time, to the other concurrent unit, which will print the line it receives to standard output. The printing unit must count the number of lines it prints. After the concurrent unit reading the file sends its last line to the printing unit, the reading unit will request the number of lines printed by the printing unit. The reading unit will then print the number of lines printed by the printing unit. This task requires two-way communication between the concurrent units. All concurrent units must cleanly terminate at the end of the program.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Synchronous concurrency step by step in the Go programming language
This program counts the number of lines in a text file, the code reads the file line by line, sends each line to a channel lines
, and starts a goroutine to count the number of lines.
The main function opens the input file and starts a goroutine to read the file line by line.
For each line read, it sends the line to the lines
channel.
The main function closes the file and closes the lines
channel to signal the end of the input.
The goroutine started by the main
function runs concurrently with the main function.
It reads lines from the lines
channel, prints each line, and increments a counter.
When the lines
channel is closed, the goroutine exits and sends the counter value to the count
channel.
The main
function receives the counter value from the count
channel and prints it.
Source code in the go programming language
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
lines := make(chan string)
count := make(chan int)
go func() {
c := 0
for l := range lines {
fmt.Println(l)
c++
}
count <- c
}()
f, err := os.Open("input.txt")
if err != nil {
log.Fatal(err)
}
for s := bufio.NewScanner(f); s.Scan(); {
lines <- s.Text()
}
f.Close()
close(lines)
fmt.Println("Number of lines:", <-count)
}
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the XPL0 programming language
You may also check:How to resolve the algorithm A+B step by step in the Transd programming language
You may also check:How to resolve the algorithm Arrays step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Io programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the Haskell programming language