Git Product home page Git Product logo

cocol's Introduction

CoCoL - Concurrent Communications Library

Nuget count License Issues open

CoCoL is a fresh multi-programming approach, leveraging the C# await keyword to produce sequential and easily understandable multithreading code. With a shared-nothing approach and explicit communication, programs written with CoCoL are automatically free from race conditions and other threading hazards.

If you are familiar with the Go Language, you can think of CoCoL as providing the Go programming model inside the CLR.

Installation

The NuGet package is the recommended way of installing CoCoL:

PM> Install-Package CoCoL

Hello World

The most basic program with multithreading would be a producer/consumer setup, where one thread produces data, and another consumes it:

using System;
using System.Linq;
using System.Threading.Tasks;
using CoCoL;

class Example
{
    static async Task Produce(IChannel<int> channel)
    {
        foreach (var i in Enumerable.Range(0, 5))
            await channel.WriteAsync(i);
        channel.Retire();
    }

    static async Task Consume(IChannel<int> channel)
    {
        try
        {
            while (true)
                Console.WriteLine("Hello World: {0}", await channel.ReadAsync());
        }
        catch (RetiredException)
        {
        }
    }

    static void Main()
    {
        var channel = ChannelManager.CreateChannel<int>();
        Task.WhenAll(
          Produce(channel),
          Consume(channel)
        ).Wait();
    }
}

Output:

Hello World: 0
Hello World: 1
Hello World: 2
Hello World: 3
Hello World: 4

The producer writes a number of values into the channel, and then stops the channel. The consumer simply reads whatever is written into the channel.

Since the producer and consumer only shares the channel, and only communicate through the channel, no race conditions are possible.

Sharing a channel

A key feature of CoCoL is that it is capable of scaling to a very large number of channels and reader/writers. On a machine with 16GB of memory, it is possible to run more than 10 million reader/writer pairs. Using the above producer and consumer example, it is possible to attach multiple producers and consumers on the same channel, with no ill effects:

static void Main()
{
  var channel = ChannelManager.CreateChannel<int>();
  
  Task.WhenAll(
    Enumerable.Range(0,4).Select(x => Produce(channel))
    .Union(
      Enumerable.Range(0,2).Select(x => Consume(channel))
    )
).Wait();
}

In the above, there are four producers and two consumers sharing the same channel. The runtime adjusts how many threads it deems necessary for running the application.

Multiple channels

If the producers each have their own channel, the consumer can choose to read from any of the channels:

static async Task Consume(IEnumerable<IChannel<int>> channels)
{
  while(true)
    Console.WriteLine("Hello World: {0}", await channels.ReadFromAnyAsync());
}

static void Main()
  {
    var channels = Enumerable.Range(0, 5).Select(x => {
      var channel = ChannelManager.CreateChannel<int>();
      Produce(channel);
      return channel;
    });
    
    Consume(channels).Wait();
  }

Mixing with existing multithreading

If existing code is used, it is possible to use blocking or probing calls as well:

static void Main()
{
  var channel = ChannelManager.CreateChannel<int>();
  
  var thread = new System.Threading.Thread(x => {
    // Probing write
    while(!channel.TryWrite(1))
    {
      // Do other stuff here
      System.Thread.Thread.Sleep(1000);
    }
  });
  thread.Run();
  
  // Blocking read
  channel.Read();
  
}

More examples?

Look at the CommsTime, Sieve, and Mandelbrot examples.

cocol's People

Contributors

danstahr avatar dependabot[bot] avatar kenkendk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

danstahr bbants

cocol's Issues

other than .net45 binaries are not signed

It makes Duplicati .netstandart throw this:
System.IO.FileLoadException : Could not load file or assembly 'CoCoL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)

Code excution appears to stop in Duplicati while using CoCoL

Looking into an issue with Duplicati and there seems to be a weird issue where throwing an Exception results in what appears to be the code no longer executing. Duplicati is stuck at that point.

This is while using CoCoL and Task.WhenAll().

https://forum.duplicati.com/t/duplicati-lock-up/12510/47

Looks like its using CoCoL's AutomationExtensions.RunTask() of which I see it (CoCoL's function there) should throw an Exception or return or whatever... so my assumption is that an issue happens and never returns execution back to Duplicati's code. Nothing else makes any sense as even Task.WhenAll() should keep code execution going. Its just a throw.

Any ideas?

Also I added logs to the other functions used before this code in Duplicati and didn't see any execution starting or finishing so other functions should not be holding up CoCoL from returning as far as I can see.

Tomorrow I will check and put a log in every loop in every function and see if one gets trapped. Looks like others are running up to just before throwing the Exception so maybe I missed one.

Add cancellation

The current implementation uses custom timeouts, but a more general approach would be to use cancellation.

We should see if CoCoL can be refactored to support cancellation.

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.