Skip to content

ocalasans/sluicecpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sluice

Go-style channels and select for C++. Threads exchange values through Channel<T>, and Select_Any waits on several channels at once — without you writing a single std::mutex or std::condition_variable by hand.

#include <thread>
//
#include <sluice/sluice.hpp>

Sluice::Channel<int> numbers(4); // buffered, capacity 4

std::thread producer([&] {
    for (int i = 1; i <= 10; ++i)
        numbers.Send_Value(i);
});

int value = numbers.Receive_Value(); // blocks until something arrives

Why

C++ has never had a mature, actively maintained library offering real multi-threaded channels with a working select — existing attempts are either single-threaded by design or abandoned. Sluice fills that gap: real multi-thread channels, a select that waits on multiple channels without busy-waiting, and an API that stays out of your way.

What you get

  • Channel<T> — capacity 0 is a true rendezvous (Send_Value blocks until another thread actually receives the value); capacity N > 0 is a bounded buffer. Blocking (Send_Value/Receive_Value) and non-blocking (Try_Receive) APIs, Close() with correct draining semantics, thread-safe with any number of producers and consumers on the same channel.
  • Select_Any — waits on 2 or 3 channels at once, returns as soon as any one of them has data, without busy-waiting. Generalizing this to an arbitrary number of channels is planned but not implemented yet.
  • C++14 and up — the same headers adapt automatically to whatever standard your project targets, using more modern standard library features where the compiler supports them and falling back to equivalent code where it doesn't. You don't need to match any specific standard; whatever C++14-or-newer version you already use is enough.
  • T only needs to be move-constructible. No copy requirement anywhere.

Closed channels

  • Send_Value on an already-closed channel throws std::logic_error immediately.
  • Receive_Value throws only once a channel is both closed and empty — buffered values sent before Close() are still delivered normally.
  • Try_Receive never throws; it just returns an empty result when there's nothing to receive.
  • On a rendezvous channel (capacity 0), if Close() happens after a value was handed to Send_Value but before a receiver actually took it, Send_Value throws too — the value stays enqueued (still receivable by whoever looks), but the sender is told the handoff itself didn't happen.
  • Select_Any throws if every channel passed to it is closed and empty at the same time, since none of them could ever produce a value again. It keeps waiting normally as long as at least one channel is still open.

Requirements

  • A C++14-or-newer compiler.
  • CMake 3.25+.

Building

cmake -S . -B build
cmake --build build

This builds a static library (libsluice.a / sluice.lib) by default, plus the examples and tests. Useful options:

Option Default Effect
SLUICE_BUILD_SHARED OFF Build a shared library (.so/.dll) instead of static.
SLUICE_BUILD_EXAMPLES ON Build the programs under examples/.
SLUICE_BUILD_TESTS ON Build the test suite and register it with CTest.

Run the tests with ctest --test-dir build after building.

Using it in your own CMake project

Vendored via add_subdirectory or FetchContent:

add_subdirectory(sluicecpp)
target_link_libraries(your_target PRIVATE Sluice::sluice)

Or after installing (cmake --install build):

find_package(Sluice 1.0 REQUIRED)
target_link_libraries(your_target PRIVATE Sluice::sluice)

Both forms give you the same Sluice::sluice target, so switching between them later doesn't require changing anything else in your build.

If you're linking against the shared library directly (not through CMake), define SLUICE_SHARED when compiling your own code — CMake consumers get this automatically.

License

Copyright © 2026 Calasans

This software is licensed under the terms of the Apache License, Version 2.0 ("License"); you may not use this software except in compliance with the License. A copy of the License can be obtained at: http://www.apache.org/licenses/LICENSE-2.0

Terms and Conditions of Use

1. Granted Permissions

The present license grants, free of charge, to any person obtaining a copy of this software and associated documentation files, the following rights:

  • Use, copy, modify, and distribute the software in any medium or format, for any purpose, commercial or non-commercial
  • Merge, publish, distribute, sublicense, and/or sell copies of the software
  • Permit persons to whom the software is furnished to do the same

2. Mandatory Conditions

All distributions of the software or derivative works must:

  • Include a complete copy of this license
  • Clearly indicate any modifications made to the original source code
  • Preserve all copyright, patent, trademark, and attribution notices
  • Provide adequate documentation of implemented changes
  • Maintain the license and warranty notice in all copies

3. Restrictions and Limitations

  • This license does not grant permission to use trademarks, logos, or trade names of Calasans
  • Contributions to the source code must be licensed under the same terms as this license
  • The use of contributor names to endorse or promote products derived from this software requires specific prior permission

4. Intellectual Property

The software and all associated documentation are protected by copyright laws and international treaties. Calasans retains all rights, titles, and interests not expressly granted by this license.

5. Disclaimer of Warranties and Limitation of Liability

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


For detailed information about the Apache License 2.0, see: http://www.apache.org/licenses/LICENSE-2.0

About

Go-style channels and select for C++, enabling safe message passing between threads without manual mutexes or condition variables.

Topics

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Contributors