00001
00002
00003
00004
00005 #include "async-worker.h"
00006 #include <OpenThreads/Thread>
00007 #include <string>
00008 #include <stdio.h>
00009
00011
00014
00015 class DBOffload : public async::dispatch_t
00016 {
00020 public: DBOffload(const std::string& query) : dispatch_t(), m_query(query) {}
00021
00025 public: virtual void Work()
00026 {
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 OpenThreads::Thread::microSleep(5000) ;
00040 printf("query being done while main thread does something else (sleeps in this case)\n") ;
00041 printf("%s\n", m_query.c_str()) ;
00042 OpenThreads::Thread::microSleep(5000) ;
00043 printf("Done, this worker will now self-destruct\n") ;
00044 }
00045
00047 private: std::string m_query ;
00048 } ;
00049
00050 int main(int argc, const char* const argv[])
00051 {
00052
00053 async::Queue(new DBOffload("INSERT INTO `user` (`beverage`, `quantity`) VALUES ('coffee', 'significant')")) ;
00054
00055
00056 DBOffload* work = new DBOffload("DELETE FROM `user` WHERE `beverage` = 'nasty tasting'") ;
00057 work->Queue() ;
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 printf("[originating thread is ''busy'' doing other stuff now]\n") ;
00068 OpenThreads::Thread::microSleep(1 * 1000 * 1000) ;
00069 printf("[originating thread has finished. is it over now?]\n") ;
00070
00071 return 0 ;
00072 }
00073