简体   繁体   English

使用指针向量创建线程

[英]creating threads using vector of pointers

ok new try, 好的新尝试,

i changed my code from a normal vector<Individual> to vector<shared_ptr<Individual>> . 我将代码从常规vector<Individual>更改为vector<shared_ptr<Individual>> since that, my threading code doesn't work and i don't have a clue on how to fix it. 从那以后,我的线程代码不起作用,而且我不知道如何解决它。 Start() is a void member of Individual, but how do i call it if there are only pointers to those? Start()是Individual的无效成员,但是如果仅存在指向这些成员的指针,我该怎么称呼它?

std::vector<thread> threads;
for (int i=0;i<(Individuals.size()-howManyChampions-howManyMutations);i++) {
    threads.push_back(thread(&Individual::start,&Individuals.at(i)));
    if (threads.size() % 5 == 0) {
        for(auto& thread : threads){
            thread.join();
        }
        threads.clear();
    }
}
for(auto& thread : threads){
    thread.join();
}

it works when i change it back to just vector and the non-parallel version: 当我将其更改回vector和非并行版本时,它将起作用:

 for (int i=0;i<(int)Individuals.size();i++) {
    Individuals.at(i)->start();
}

works like a charm, too. 就像魅力一样。 So i assume there is something wrong at the thread push_back? 所以我认为线程push_back有问题吗? error messages (roughly translated) are: 错误消息(大致翻译为)是:

 instance created from »_Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&, _ArgTypes ...) const [mit _Tp = std::shared_ptr<Individual>*, _Res = void, _Class = Individual, _ArgTypes = {}]«|
 instance created from »void std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__call(std::tuple<_Args ...>&&, std::_Index_tuple<_Indexes ...>, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type) [mit _Res = void, _Args = {}, int ..._Indexes = {0}, _Result = void, _Functor = std::_Mem_fn<void (Individual::*)()>, _Bound_args = {std::shared_ptr<Individual>*}, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type = int]«|
 instance created from »std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type std::_Bind_result<_Result, _Functor(_Bound_args ...)>::operator()(_Args&& ...) [mit _Args = {}, _Result = void, _Functor = std::_Mem_fn<void (Individual::*)()>, _Bound_args = {std::shared_ptr<Individual>*}, std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type = void]«|
 instance created from »void std::thread::_Impl<_Callable>::_M_run() [mit _Callable = std::_Bind_result<void, std::_Mem_fn<void (Individual::*)()>(std::shared_ptr<Individual>*)>]«|
 instanziiert von hier|
 Pointer to element type »void (Individual::)()« with Objecttype »std::shared_ptr<Individual>« incompatible
 Return-Command with value in »void« returning Function [-fpermissive]

thanks guys 多谢你们

Try this : 尝试这个 :

threads.push_back(thread(&Individual::start,Individuals.at(i).get()));

You can access the pointer hold by a shared_ptr with get method. 您可以使用get方法访问shared_ptr持有的指针。 However you should make sure that you do not dispose all instances of this shared_ptr before you join all threads or else they will have a dangling pointer of Individual . 但是,您应该确保在加入所有线程之前不要丢弃此shared_ptr所有实例,否则它们将具有悬垂的Individual指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM