0

The code below gives the following error, however the function without threads and async works well:

main.cpp:38:93: error: no matching function for call to ‘async(std::launch, void (&)(sqlite3*, std::string*, std::ofstream&), sqlite3*&, std::string*, std::ofstream&)’
   38 | ultFromDB = std::async(std::launch::async, writeToDB, DB, &z, logFile);

Can you please show the source of error?

// C library headers
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>

// db
#include <sqlite3.h>

// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()

// multithreading
#include <chrono>
#include <thread>
#include <future>

using namespace std;

void writeToDB(sqlite3* DB,std::string* state, ofstream& logFile)
{
    // do some work
}

int main()
{
    std::ofstream logFile;
    logFile.open ("a.txt");
    sqlite3* DB; 
    for(int i = 0; i < 10; i++)
    {
        string z = "123";
        std::future<void> resultFromDB = std::async(std::launch::async, writeToDB, DB, &z, logFile);
    }
}

I understand that there is something about pointers and params, but I am not advanced in CPP. Thank you.

EDIT: After I replaced logFile with std::ref(logFile), I get this error:

/usr/bin/ld: /tmp/ccl0RarK.o: undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
/usr/bin/ld: /usr/lib/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
jokey
  • 21
  • 6
  • Does this answer your question? [Passing arguments to std::async by reference fails](https://stackoverflow.com/questions/18359864/passing-arguments-to-stdasync-by-reference-fails). – François Andrieux Aug 10 '20 at 13:56
  • Aside: Luckily for you, `resultFromDB`'s destruction at the end of the block waits on that future, otherwise `state` in `writeToDB` might become invalid from under you. Unfortunately that means you do each write sequentially. – Caleth Aug 10 '20 at 14:07
  • @Caleth That is a very good point, passing by value might be appropriate here. Perhaps `std::shared_ptr` for logFile. – Quimby Aug 10 '20 at 14:10

1 Answers1

2

std::thread and std::async do not accept references implicitly, you have to wrap them in std::ref:

std::async(std::launch::async, writeToDB, DB, &z, std::ref(logFile));
Marek R
  • 32,568
  • 6
  • 55
  • 140
Quimby
  • 17,735
  • 4
  • 35
  • 55
  • Ok, this solved the problem, but now, I get this: `/usr/bin/ld: /tmp/ccl0RarK.o: undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' /usr/bin/ld: /usr/lib/libpthread.so.0: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status` – jokey Aug 10 '20 at 14:06
  • 1
    On linux with gcc you have to explicitly link with POSIX threads. Add `-pthread` option to the compiler. – Quimby Aug 10 '20 at 14:08
  • Are these proccesses totally independent from each other? – jokey Aug 10 '20 at 14:28
  • What processes? `std::async` launches threads or reuses them from a thread pool. – Quimby Aug 10 '20 at 14:56