• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ Async database

Itutorial

Legendary OT User
Joined
Dec 23, 2014
Messages
2,426
Solutions
68
Reaction score
1,075
How hard is it to implement async database calls? It seems pretty easy based on what I have read. What things should I consider? I have created this code which theoretically will execute a query async. What problems could happen from this?

C++:
std::future<bool> Database::executeAsyncQuery(const std::string& query)
{
    return std::async(std::launch::async, [this, query] { return executeQuery(query); });
}

Then to get and do something with the result after it returns:
C++:
std::future<bool> resultFuture = Database::getInstance().executeAsyncQuery("SELECT * FROM users");

if (resultFuture.get()) {
    std::cout << "Query executed successfully!" << std::endl;
} else {
    std::cerr << "Query execution failed!" << std::endl;
}
 
You'll need to read a bit more. This is not async, .get effectively blocks current thread by awaiting the result. Generally std::async in c++ is mostly useless, it's only nice if you want to start computing something beforehand and do other work in the same time.

TFS has async connection to database, it's in DatabaseTask, it's done by spawning another thread that processes task queue like dispatcher, your main problem is and will always be synchronization, but it depends where used and how. You can't write asynchronous code like synchronous code and it starts being ugly really fast and hard to manage.
 
Back
Top