C++20's jthread for C++11 and later in a single-file header-only library

Overview

jthread lite: C++20's jthread for C++11 and later

A work in its infancy. Suggested by Peter Featherstone.

Language License Build Status Build status Version download Conan Try it online Try it on godbolt online

Contents

Example usage

#include "nonstd/jthread.hpp"
#include <iostream>

int main(int argc, char **)
{
    int product = 0;
    const int six = 6;
    const int seven = 7;

    {
        nonstd::jthread thr{[&](int x, int y){ product = x * y; }, six, seven };

        // automatically join thread here, making sure it has executed:
    }

    std::cout << "Product of "<< six << " and " << seven << " is " << product << ".\n";
}

Compile and run

$ g++ -std=c++11 -Wall -I../include/ -o 02-arguments.exe 02-arguments.cpp && 02-arguments.exe
Product of 6 and 7 is 42.

In a nutshell

jthread lite is a single-file header-only library to provide C++20's class jthread for use with C++11 and later. If available, the standard library is used, unless configured otherwise.

Currently nonstd::jthread does not (yet) support thread cancellation using stop_token, stop_source and stop_callback. It also does not support std::condition_variable_any.

Features and properties of jthread lite are ease of installation (single header), freedom of dependencies other than the standard library.

Limitations of jthread lite are ... [to be summed up].

License

jthread lite is distributed under the Boost Software License.

Dependencies

jthread lite has no other dependencies than the C++ standard library.

Installation

jthread lite is a single-file header-only library. Put jthread.hpp in the include folder directly into the project source tree or somewhere reachable from your project.

Synopsis

Documentation of class jthread

[Envisioned] Depending on the compiler and C++ standard used, jthread lite behaves less or more like the standard's version. To get an idea of the capabilities of jthread lite with your configuration, look at the output of the tests, issuing jthread-main.t --pass @.

For the standard's documentation, see class jthread, which is part of the C++ thread library.

jthread lite implementation status

Kind Type or function Notes
Type jthread present, no stop abilities
  nostopstate_t present
  stop_token present, no functionality
  stop_source present, no functionality
  stop_callback not present, no functionality
  condition_variable_any not present, no functionality
     
Objects nostopstate macro for pre-C++17 (no inline var.)
     
Utilities make_stop_callback() not present, deduction guideline workaround

Configuration

Tweak header

If the compiler supports __has_include(), jthread lite supports the tweak header mechanism. Provide your tweak header as nonstd/jthread.tweak.hpp in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like #define jthread_CPLUSPLUS 201103L.

Select std::jthread or nonstd::jthread

[To be implemented]

At default, jthread lite uses std::jthread if it is available and lets you use it via namespace nonstd. You can however override this default and explicitly request to use std::jthread or jthread lite's nonstd::jthread as nonstd::jthread via the following macros.

-Djthread_CONFIG_SELECT_JTHREAD=jthread_SELECT_JTHREAD_NONSTD
Define this to jthread__CONFIG_SELECT_JTHREAD_STD to select std::jthread as nonstd::jthread. Define this to jthread_SELECT_JTHREAD_NONSTD to select nonstd::jthread as nonstd::jthread. Default is undefined, which has the same effect as defining to jthread_SELECT_JTHREAD_NONSTD currently (this may change to jthread_SELECT_JTHREAD_DEFAULT).

Standard selection macro

-Djthread_CPLUSPLUS=199711L
Define this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the __cplusplus macro correctly.

Disable exceptions

-Djthread_CONFIG_NO_EXCEPTIONS=0 Define this to 1 if you want to compile without exceptions. If not defined, the header tries and detect if exceptions have been disabled (e.g. via -fno-exceptions). Default is undefined.

Other implementations of jthread

  • jthread. Nicolai Josuttis. GitHub.

Notes and references

  • p0660 - Stop Token and Joining Thread. Nicolai Josuttis, Lewis Baker, Billy O’Neal, Herb Sutter, Anthony Williams. 2019.

Appendix

A.1 Compile-time information

The version of jthread lite is available via tag [.version]. The following tags are available for information on the compiler and on the C++ standard library used: [.compiler], [.stdc++], [.stdlanguage] and [.stdlibrary].

A.2 Jthread lite test specification

click to expand

jthread: default construct
jthread: create thread with callback - no parameters
jthread: create thread with callback - with parameters
tweak header: Reads tweak header if supported [tweak]
Issues
  • The beginnings

    The beginnings

    First release:

    Code

    • [x] jthread, without stop abilities
    • [x] nostopstate_t, nostopstate,

    Configuration

    • [x] Selection between std::jthread and nonstd::jthread
    • [x] No exception usage

    Tests

    • [x] jthread, without stop abilities

    Distribution

    • [x] CMake
    • [x] Conio

    Other

    • [x] godbolt
    • [x] wandbox

    Deferred:

    Code

    • jthread, with stop abilities
    • stop_token
    • stop_source
    • stop_callback, make_stop_callback()
    • condition_variable_any

    Configuration

    • ...

    Tests

    • jthread, with stop abilities
    • nostopstate_t, nostopstate,
    • stop_token
    • stop_source
    • stop_callback, make_stop_callback
    • condition_variable_any

    Distribution

    • vcpkg

    Other

    • ...
    opened by martinmoene 0
  • Add thread cancellation

    Add thread cancellation

    Code

    • [ ] jthread, with stop abilities
    • [ ] stop_token
    • [ ] stop_source
    • [ ] stop_callback, make_stop_callback()
    • [ ] condition_variable_any

    Configuration

    • [ ] ...

    Tests

    • [ ] jthread with stop abilities
    • [ ] nostopstate_t, nostopstate,
    • [ ] stop_token
    • [ ] stop_source
    • [ ] stop_callback, make_stop_callback
    • [ ] condition_variable_any

    Distribution

    • [ ] vcpkg

    Other

    • [ ] ...
    opened by martinmoene 0
Releases(v0.1.0)
Owner
Martin Moene
C++ programmer at Universiteit Leiden, member and former webeditor of @accu-org.
Martin Moene
A fast single-producer, single-consumer lock-free queue for C++

A single-producer, single-consumer lock-free queue for C++ This mini-repository has my very own implementation of a lock-free queue (that I designed f

Cameron 2.7k Aug 5, 2022
Single header asymmetric stackful cross-platform coroutine library in pure C.

minicoro Minicoro is single-file library for using asymmetric coroutines in C. The API is inspired by Lua coroutines but with C use in mind. The proje

Eduardo Bart 309 Jul 24, 2022
Coro - Single-header library facilities for C++2a Coroutines

coro This is a collection of single-header library facilities for C++2a Coroutines. coro/include/ co_future.h Provides co_future<T>, which is like std

Arthur O'Dwyer 60 Jul 20, 2022
Px - Single header C++ Libraries for Thread Scheduling, Rendering, and so on...

px 'PpluX' Single header C++(11/14) Libraries Name Code Description px_sched px_sched.h Task oriented scheduler. See more px_render px_render.h Multit

PpluX 440 Jun 30, 2022
A header-only C++ library for task concurrency

transwarp Doxygen documentation transwarp is a header-only C++ library for task concurrency. It allows you to easily create a graph of tasks where eve

Christian Blume 586 Jul 29, 2022
Header-Only C++20 Coroutines library

CPP20Coroutines Header-Only C++20 Coroutines library This repository aims to demonstrate the capabilities of C++20 coroutines. generator Generates val

null 15 Mar 2, 2022
Header-only library for multithreaded programming

CsLibGuarded Introduction The CsLibGuarded library is a standalone header only library for multithreaded programming. This library provides templated

CopperSpice 178 Jul 18, 2022
Portable header-only C++ low level SIMD library

libsimdpp libsimdpp is a portable header-only zero-overhead C++ low level SIMD library. The library presents a single interface over SIMD instruction

Povilas Kanapickas 1k Jul 21, 2022
Header-only library for multithreaded programming

CsLibGuarded Introduction The CsLibGuarded library is a standalone header only library for multithreaded programming. This library provides templated

CopperSpice 178 Jul 18, 2022
DwThreadPool - A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

dwThreadPool A simple, header-only, dependency-free, C++ 11 based ThreadPool library. Features C++ 11 Minimal Source Code Header-only No external depe

Dihara Wijetunga 26 May 29, 2022
Cpp-mempool - C++ header-only mempool library

cpp-mempool C++ header-only mempool library

Hardik Patel 13 Jun 21, 2022
A competitive programming helper tool, which packages included libraries into a single file, suitable for online judges.

cpack Cpack is a competitive programming helper tool, which packages the main source file along with included libraries into a single file, suitable f

PetarMihalj 11 Apr 22, 2022
Mx - C++ coroutine await, yield, channels, i/o events (single header + link to boost)

mx C++11 coroutine await, yield, channels, i/o events (single header + link to boost). This was originally part of my c++ util library kit, but I'm se

Grady O'Connell 4 Sep 21, 2019
Parallel-hashmap - A family of header-only, very fast and memory-friendly hashmap and btree containers.

The Parallel Hashmap Overview This repository aims to provide a set of excellent hash map implementations, as well as a btree alternative to std::map

Gregory Popovitch 1.5k Aug 9, 2022
Parallel-util - Simple header-only implementation of "parallel for" and "parallel map" for C++11

parallel-util A single-header implementation of parallel_for, parallel_map, and parallel_exec using C++11. This library is based on multi-threading on

Yuki Koyama 27 Jun 24, 2022
EOSP ThreadPool is a header-only templated thread pool writtent in c++17.

EOSP Threadpool Description EOSP ThreadPool is a header-only templated thread pool writtent in c++17. It is designed to be easy to use while being abl

null 1 Apr 22, 2022
Fiber - A header only cross platform wrapper of fiber API.

Fiber Header only cross platform wrapper of fiber API A fiber is a particularly lightweight thread of execution. Which is useful for implementing coro

Tony Wang 41 Jul 31, 2022
Fork of rpmalloc to be used with single thread applications and old C compilers

srpmalloc - Small rpmalloc This is a fork of rpmalloc, with the intent to be used in single threaded applications only, with old C99 compilers, and in

Eduardo Bart 6 Apr 30, 2022
Xenium is a noninteractive protocol for producing and redeeming single-use claim codes

Xenium Xenium is a noninteractive protocol for producing and redeeming single-use claim codes. What this means in practice is that you can have a devi

Nick Johnson 30 Jun 23, 2022