# libtmux::command_sender

- **Module:** libtmux
- **Package:** libtmux-cxx
- **Language:** C++
- **Kind:** function
- **Source:** https://github.com/libtmux/libtmux-cxx/blob/75ff8098f8b2a6d7690962ba82087b19d086b5b8/include/libtmux/execution.hpp#L217
- **Page:** https://libtmux.org/en/cxx/latest/reference/libtmux-command_sender/

```
template <stdexec::scheduler Scheduler>
[[nodiscard]] auto
command_sender(Scheduler scheduler, const Server& server,
               CommandRuntime& runtime,
               CommandRequest request,
               stdexec::inplace_stop_token stop = {});
```

Schedules admission and receiver completion on the supplied scheduler.

A scheduler that dispatches on another thread must return from posting the work without waiting for the receiver. Inline completion is supported. The runtime hands off an admitted result from its completion thread. An inline scheduler can run a receiver on that thread. Receivers running there must return promptly and arrange runtime shutdown on another thread.

Owns the server and request. The connected operation owns the receiver. Keep the operation state alive until receiver completion begins. Keep the runtime, scheduler and explicit stop source alive through completion, even if `CommandRuntime::close` has already returned.

The value channel carries the output string. The error channel carries `CommandFailure`, including delivery information, or `std::exception_ptr`. The scheduler can also stop admission or delivery. Admission forwards scheduler errors. Delivery scheduling errors use `std::exception_ptr`. Receiver stop can stop admission or request transport cancellation after admission. The optional explicit token also requests transport cancellation. Global observers still require `CommandRuntime::dispatch_ready`.

Run example.

## Type parameters

- `Scheduler` (constraint `stdexec::scheduler`)

## Parameters

- `scheduler` (`Scheduler`)
- `server` (`const Server &`)
- `runtime` (`CommandRuntime &`)
- `request` (`CommandRequest`)
- `stop` (`stdexec::inplace_stop_token`)
  Default: `{}`.

## Examples

Fetch the library version documented on this page.

```console
$ git clone https://github.com/libtmux/libtmux-cxx.git libtmux-source && \
  git -C libtmux-source checkout 75ff8098f8b2a6d7690962ba82087b19d086b5b8
```

Build and run the optional sender example on POSIX. The linked guide also covers installed packages, cancellation and shutdown.

[Source example](https://github.com/libtmux/libtmux-cxx/blob/75ff8098f8b2a6d7690962ba82087b19d086b5b8/docs/execution.md).

```console
$ cd libtmux-source
$ cmake -S . \
    -B build/command-senders \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DCMAKE_CXX_FLAGS=-stdlib=libc++ \
    -DLIBTMUX_BUILD_TESTS=OFF \
    -DLIBTMUX_BUILD_EXAMPLES=ON \
    -DLIBTMUX_EXAMPLES_BUILD_EXECUTION=ON \
    -DLIBTMUX_FETCH_DEPS=ON
$ cmake --build build/command-senders \
    --target libtmux_example_09_command_senders
$ ./build/command-senders/examples/libtmux_example_09_command_senders
```

While tmux waits for a signal, the same scheduler worker can admit the command that sends it.

[Source example](https://github.com/libtmux/libtmux-cxx/blob/75ff8098f8b2a6d7690962ba82087b19d086b5b8/examples/09-command-senders.cpp).

```cpp
#include <chrono>
#include <exception>
#include <future>
#include <iostream>
#include <tuple>
#include <utility>

#include <exec/static_thread_pool.hpp>
#include <libtmux/batch.hpp>
#include <libtmux/execution.hpp>
#include <libtmux/testing/scoped_server.hpp>
#include <stdexec/execution.hpp>

namespace {

int report_failure(const libtmux::CommandFailure& failure) {
  std::cerr << libtmux::to_string(failure) << '\n';
  return 1;
}

} // namespace

int main() try {
  using libtmux::command_sender;
  using namespace std::chrono_literals;
  constexpr auto timeout = 5s;

  auto fixture = libtmux::test::ScopedTmuxServer::start(
      {.socket_namespace = libtmux::test::SocketNamespace::consumer("sender")});
  if (!fixture) {
    std::cerr << fixture.error() << '\n';
    return 1;
  }
  const auto& path = fixture->socket_path();
  auto server = libtmux::Server::at_socket_path(path, {}, {.timeout = timeout});
  if (!server) {
    return report_failure(server.error());
  }

  exec::static_thread_pool pool{1};
  auto runtime = libtmux::CommandRuntime::start({.capacity = 2});
  if (!runtime) {
    return report_failure(runtime.error());
  }
  const auto scheduler = pool.get_scheduler();

  libtmux::CommandBatch batch;
  batch.add({"wait-for", "-S", "entered"});
  batch.add({"wait-for", "release"});
  batch.add({"display-message", "-p", "tmux released the wait"});
  auto wait = command_sender(scheduler, *server, *runtime, batch.request());
  auto wait_future =
      std::async(std::launch::async, stdexec::sync_wait, std::move(wait));

  if (auto entered = server->wait_for("entered", timeout); !entered) {
    return report_failure(entered.error());
  }
  libtmux::CommandRequest release{"wait-for", "-S", "release"};
  const auto signalled = stdexec::sync_wait(
      command_sender(scheduler, *server, *runtime, std::move(release)));
  const auto waited = wait_future.get();
  if (!signalled || !waited) {
    std::cerr << "a command sender stopped\n";
    return 1;
  }
  if (const auto closed = runtime->close(); closed.failure) {
    return report_failure(*closed.failure);
  }
  std::cout << std::get<0>(*waited);
  return 0;
} catch (const libtmux::CommandFailure& failure) {
  return report_failure(failure);
} catch (const std::exception& failure) {
  std::cerr << failure.what() << '\n';
  return 1;
}
```

Expected output:

[Source example](https://github.com/libtmux/libtmux-cxx/blob/75ff8098f8b2a6d7690962ba82087b19d086b5b8/examples/09-command-senders.out).

```text
tmux released the wait
```
