boost thread 多线程返回用法?
请问各位大牛,小弟最近须用上 多线程。 有下面一例子,想问在在多线程编程的返回
代码如下:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <ctime>
#include <cstdlib>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost ;
void Test (int B , int &C )
{
? for (int i=0 ; i<B ; i++)
? {
? C+=B;
? }
? cout<<B<<"\t"<<C<<endl;
}
int main(int argc,char *argv[])
{
? thread_group M_trhead ;
? int C=0,D=0;
? M_trhead.create_thread(bind(Test , 10 ,C ));
? M_trhead.create_thread(bind(Test , 5 ,D ));
? M_trhead.join_all();
? cout<<C<<"\t"<<D<<endl;
? return 0 ;
}
////////////////////////swimming in the sea & flying in the sky //////////////////
结果运行是 :
10 100
5 25
0 0
但我想到的结果是?
10 100
5 25
100 25
不知如何修改?
[解决办法]
boost::bind会在传递参数的时候产生一个拷贝,可以使用boost::ref(C)来传递引用