Dlaczego program raz kończy się na pierwszym awake <- 0 a raz na drugim awake <- 1

0

Dobry wieczór, mój program uruchamia 11 go rutyn i pierwsza działa dobrze, a reszta blokuję się na awake <- 1, mógłby mi ktoś wytłumaczyć dlaczego?

func patient(p chan <- chan string, awake chan <- int, id string) {
	fmt.Println("1")
	awake <- 1 // Czemu tu się wykurwaisz?
	fmt.Println("2")
	time.Sleep(1 * time.Millisecond)
	fmt.Println("3")
	x := make(chan string, 1)
	fmt.Println("4")
	x <- id
	fmt.Println("5")
	p <- x
	fmt.Println("6")
	awake <- 0
	fmt.Println("7")
}

Edit: Błąd był w innej części kodu, przez co tutaj nie działało

2

a jest coś co nasłuchuje na awake? Jeśli nie ma innej gorotutiny, która by nasłuchiwała na awake albo jest ona buforowalna, lecz zapełniona, to patient się zawiesi na tej linijce. Może pokaż trochę większy kawałek kodu, gdzie będziemy mogli ten błąd odtworzyć.

0

@no_solution_found:

Tam jest edit, ogarnąłem już o co z tym chodziło ( worker nasłuchiwał tylko raz ).
Ale korzystając z okazji zadam pytanie, masz jakiś pomysł co zrobić by najpierw wypisywać wszystko z kanału high a dopiero kiedy ten jest pusty to wyświetlać low?
Zapomniałem jak się edytowało posta więc tu wklejam cały kod:

package main

import (
	"fmt"
	"time"
)

func worker(high, low <- chan chan string, awake chan int) {
	for {
		for <-awake == 0 {
			time.Sleep(1 * time.Millisecond)
			fmt.Println("Maintenance")
		}

		time.Sleep(1 * time.Millisecond)

		select {
		
		case h := <-high:
			fmt.Printf("Job %s is done\n", <-h)
		
		case l := <-low:
			fmt.Printf("Job %s is done\n", <-l)
		}
	}
}

func patient(p chan <- chan string, awake chan <- int, id string) {
	awake <- 1
	time.Sleep(1 * time.Millisecond)
	x := make(chan string, 1)
	x <- id
	p <- x
	awake <- 0
}

func main() {

	high := make( chan chan string, 10)
	low := make( chan chan string, 10)

	awake := make(chan int)

	go worker(high, low, awake)
	awake <- 0

	time.Sleep(1 * time.Second)

	for i := 0; i < 10; i++ {
		if i%2 == 0 {
			go patient(low, awake, "low")
		}
		if i%2 != 0 {
			go patient(high, awake, "high")
		}
	}

	time.Sleep(1 * time.Second)

	go patient(high, awake, "high")

	time.Sleep(10 * time.Second)

	close(high)

	close(low)

}
0

możesz zrobić to tak

for {
  select {
    case <- chan1:
    // ble ble
   case <-chan2:
   // ble ble 2
  }
}

Jeśli nic nie będzie na kanale 1, to będzie sprawdzać kanał 2 i tak w kółko

0

@no_solution_found: No tak, ale jeżeli raz idzie coś na A a raz na B to będzie wtedy output A, B, A, B, A, B, A, B. A program powinien teoretycznie czytać najpierw A a potem B.
Próbowałem tak, ale kiedy "Low" jest pierwsze to czeka w nieskończoność a kiedy "High" jest pierwsze to wyświetli tylko raz i potem nie działa.

func worker(high, low <- chan chan string, awake chan int) {
	for {
		for <-awake == 0 {
			time.Sleep(1 * time.Millisecond)
			fmt.Println("Maintenance")
		}

		time.Sleep(1 * time.Millisecond)

		val, ok := <-high
		if ok {
			fmt.Printf("Job %s is done", <-val)
			continue
		}

		val, ok = <-low
		if ok {
			fmt.Printf("Job %s is done", <-val)
			continue
		}
	}
}

PS. Jest coś podobnego do if !<-chan {}?

0

to co teraz robisz, to jest trochę na ślinę zrobienie tego selecta, którego opisałem. Jeśli wiesz, że przestanie w którymś momencie przychodzić coś do kanału high, to możesz go zamknąć i np użyć takiej pętelki.


for val := range high {
   // ble ble
}

for val := range low {
   // ble ble
}

I gdzieś gdzie produkujesz dane dla high na końcu zrobić close(high). o coś takiego chodziło?

0

@no_solution_found:

I gdzieś gdzie produkujesz dane dla high na końcu zrobić close(high).

A to nie wyłączy mi kanału z użytkowania - znaczy, wydaje mi się że wtedy kanał po pierwszej informacji się wyłączy i będzie niezdatny do użytku.

func patient(p chan <- chan string, awake chan <- int, id string) {
	awake <- 1
	time.Sleep(1 * time.Millisecond)
	x := make(chan string, 1)
	x <- id
	p <- x
	awake <- 0
       close(p) // <-
}
0
no_solution_found napisał(a):

możesz zrobić to tak

for {
  select {
    case <- chan1:
    // ble ble
   case <-chan2:
   // ble ble 2
  }
}

Jeśli nic nie będzie na kanale 1, to będzie sprawdzać kanał 2 i tak w kółko

Ale to mu nie daje priorytetów, które chciał.

https://go.dev/ref/spec#Select_statements

Execution of a "select" statement proceeds in several steps:

  1. **For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. **The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
  5. The statement list of the selected case is executed.

Można zrobić tak:


		select {
		case h := <-high:
			fmt.Printf("Job %s is done\n", <-h)
		default:
			select {
			case h := <-high:
				fmt.Printf("Job %s is done\n", <-h)
			case l := <-low:
				fmt.Printf("Job %s is done\n", <-l)

			}
		}

Swoją drogą, nie bardzo rozumiem, po co ci kanał awake? Co chcesz osiągnąć? worker i tak będzie spał, jeżeli select nie ma niczego do wykonania.

Na poniższym przykładzie, widać, że high ma zawsze priorytet, a low jest zagłodzone:


package main

import (
	"fmt"
	"time"
)

func worker(high, low <-chan chan string, quit <-chan struct{}) {
	for {
		select {
		case <-quit:
			return
		case h := <-high:
			handle(h)
		default:
			select {
			case h := <-high:
				handle(h)
			case l := <-low:
				handle(l)

			}
		}

	}
}

func handle(job <-chan string) {
	fmt.Printf("Job %s is done\n", <-job)
}

func patient(p chan<- chan string, quit <-chan struct{}, id string) {
	for {
		select {
		case <-quit:
			return
		default:
			x := make(chan string, 1)
			x <- id
			p <- x
		}
	}

}

func main() {
	high := make(chan chan string, 10)
	low := make(chan chan string, 10)
	quit := make(chan struct{})

	go patient(low, quit, "low")
	go patient(high, quit, "high")
	go worker(high, low, quit)

	time.Sleep(2 * time.Second)
	close(quit)
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1