Thread: Need help designing first c++ app

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Need help designing first c++ app

    I'm stuck with the design of my first C++ application. I hope someone could take the time to give me some pointers. (I'll try to dereference them )

    I want to write a console application that does the following:
    -Recieve packets
    -Do a query in a database depending on the incoming requests
    -Send results back

    I won't bother you with the implementation details; I already have the libraries for networking and querying the database. I'd rather talk about the structure of my program.

    I'll explain the code I have in mind so it will be easyer to understand the problem:
    -There is a class Request that has attributes 'ID' (this has to be included in the packet that is returned) and a method 'execute()'.
    -There are several subclasses of Request, which add special variables to it to store specific data that this request needs. For example the class CheckPasswordRequest will add an int accountNumber and a char* Password.
    -main() goes into an infinite loop that calls the function recieve(), which will return a packet if one was recieved.
    -If a packet is returned from recieve(), I check what kind of request it is.
    -I initialise the correct class, set it's variables (like the accountNumber and password that was read from the packet).
    -I call it's execute() method.
    -The execute() method is a pure virtual function and for the class CheckPasswordRequest it will create an SQL query to retrieve the password for the given accountNumber. If it matches the given password, 1 is returned, 0 otherwise.

    That's as far as I got (with the design, that is). The problem I'm having is that the function that queries the database can take arbitrarily long to return. In the mean time, no packets can be processed (they'll just get queued). However, the database server is written to handle multiple queries at the same time which is especially beneficial on a multi-cpu system.

    So my code must somehow be able to send a second query to the databse, while the first has not returned yet. How would you do this in C++? Do I need multithreading?

    Also, how should I return the data from the query? Should the execute() function instantiate a specialised class to store the results for that specific query (some queries will return alot of data)? Would it be a good idea to store these return classes in a linked list so I can process them in the main loop (create a packet to be returned for each)?

    If there is anything wrong with my design so far, please also point that out.

    I understand it might be difficult to follow this post, if anything is unclear please don't hesitate to ask.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Have you written any code? If so, why not post it so others can see what you have done and comment, make suggestions ect.
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Hi swgh, thanks for your reply.

    I have not written any actual code yet (except for some messing around with the libraries to get a feel for them, but it is irrelevant to the design of my program). I'm trying to get a clear idea of how I will build my application first, before I actually start coding. This is especially important because since this is my first project in C++, I think I will get caught up in syntactical problems. If I don't design things upfront I will have to deal with _what_ to code and _how_ to code it at the same time, which seems like a bad idea.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If there is anything wrong with my design so far, please also point that out.
    Not that I can see.

    But then such designs are more of a guide to what to do rather than a specific set of instructions for success. It might actually work functionally, but have performance problems in say the database.

    Build in some clearly defined boundaries between the functional elements, so that rewriting one part doesn't affect another part.

    > Do I need multithreading?
    Does the library support multi-threaded access?
    Does the library support asynchronous access - say it invokes a callback when the request is complete.

    Thread programming is much harder than most people imagine
    http://www.kuro5hin.org/?op=displays...1/18/22112/860
    Get one unguarded access to a variable, and you'll be chasing hard to find race conditions for the life of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Yes, the library supports multithreaded access (or so I have read, I have never attempted to write a multithreaded application). If you know anything about it: it is the MySQL C library.

    Not sure about asynchronous access. Actually I'm not sure what it means, exactly. Does it mean that the function will return immediately and later on call some function of my code when it is actually done?

    I've done some testing with the following code:
    Code:
          std::cout<<"Starting query\n";
          mysql_query(mysql, "SELECT BENCHMARK(10000000,ENCODE('hello','goodbye'))");
          std::cout<<"Query finished\n";
          std::cout<<"Starting result retrieval\n";
          results = mysql_store_result(mysql) ;
          std::cout<<"Result retrieval finished\n";
    The SQL statement takes around 6 seconds to complete, so I can easily see what happens. "Starting query" is printed as soon as I start the program. It then takes 6 seconds before "Query finished" is printed. After that, the last two lines are printed seemingly instantaneously.

    So if my assumption of what asynchronous access is, is correct it is not supported.
    Last edited by C+/-; 12-31-2006 at 10:37 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Does it mean that the function will return immediately
    > and later on call some function of my code when it is actually done?
    Yeah, that's it exactly - except I couldn't find anything in the manual (though I'm no mySQL expert).

    Found this though
    http://dev.mysql.com/tech-resources/...719/index.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks, I hadn't seen that page before but it will certainly come in handy.

    I'd like to stay away from threads, but it seems like there is no other way. If this application is run on a multi-cpu system, I will only be able to make use of one of the CPUs. MySQL has no problem dealing with multiple queries at the same time (and using multiple CPUs for them), but if I have to wait for a query to complete before I can send the next one, there is obviously only one query being executed at once and so only one CPU will be utilized.

    Is there another way to solve/circumvent this problem given the way the MySQL C API currently works? Will threaded programming really be so difficult for such a relatively simple application?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. best program to start
    By gooddevil in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-28-2004, 05:56 PM
  3. Need help migrating console app to windows app
    By DelphiGuy in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 07:05 PM
  4. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM
  5. How do I make my Linux app standalone?
    By Joda in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 04:53 AM