Code Monkey home page Code Monkey logo

channel-cpp's Introduction

channel

test codecov

C++ implementation of Go channel.

Usage

Send/Recv

auto const chan = make_chan<int>(); // Channel with 0 size buffer.

auto const sender = std::jthread([&](){
	chan->send(42);
});

int v;
chan->recv(v);
/* Golang equivalent. */

c := make(chan int)

go func(){ c <- 42 }()

answer := <- c

Select

auto const chan1 = make_chan<int>();
auto const chan2 = make_chan<int>();

auto const sender = std::jthread([&](){
	chan2->send(42);
});

select(
	recv(*chan1, [](bool ok, int v){
		std::cout << "chan1 received " << v << std::endl;
	}),
	recv(*chan2, [](bool ok, int v){
		std::cout << "chan2 received " << v << std::endl;
	}),
);

// This blocks forever because the receive from `chan1` is effectively cancelled.
chan1->send(36);
/* Golang equivalent. */

c1 := make(chan int)
c2 := make(chan int)

go func(){ c2 <- 42 }()

select {
	case v := <- c1: fmt.Println("c1 received", v)
	case v := <- c2: fmt.Println("c2 received", v)
}

channel-cpp's People

Contributors

lesomnus avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

brugarolas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.