Questions tagged [channel]

A communication construct enabling sending of objects between execution threads. You may and should use this tag to refer either to the generic programming notion or the specific implementations in various languages or libraries. In the latter case don't forget to add the appropriate tag.

The channel construct is frequently used in multithreading programs to enable safe synchronization of execution threads and communication of objects between them.

When using this tag to refer to the specific implementation in a programming language or library, don't forget to add an additional tag to prevent confusion, for example .

General References :

Wikipedia definition of the channel

Go Language :

The channel is one of the basic constructs of Go and generally behind every parallelization.

As described in Go specification :

A channel provides a mechanism for two concurrently executing functions to synchronize execution and communicate by passing a value of a specified element type.

// creation of a basic synchronous channel of strings
c := make(chan string)

// creation of a channel enabling the buffering of 100 strings before blocking
c := make(chan string, 100)

// sending of a string over the channel
c <- "somestring"

// closing of a channel
close(c)

// reception from a channel. This blocks until a string is available
s := <- c

// blocking loop over a channel until closing
for s := range c {

Java Language :

With Java 1.4, the New IO introduced the channel as an interface with many concrete implementations intended to replace the use of stream in concurrent contexts :

A channel represents an open connection to an entity such as a hardware device, a file, a network socket, or a program component that is capable of performing one or more distinct I/O operations, for example reading or writing.

A channel is either open or closed. A channel is open upon creation, and once closed it remains closed. Once a channel is closed, any attempt to invoke an I/O operation upon it will cause a ClosedChannelException to be thrown. Whether or not a channel is open may be tested by invoking its isOpen method.

Channels are, in general, intended to be safe for multithreaded access as described in the specifications of the interfaces and classes that extend and implement this interface.

2474 questions
259
votes
9 answers

PackagesNotFoundError: The following packages are not available from current channels:

I'm somewhat new to Python. I've used it in a bunch of projects, but haven't really needed to stray from its standard setup. I'm trying to install some new packages to get access to functions necessary for a university assignment. When I try to…
Sunafegon
  • 2,691
  • 2
  • 8
  • 4
258
votes
8 answers

Is it OK to leave a channel open?

Is it OK to leave a Go channel open forever (never close the channel) if I never check for its state? Will it lead to memory leaks? Is the following code OK? func (requestCh chan<- Request) GetResponse(data RequestData) Response { reply :=…
Kluyg
  • 5,119
  • 2
  • 25
  • 28
228
votes
4 answers

RabbitMQ and relationship between channel and connection

The RabbitMQ Java client has the following concepts: Connection - a connection to a RabbitMQ server instance Channel - ??? Consumer thread pool - a pool of threads that consume messages off the RabbitMQ server queues Queue - a structure that holds…
user1768830
154
votes
8 answers

How to stop a goroutine

I have a goroutine that calls a method, and passes returned value on a channel: ch := make(chan int, 100) go func(){ for { ch <- do_stuff() } }() How do I stop such a goroutine?
Łukasz Gruner
  • 2,929
  • 3
  • 26
  • 28
125
votes
10 answers

How to check a channel is closed or not without reading it?

This is a good example of workers & controller mode in Go written by @Jimt, in answer to "Is there some elegant way to pause & resume any other goroutine in golang?" package main import ( "fmt" "runtime" "sync" "time" ) // Possible…
Shane Hou
  • 4,808
  • 9
  • 35
  • 50
110
votes
3 answers

What is channel buffer size?

I'm trying to create an asynchronous channel and I've been looking at http://golang.org/ref/spec#Making_slices_maps_and_channels. c := make(chan int, 10) // channel with a buffer size of 10 What does it mean that the buffer size is 10?…
Tech163
  • 4,176
  • 8
  • 33
  • 36
102
votes
1 answer

Does WebRTC use TCP or UDP?

I have some questions about WebRTC: Does WebRTC use TCP or UDP as its peer-to-peer transport? How can I know? I read that there are reliability mode and DTLS agreement, how does these affect? Is this transport the same for both Media and…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
98
votes
4 answers

How are Go channels implemented?

After (briefly) reviewing the Go language spec, effective Go, and the Go memory model, I'm still a little unclear as to how Go channels work under the hood. What kind of structure are they? They act kind of like a thread-safe queue /array. Does…
Matt
  • 22,721
  • 17
  • 71
  • 112
89
votes
7 answers

What is the Advantage of sync.WaitGroup over Channels?

I'm working on a concurrent Go library, and I stumbled upon two distinct patterns of synchronization between goroutines whose results are similar: Waitgroup package main import ( "fmt" "sync" "time" ) var wg sync.WaitGroup func main()…
Pandemonium
  • 7,724
  • 3
  • 32
  • 51
86
votes
4 answers

Different ways to pass channels as arguments in function

I was reading some go code and say a few different ways to pass go channels. Maybe they are the same but I was wondering if there is any difference since I couldn't find documentation online: 1) func serve(ch <-chan interface{}){ //do stuff…
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
81
votes
2 answers

What's the difference between "<-chan" and "chan" as a function return type?

Golang newbie here. Is there a functional difference between func randomNumberGenerator() <-chan int { and func randomNumberGenerator() chan int { I've tried using both and they seem to work fine for me. I've seen the former used by Rob Pike…
Abs
  • 2,234
  • 1
  • 19
  • 27
81
votes
4 answers

Are channels passed by reference implicitly

The go tour has this example for channels: https://tour.golang.org/concurrency/2 package main import "fmt" func sum(a []int, c chan int) { sum := 0 for _, v := range a { sum += v } c <- sum // send sum to c } func main()…
lhk
  • 27,458
  • 30
  • 122
  • 201
78
votes
3 answers

Kotlin Coroutines: Channel vs Flow

I'm recently studying and reading a lot about Flow and Kotlin Coroutines. But I still get confused about when I should use Flow and when I should use Channel. At the beginning it looked more simple. Working with hot streams of data? Channel. Cold…
Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95
70
votes
4 answers

How to know a buffered channel is full

How to know a buffered channel is full? I don't know to be blocked when the buffered channel is full, instead I choose to drop the item sent to the buffered channel.
GarudaReiga
  • 1,057
  • 2
  • 9
  • 12
67
votes
4 answers

Why does the use of an unbuffered channel in the same goroutine result in a deadlock?

I'm sure that there is a simple explanation to this trivial situation, but I'm new to the go concurrency model. when I run this example package main import "fmt" func main() { c := make(chan int) c <- 1 fmt.Println(<-c) } I get…
Salah Eddine Taouririt
  • 24,925
  • 20
  • 60
  • 96
1
2 3
99 100