【发布时间】:2014-03-14 09:31:18
【问题描述】:
我创建了两个线程,它们将并行执行一段代码。我想在两个线程完成执行后运行另一个方法。我尝试过 join() 和 timed_join(),但它不起作用。我我正在使用 boost 线程类。请在下面找到我的代码:
代码:
A class:
A a;
boost::thread t(boost::bind(&A::method1,&a,s1,e1));//Call method1 using two parameters s1 and e1.boost bind is used to call class methods
boost::thread t1(boost::bind(&A::method1,&a,s2,e2));//Call method1 using two parameters s2 and e2.boost bind is used to call class methods.
t.join();
t1.join();
methodToBeExecutedAfterThreadExec();
让我知道如何使用线程并行执行方法 1,然后我必须调用 methodToBeExecutedAfterThreadExec() 方法。
提前致谢。
编辑 1:
void method1(int start,int end)
{
vector< vector<string> >::iterator row;
vector<string>::iterator col;
db.onAutoVacuumMode();//Vacuum mode is set to true which eliminates fragmentation
db.startTransaction();//Starts the transaction which does the stuff in a batch wise
for (row = dupViewResults.begin()+start; row != dupViewResults.begin()+end; ++row) {
std::string filedata=readFileDataInStr(row->at(0));
int x = boost::lexical_cast<int>( row->at(1) );
long long int fileCRC32=MurmurHash(filedata.c_str(),x,0);
std::string strCRC32=std::to_string(fileCRC32);
std::string query="update duplicatesview set CRC32='"+strCRC32+"' where Filepath='"+row->at(0)+"'";
char* charQuery = &query[0];
db.fireSQLQuery(charQuery,results);
}
db.endTransaction();
}
上面的代码将读取向量 dupViewResults(我的代码已经填充了它)然后更新视图,然后在事务中触发 sql 查询。谢谢
【问题讨论】:
-
“它不工作”不是一个有用的问题描述。您的代码看起来不错。
-
__debugbreak() 类 dbgrptt.c 在第二次连接中被命中。不知道为什么。
-
可能是因为
method1或其调用的某些函数以不安全或不正确的方式被调用。给我们足够的代码来重现问题。 -
已添加。感谢您的帮助。
-
db对象是否自动将事务与线程关联?如果不是,它如何知道调用endTransaction时哪个事务正在结束?
标签: c++ multithreading boost