As a beginner in D and Vibe programming I have a hard time finding code examples online.
I'm currently trying to find out how to use vibe's fiber friendly mutexes. Is the following code the correct way to use mutexes ? I suppose synchronizing on the object is nos OK because the mutex in the base object is not a fiber friendly mutex.
final class Store {
auto mtx = new TaskMutex;
int count = 0;
// Return the incremented value of count in a string
string getIncrementedCount()
{
synchronize(mtx)
{
count += 1;
return to!string(count);
}
}
}
void get(HTTPServerRequest req, HTTPServerResponse res)
{
string str = g_store.getIncrementedCount();
render!"index.dt"(res, str);
}
shared Store g_store;
shared static this()
{
g_store = new Store;
auto router = new URLRouter;
router.get("/", &get);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
logInfo("Open http://127.0.0.1:8080/ in your browser");
}