Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    421589

Multithreaded database hillarity

Sign in to follow this  
Evil Steve

85 views

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.
  • Sign in to follow this  


    0 Comments


    Recommended Comments

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now