容器作为参数在一般的情况是时没有问题;一般情况下使用的方法如下:
--------------------------------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <vector>
using namespace std;
void fun(vector<int>& vecInt)
{
for (int i = 0; i < 5;i++)
{
vecInt.push_back(i);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vecInt;
fun(vecInt);
vector<int>::iterator it = vecInt.begin();
for (; it != vecInt.end(); it++)
{
cout << *it << endl;
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
但是在使用boost的线程的时候出现问题,如下:
--------------------------------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <vector>
#include <algorithm>
//boost
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
using namespace boost;
using namespace std;
void fun(vector<int>& vecInt)
{
for (int i = 0; i < 5;i++)
{
vecInt.push_back(i);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vecInt;
thread th(&fun, vecInt);
th.join();
for_each(vecInt.begin(), vecInt.end(), cout << boost::lambda::_1 << "\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
在运行的时候,没有添加容器中。使用CreateThread是没有问题的,没有找到具体的原因,先不纠结了。。。。