Multithreaded database hillarity

posted in DruinkJournal
Published March 06, 2006
Advertisement
I finally got around to writing some multithreaded database functions. You can use the blocking methods still, they just execute the SQL query in the current thread, which is useful at startup or some other time you don't care about blocking.

Here's a code snippet on how to request a user from the database:
CDatabase::Result theResult;User* pUser = CDatabase::Get().AllocateUser();HANDLE h = CDatabase::Get().RequestUser(1,pUser);while(!CDatabase::Get().OperationComplete(h,theResult))   Sleep(10);CDatabase::Get().FreeUser(pUser);

In my test, it took 4 loops in there (~40ms) to actually get the user data out, so this should definitely give me a performance boost.
The AllocateUser() and FreeUser() functions use a templated free list inside the database class, just to save the app calling new and delete. It might be better placed elsewhere, but I figure it's a good place for it, since only really the database deals with User structures.

A bit of info on what happens internally:
  • The main thread generates a HANDLE for querying the result status (Just an incrementing integer), and posts a message saying "Get the user with this ID, and chuck it into the buffer provided"
  • The worker thread picks up that message, executes the SQL query, and posts a result in an output queue
  • The CDatabase::OperationComplete() function checks the output queue, and if the result with the corresponding handle is in there, it fills the result buffer and returns true
    The Result structure contains a pointer to a buffer, the length of the buffer, and an error code for if the query failed.

    Anyway, I suppose I'd better go do some other code now. I was considering re-tackling IO Completion Ports, but I might leave that for another day.

    EDIT:
    Yay, I can now happily connect through the login server to a game server, with all nice authentication. Next up, I need to make some fake code for sending messages to players, then I can get stress testing.
    Hopefully, it should all be ready for a first stress test in a couple of days.
  • Previous Entry Random Updates
    0 likes 0 comments

    Comments

    Nobody has left a comment. You can be the first!
    You must log in to join the conversation.
    Don't have a GameDev.net account? Sign up!
    Advertisement
    Advertisement