花了前期后后应该一整天的时间来搞一个很蛋疼的问题,现在的话,哎,或多或少算是解决吧,说说过程和最后的结果吧
首先是这部分,在boost的例程中的源码:
for (std::size_t i = 0; i < io_services_.size(); ++i) { boost::shared_ptr<boost::thread> thread(new boost::thread( boost::bind(&boost::asio::io_service::run, io_services_[i]))); threads.push_back(thread); }
就是每次启动一个线程就拿ioservice的run来作为启动函数,那我现在像在客户端实现下多线程的效果,实现下并发或者是同时访问服务端的效果,那么我就自己写个launch_client的函数呗,像每次一个线程启动的时候,以这个作为启动函数,然后每次从哪个ioservice的vector传个不同的ioservice对象进去,然后那个函数就自然会向服务端发起不同的请求了,好吧,结果发现我对bind的理解是在太naive了,分点说下几个不能干的事情吧:
第一:不能这样传io_serviced的,因为它是noncopyable的,如果这样传的话就会默认调用它的那个复制构造函数,而为什么例程那里可以呢,因为它是调用自己的类本身的函数,所以就可以,反正就这样
第二:我最终的是这样的:
for (int thread_num = 1; thread_num <= 5; thread_num ++) { cout << "Now Thread:" << thread_num << "is about to be executed...\n"; /* boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&simple_print, thread_num)));*/ boost::asio::io_service& s = *io_services[thread_num]; /* lauch_client(s,1,1);*/ /* boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &get_io_service[thread_num])));*/ /* boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&lauch_client, get_io_service(thread_num), POST_REQUEST, thread_num)));*/ boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&io_service_pool::lauch_client, &io_pool, thread_num))); /* boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&lauch_client, io_pool, POST_REQUEST, thread_num)));*/ // thread_pool.push_back(thread); // thread->join(); }
main函数负责跑起一个vector的线程,然后io_pool负责管理一堆io_service,所以在main这外面的话就不用去管了, 反正跟线程的id是一一对应的就只传个id进去就好了,记得!!!!!第二参数!要传个你所调用的那个类的对象的引用进去!!!!!!!!!!如果是在类里面的方法启动线程的话,启动函数是这个类的其他方法的话,就传个this进去,或者是boost里面的shared_from_this()
尼玛的傻逼vs在模板相关的编译出错的时候的提示真心傻逼,你能不能跟我说却了个参数并且给出参数的类型?那样我猜也好猜啊!,哎,反正就是这样了
第三:对了,上面说不能直接搞个ioservice进去的,那反正在launch client方法里面去搞就可以了,把io_service拿出来然后直接用函数调用的方式来传给其他的函数,还是合法可行的。
第四:launch client是非静态的,静态的话就不用加个&在前面,不过还是这样好,静态方法里面又诸多限制的,又不能用非静态的成员变量
第五:在launch client方法里面,就这样开始链接就好了:。。。。
void io_service_pool::lauch_client(int thread_id) { //Handle the post request using the file according to the thread_id boost::asio::io_service& io_service = *io_services_[thread_id]; string strs[5]; char tmp_local[30] = ""; char tmp_remote[30] = ""; itoa(thread_id + 10, tmp_remote, 10); itoa(thread_id, tmp_local, 10); strs[0] = "localhost"; strs[1] = "/test"; strs[1] += tmp_remote; strs[1] += ".txt"; string local_path = "I:/test"; local_path += tmp_local; local_path += ".txt"; try { tcp::resolver resolver(io_service); tcp::resolver::query query(strs[0].c_str(), "http"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::socket socket(io_service); boost::asio::connect(socket, endpoint_iterator); cout << "Now the Client:" << thread_id << "is making a POST request...\n"; request req; req.make_post_request(local_path, strs[0], strs[1]);
ok,测试通过了!哎~搞了那么久~总算有点对多线程上手了,不过现在还是很简单的,线程之间没有竞争的情况,接下来就要在server端来实现了,好吧,此篇结束,睡觉,下午打球!
本文记录了作者使用Boost.Asio库实现客户端多线程并发访问服务端的过程,详细探讨了线程启动函数、bind函数使用技巧及非拷贝构造的io_service对象传递等问题。
1475

被折叠的 条评论
为什么被折叠?



