struct Data_1{
string stm;
...
};
void get_child_tree_thread(void *arguments){
vector<Data_1> v_result;
Data_1 a,a1;
a.stm = "1234";
a1.stm = "1235";
v_result.push_back(a);
v_result.push_back(a1);
std::vector<Data_1>* vec = new std::vector<Data_1>(v_result.size());
vec->assign(v_result.begin(),v_result.end());
**return vec;**
}
int main(void)
{
if (pthread_create(&thread,NULL, get_child_tree_thread,NULL)){
cerr << "create thread error" << endl;
cout << "create thread fail->" << endl;
exit(0);
}
sleep(3);
void *threadResult = NULL;
cout << "main 3" << endl;
pthread_join(thread, &threadResult);
std::vector<Data_1> *v_child_out = (std::vector<Data_1> *)((std::vector<Data_1>* )threadResult);
cout << "p join v_child_out size" << v_child_out->size()<< endl;
}
如何正确投射矢量? ->
std::vector<Data_1> *v_child_out = (std::vector<Data_1> *)((std::vector<Data_1>* )threadResult);
cout << "p join v_child_out size" << v_child_out->size()<< endl;
您的
get_child_tree_thread()
函数被声明为错误。 它需要返回 void*
而不是 void
(你的编译器应该已经发现了这个错误):
void* get_child_tree_thread(void *arguments){
...
auto* vec = new std::vector<Data_1>(...);
...
return vec;
}
然后,可以通过删除多余的转换来简化
threadResult
的类型转换(一次就足够了,但要转换两次):
int main()
{
...
void *threadResult = nullptr;
pthread_join(thread, &threadResult);
auto *v_child_out = static_cast<std::vector<Data_1>*>(threadResult);
...
delete v_child_out;
}
然后,您可以通过其输入参数将指向
vector
对象的指针传递给线程,从而进一步简化代码,并完全摆脱 new
,例如:
void* get_child_tree_thread(void *argument){
auto *v_result = static_cast<std::vector<Data_1>*>(argument);
...
v_result->push_back(...);
...
return nullptr;
}
int main()
{
std::vector<Data_1> v_child_out;
if (pthread_create(&thread, nullptr, get_child_tree_thread, &v_child_out)){
std::cerr << "create thread error" << std::endl;
std::cout << "create thread fail->" << std::endl;
return 0;
}
sleep(3);
std::cout << "main 3" << std::endl;
pthread_join(thread, nullptr);
std::cout << "p join v_child_out size" << v_child_out.size() << std::endl;
}
现在,您根本不应该使用 pthreads。使用 C++ 原生的
std::thread
来代替:
#include <thread>
void get_child_tree(std::vector<Data_1> &v_result){
...
v_result.push_back(...);
...
}
int main()
{
std::vector<Data_1> v_child_out;
std::thread thd(get_child_tree, std::ref(v_child_out));
sleep(3);
std::cout << "main 3" << std::endl;
thd.join();
std::cout << "p join v_child_out size" << v_child_out.size() << std::endl;
}
甚至
std::async()
和std::future
:
#include <future>
std::vector<Data_1> get_child_tree(){
std::vector<Data_1> v_result;
...
v_result.push_back(...);
...
return v_result;
}
int main()
{
auto f = std::async(std::launch::async, get_child_tree);
sleep(3);
std::cout << "main 3" << std::endl;
auto v_child_out = f.get();
std::cout << "p join v_child_out size" << v_child_out.size() << std::endl;
}
首先不要使用 pthreads,使用 C++ 的本机线程支持。 (自 C++11 以来一直存在)如果您有一个线程可以生成某些内容,则使用
std::async
/std::future
。
#include <future>
#include <vector>
std::vector<int> produce_vector()
{
return std::vector<int>{ 1, 2, 3, 4, 5 };
}
int main()
{
// run produce_vector on a background thread
auto future = std::async(std::launch::async, produce_vector);
// this will block until the vector is ready and get the vector
auto vec = future.get();
return 0;
}