Author: wyoung
Date: Thu Jan  3 18:38:39 2008
New Revision: 2083

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2083&view=rev
Log:
Recent cpoolp changes to Windows' cpoolw.

Modified:
    trunk/examples/cpoolw.cpp

Modified: trunk/examples/cpoolw.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/cpoolw.cpp?rev=2083&r1=2082&r2=2083&view=diff
==============================================================================
--- trunk/examples/cpoolw.cpp (original)
+++ trunk/examples/cpoolw.cpp Thu Jan  3 18:38:39 2008
@@ -67,7 +67,7 @@
                // Create connection using the parameters we were passed upon
                // creation.  This could be something much more complex, but for
                // the purposes of the example, this suffices.
-               cout << "Creating new pooled connection..." << endl;
+               cout << endl << "Creating new pooled connection!" << endl;
                return new mysqlpp::Connection(
                                db_.empty() ? 0 : db_.c_str(),
                                server_.empty() ? 0 : server_.c_str(),
@@ -79,7 +79,7 @@
        {
                // Our superclass can't know how we created the Connection, so
                // it delegates destruction to us, to be safe.
-               cout << "Destroying pooled connection..." << endl;
+               cout << endl << "Destroying pooled connection!" << endl;
                delete cp;
        }
 
@@ -98,6 +98,43 @@
 *poolptr = 0;
 
 
+static DWORD WINAPI
+worker_thread(LPVOID running_flag)
+{
+       // Pull data from the sample table a bunch of times, releasing the
+       // connection we use each time.
+       for (int i = 0; i < 6; ++i) {
+               // Go get a free connection from the pool, or create a new one
+               // if there are no free conns yet.
+               mysqlpp::Connection* cp = poolptr->grab();
+               if (!cp) {
+                       cerr << "Failed to get a connection from the pool!" << 
endl;
+                       break;
+               }
+
+               // Pull a copy of the sample stock table and print a dot for
+               // each row in the result set.
+               mysqlpp::Query query(cp->query("select * from stock"));
+               mysqlpp::StoreQueryResult res = query.store();
+               for (int j = 0; j < res.num_rows(); ++j) {
+                       cout.put('.');
+               }
+
+               // Immediately release the connection once we're done using it.
+               // If we don't, the pool can't detect idle connections reliably.
+               poolptr->release(cp);
+
+               // Delay 1-4 seconds before doing it again.  Because this can
+               // delay longer than the idle timeout, we'll occasionally force
+               // the creation of a new connection on the next loop.
+               Sleep(1000 * (rand() % 4 + 1)); 
+       }
+
+       // Tell main() that this thread is no longer running
+       *reinterpret_cast<bool*>(running_flag) = false;
+       
+       return 0;
+}
 int
 main(int argc, char *argv[])
 {
@@ -126,7 +163,30 @@
        // Setup complete.  Now let's spin some threads...
        cout << "Pool created and working correctly.  Now to do some "
                        "real work..." << endl;
+       srand(time(0));
+       bool running[] = {
+                       true, true, true, true, true, true, true,
+                       true, true, true, true, true, true, true };
+       const int num_threads = sizeof(running) / sizeof(running[0]);
+       int i;
+       for (i = 0; i < num_threads; ++i) {
+               if (CreateThread(0, 0, worker_thread, running + i, 0, 0) == 0) {
+                       cerr << "Failed to create thread " << i <<
+                                       ": error code " << GetLastError() << 
endl;
+                       return 1;
+               }
+       }
 
+       // Test the 'running' flags every second until we find that they're
+       // all turned off, indicating that all threads are stopped.
+       cout << endl << "Waiting for threads to complete..." << endl;
+       do {
+               Sleep(1000);
+               i = 0;
+               while (i < num_threads && !running[i]) ++i;
+       }
+       while (i < num_threads);
+       cout << endl << "All threads stopped!" << endl;
        // Shut it all down...
        delete poolptr;
 


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to