Created
August 9, 2023 16:49
-
-
Save rag594/2d146c9fc2322ccb6abe4c7f5d6383d5 to your computer and use it in GitHub Desktop.
goroutines-incorrect
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"sync" | |
"time" | |
"github.com/labstack/echo" | |
"github.com/labstack/echo/middleware" | |
) | |
var wGGlobal sync.WaitGroup | |
var pollQChannel = make(chan int, 3) | |
var pollYChannel = make(chan int, 3) | |
func A(wG *sync.WaitGroup) { | |
wG.Add(1) | |
defer wG.Done() | |
fmt.Println("A") | |
} | |
func D(wG *sync.WaitGroup) { | |
// missing the call to Add() | |
defer wG.Done() | |
fmt.Println("C") | |
} | |
func B(wG *sync.WaitGroup) { | |
defer func() { | |
fmt.Println(<-pollQChannel) | |
}() | |
fmt.Println("B") | |
A(wG) | |
} | |
func C(wG *sync.WaitGroup) { | |
fmt.Println("C") | |
defer func() { | |
fmt.Println(<-pollYChannel) | |
}() | |
D(wG) | |
} | |
func PollQ() { | |
for { | |
time.Sleep(10 * time.Second) | |
pollQChannel <- 1 | |
go B(&wGGlobal) | |
} | |
} | |
func PollY() { | |
for { | |
time.Sleep(10 * time.Second) | |
pollYChannel <- 1 | |
go C(&wGGlobal) | |
} | |
} | |
func main() { | |
//var wG sync.WaitGroup | |
e := echo.New() | |
// Middleware | |
e.Use(middleware.Logger()) | |
e.Use(middleware.Recover()) | |
// Routes | |
e.GET("/", hello) | |
go PollQ() | |
go PollY() | |
// Start server | |
e.Logger.Fatal(e.Start(":1323")) | |
} | |
func hello(c echo.Context) error { | |
return c.String(http.StatusOK, "Hello, World!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment