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