0

i'm trying to run a listener Method async with std::async but i get the following error: No instance of overloaded function "std::async" matches argument list

auto listener = std::async(std::launch::async, server.Listen());

The server.Listen() returns void (nothing) I've already read throu https://en.cppreference.com/w/cpp/thread/async for reference.

template< class Function, class... Args >
std::future<std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>>
    async( std::launch policy, Function&& f, Args&&... args );


template< class Function, class... Args >
std::future<std::invoke_result_t<std::decay_t<Function>, std::decay_t<Args>...>>
    async( std::launch policy, Function&& f, Args&&... args );

I'm using C++20 with CMAKE.

The full main.cpp Code:

#include <iostream>
#include <future>
#include <string>
#include <stdio.h>
#include "../<redacted>/IPC/UnixSocket/Server.h"

// https: // github.com/MikeShah/moderncppconcurrency/blob/main/async_buffer.cpp

int main()
{
    printf("Hello World from <redacted>.Demo.IPCServer!\n");
    <redacted>::IPC::UnixSocket::Server server(1556, SOCK_STREAM, INADDR_ANY);

    auto listener = std::async(std::launch::async, server.Listen());

    return 0;
}

i also get an intellisense issue on all the std::async or std::future types: <error-type> - i don't know if this might also be an indicator for something.

Can someone help?

Oliver Karger
  • 105
  • 1
  • 11
  • 3
    `std::async` expects a callable object, not something it returns. Probably, you want something like `async(..., &Server::Listen, std::ref(server));`. – Evg Nov 05 '22 at 13:14
  • 4
    I advise against using `` as placeholder. Use something that results in valid C++ syntax instead. e.g. using `myproject` would serve the same purpose without potentially confusing the reader. – fabian Nov 05 '22 at 13:21
  • 3
    I prefer to use a lambda in situations like this e.g. `auto listener = std::async(std::launch::async, [&]{server.Listen();});`. – Pepijn Kramer Nov 05 '22 at 13:30
  • 2
    You can write an answer for your own question if you like, but you should not you not add solutions to the question. – 463035818_is_not_an_ai Nov 05 '22 at 13:53

1 Answers1

1

Solution

thanks y'all for your answers, both solutions worked!
and also thanks for the comment on the <redacted> 'practice'

the working code:

auto listener = std::async(
    std::launch::async, 
    &myprojectlibrary::IPC::UnixSocket::Server::Listen, 
    std::ref(server)
);
auto listener = std::async(
    std::launch::async, 
    [&]{ server.Listen(); }
);
Oliver Karger
  • 105
  • 1
  • 11