Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chan

Go-style channels in C.

This project respects Golang channels and rewrites it in C based on C99 standard. The implementation is just quite simple, no fancy features included.

Buffered/Unbuffered channels

The following C code creates an unbuffered channel (buffered if size is specified), then pushes a string hello to the channel:

struct chan * ch = chan_make(sizeof(char *), 0);

// sender
char * str = strdup("hello");
chan_send(ch, &str);

// receiver
char * str;
chan_recv(ch, &str);
free(str);

Equivalent Go code:

ch := make(chan string)

// sender
ch <- "hello"

// receiver
<- ch

Synchronization (zero-byte transmission)

Let's say you don't want overhead on data transmission but a sychronization way:

struct chan * ch = chan_make(0, 0);
chan_send(ch, NULL);

Equivalent Go code:

ch := make(chan struct{})
ch <- struct{}{}

About

Go-style channels in C

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Contributors

Languages