RejectedSoftware Forums

Sign up

Access __gshared variable from a function

I would like to access a __gshared variable from a function. The code snippet is like this:

import vibe.d;

struct Student
{
    string name;
    uint number;
}

class StudentStore
{
    Student[][string] store;
    static Student[0] empty;
    Student[] getNotes(string id)
    {
         return (id in store) ? store[id] : empty;
    }
    private __gshared StudentStore studentStore;
    shared static this()
    {
        studentStore = new StudentStore();
    }
}

void listStudents(HTTPServerRequest req, HTTPServerResponse res)
{
    auto allstudents = studentStore.getNotes(req.session.id);
}

void main()
{
    auto router = new URLRouter;
    router.get("/", staticTemplate!"index.dt");
    router.get("*", serveStaticFiles("public/"));

    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    settings.bindAddresses = ["::1", "127.0.0.1"];
    listenHTTP(settings, router);

    logInfo("Lütfen tarayıcınızda http://127.0.0.1:8080/ adresini açınız.");
    runApplication();
}

As I know, there are two ways to make a variable available across threads in D: __gshared and shared .

The former has no guarantees. All it does is put the variable in global storage, making it just like any global variable in C or C++.

The latter actually affects the type of the variable; the compiler is able to help prevent the variable from being used in ways it shouldn't be.

When I try to access __gshared variable studentStore, I get undefined identifier error.

source/app.d(26,24): Error: undefined identifier studentStore, did you mean class StudentStore?

What might be the cause of this?

Re: Access __gshared variable from a function

On Thu, 12 Dec 2019 07:25:17 GMT, Erdem wrote:

When I try to access __gshared variable studentStore, I get undefined identifier error.

source/app.d(26,24): Error: undefined identifier studentStore, did you mean class StudentStore?

What might be the cause of this?

Ok. I've recognized that my initial code was faulty. Here is the working one.

import vibe.d;

struct Student
{
    string name;
    uint number;
}

class StudentStore
{
    Student[][string] store_;
    static Student[0] empty_;

    Student[] getNotes(string id)
    {
         return (id in store_) ? store_[id] : empty_;
    }

}

private __gshared StudentStore studentStore;

shared static this()
{
    studentStore = new StudentStore();
}

void listStudents(HTTPServerRequest req, HTTPServerResponse res)
{
    auto allstudents = studentStore.getNotes(req.session.id);
}

void main()
{
    auto router = new URLRouter;
    router.get("/", staticTemplate!"index.dt");
    router.get("*", serveStaticFiles("public/"));

    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    settings.bindAddresses = ["::1", "127.0.0.1"];
    listenHTTP(settings, router);

    logInfo("Lütfen tarayıcınızda http://127.0.0.1:8080/ adresini açınız.");
    runApplication();
}

Should I move the line within shared static this {} which constructs a student Store object to main function ? Or leave as is.