2011-11-22 1 views
1

이동 중에도 가능합니까? 서로 정수를 전송할 수있는 3 개의 동시 루틴이 있습니다. 이제 동시 루틴 2 2가 모두 동시 루틴 1에 정수를 전송한다고 가정하십시오. 루틴 1이 두 값을 모두 취하여 더 멀리 처리 할 수 ​​있습니까? 루틴 3도 int를 보내, 나는 3. 이제 가정 일과 가정Google Go 언어의 동시 루틴

package main 
import "rand" 

func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) { 
for i := 0; i < 10; i++ { 
    i := rand.Intn(100) 
if i%2 == 0 { 
command12 <- i 
} 

if i%2 != 0 { 
command13 <- i 
} 

print(<-response13, " 1st\n"); 
} 
close(command12) 
} 

func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) { 
for i := 0; ; i++ { 
    x, open := <-command12 
    if !open { 
     return; 
    } 
    print(x , " 2nd\n"); 
    y := rand.Intn(100) 
    if i%2 == 0 { 
command12 <- y 
} 

if i%2 != 0 { 
command23 <- y 
} 
} 
} 

func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) { 
for i := 0; ; i++ { 
    x, open := <-command13 
    if !open { 
     return; 
    } 
    print(x , " 3nd\n"); 
    y := rand.Intn(100) 
    response23 <- y 
} 
} 

func main() { 
    command12 := make(chan int) 
    response12 := make(chan int) 
    command13 := make(chan int) 
    response13 := make(chan int) 
    command23 := make(chan int) 
    response23 := make(chan int) 

    go Routine1(command12, response12,command13, response13) 
    Routine2(command12, response12,command23, response23) 
    Routine3(command13, response13,command23, response23) 
} 

여기서, 1 일과 2 또는 3에 int를 보낼 수있는 루틴이 예에서 : 나는 다음 코드 한이 명확하게하려면 루틴 2는 루틴 2가이 두 값을 취하여 더 처리 할 수 ​​있습니까 (동적 동시 루틴)? 어떤 몸이라도이 프로그램을 적절하게 수정할 수 있습니까?

+0

가능한 중복 (http://stackoverflow.com/questions/8232422/concurrent을 :) 단순화 할 수 -routines-in-go) –

답변

2

나는 추상적 인 예를 싫어한다. 어쨌든 나는 당신의 질문에 답하기 위해 최선을 다할 것이다.

루틴 1은 두 값을 모두 가져 와서 더 멀리 처리 할 수 ​​있습니까?

무엇을 아카이브 하시겠습니까? 당신도 할 수 routine1 내부 :

// Read exactly one command from routine2 as well as exactly 
// one command from routine3 
cmd1 := <-command12 
cmd2 := <-command13 
// Process the pair of the two commands here 

또는

// Process a single command only, which was either sent by routine2 
// or by routine3. If there are commands available on both channels 
// (command12 and command13) the select statement chooses a branch 
// fairly. 
select { 
    case cmd1 := <-command12: 
     // process command from routine 2 
    case cmd2 := <-command13 
     // process command from routine 3 
} 

나는 그 질문에 대한 답변 바랍니다. 또한 Go 채널은 기본적으로 여러 작성자 (여러 독자가 포함될 수 있음)를 지원합니다. 따라서 goroutine 당 정확히 하나의 입력 채널을 사용하는 것으로 충분할 수 있습니다. 예를 들어, routine1은 command1이라는 채널에서만 명령을 읽을 수 있지만, routine2와 routine3은 동일한 command1 채널을 사용하여 routine1에 메시지를 보낼 수 있습니다.

Go의 또 다른 일반적인 관용구는 메시지의 일부로 응답 채널을 전달하는 것입니다. 예를 들어 :

type Command struct { 
    Cmd string 
    Reply chan-> int 
} 

func routine2() { 
    reply := make(chan int) 
    command1 <- Command{"doSomething", reply} 
    status := <-reply 
} 

func routine1() { 
    cmd <- command1; 
    // process cmd.Cmd 
    cmd.Reply <- 200 // SUCCESS (status code) 
} 

실제 문제에 따라,이 프로그램이 대폭 [이동에 동시 루틴]의

+0

감사합니다 tux21b .. 이건 내가 실제로 원하는 것보다 더 낫다 .. 고마워. .. – Arpssss

0

GO에서는 불가능합니다. 동시 채널을 만드는 것입니다.

+1

친절하게 도와 주실 수 있습니까? 왜 동시 루틴이 불가능합니까? – Arpssss