0
votes

I'm trying to do a Thread calling the cook class through a vector but I don't know to do it correctly. Here is my code:

for (int i = 0; i < _numCooks; i++) {
     std::thread threadObj(this->_cooks[i].nextOrder, void);
    _threads.push_back(threadObj);
}

_cooks is a vector of a class and the function that I am trying to make a thread of is:

bool cooks::nextOrder()
{
    Margarita margarita(_cookOrder[0].pizzaSize);
    Regina regina(_cookOrder[0].pizzaSize);
    Fantasia fantasia(_cookOrder[0].pizzaSize);
    Americana americana(_cookOrder[0].pizzaSize);
    if (_cookOrder.empty())
        return (false);
    switch (_cookOrder[0].pizzaType) {
        case plazza::Margarita:
            return (makeOrder(margarita));
            break;
        case plazza::Regina:
            return (makeOrder(regina));
            break;
        case plazza::Fantasia:
            return (makeOrder(fantasia));
            break;
        case plazza::Americana:
            return (makeOrder(americana));
            break;
        default:
            return (false);

    }
}

however when I try to compile I get this:

kitchen/kitchen.cpp: In member function ‘void Kitchen::make_pizza(std::__cxx11::string, std::__cxx11::string, Ipc)’: kitchen/kitchen.cpp:148:59: error: expected primary-expression before ‘void’ std::thread threadObj(this->_cooks[i].nextOrder, void); ^~~~ kitchen/kitchen.cpp:148:63: error: invalid use of non-static member function ‘bool cooks::nextOrder()’ std::thread threadObj(this->_cooks[i].nextOrder, void); ^ In file included from kitchen/kitchen.hpp:17, from kitchen/kitchen.cpp:5: kitchen/cooks.hpp:32:10: note: declared here bool nextOrder(); ^~~~~~~~~ In file included from /usr/include/c++/8/x86_64-redhat-linux/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from /usr/include/c++/8/ostream:38, from /usr/include/c++/8/iostream:39, from kitchen/../pizza/IPizza.hpp:23, from kitchen/../pizza/APizza.hpp:11, from kitchen/kitchen.hpp:8, from kitchen/kitchen.cpp:5: /usr/include/c++/8/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = cooks; _Args = {const cooks&}; _Tp = cooks]’: /usr/include/c++/8/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits ::construct(std::allocator_traits >::allocator_type&, _Up*, _Args&& ...) [with _Up = cooks; _Args = {const cooks&}; _Tp = cooks; std::allocator_traits >::allocator_type = std::allocator]’ /usr/include/c++/8/bits/stl_vector.h:1079:30: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = cooks; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = cooks]’ kitchen/kitchen.cpp:36:30: required from here /usr/include/c++/8/ext/new_allocator.h:136:4: error: use of deleted function ‘cooks::cooks(const cooks&)’ { ::new((void )__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from kitchen/kitchen.hpp:17, from kitchen/kitchen.cpp:5: kitchen/cooks.hpp:21:7: note: ‘cooks::cooks(const cooks&)’ is implicitly deleted because the default definition would be ill-formed: class cooks ^~~~~ kitchen/cooks.hpp:21:7: error: use of deleted function ‘std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const std::basic_ofstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]’ In file included from kitchen/../plazza.hpp:15, from kitchen/kitchen.hpp:13, from kitchen/kitchen.cpp:5: /usr/include/c++/8/fstream:778:7: note: declared here basic_ofstream(const basic_ofstream&) = delete; ^~~~~~~~~~~~~~ In file included from /usr/include/c++/8/vector:62, from kitchen/../pizza/IPizza.hpp:24, from kitchen/../pizza/APizza.hpp:11, from kitchen/kitchen.hpp:8, from kitchen/kitchen.cpp:5: /usr/include/c++/8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1, _Args&& ...) [with _T1 = cooks; _Args = {cooks}]’: /usr/include/c++/8/bits/stl_uninitialized.h:83:18:
required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*; bool _TrivialValueTypes = false]’ /usr/include/c++/8/bits/stl_uninitialized.h:134:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*]’ /usr/include/c++/8/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*; _Tp = cooks]’ /usr/include/c++/8/bits/stl_uninitialized.h:311:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = cooks*; _ForwardIterator = cooks*; _Allocator = std::allocator]’ /usr/include/c++/8/bits/vector.tcc:447:6:
required from ‘void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const cooks&}; _Tp = cooks; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename std::_Vector_base<_Tp, _Alloc>::pointer = cooks*]’ /usr/include/c++/8/bits/stl_vector.h:1085:4: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = cooks; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = cooks]’ kitchen/kitchen.cpp:36:30: required from here /usr/include/c++/8/bits/stl_construct.h:75:7: error: use of deleted function ‘cooks::cooks(const cooks&)’ { ::new(static_cast(__p)) _T1(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ make: *** [: kitchen/kitchen.o] Error 1

Is there any way to fix it?

1
Some compilation errors are due to cooks being non-copyable. Make sure you understand en.cppreference.com/w/cpp/language/rule_of_three and know whether cooks members are copyable or notwreckgar23

1 Answers

0
votes

According to this, the first argument to a std::thread constructor should meet the Callable concept. You then pass the subsequent arguments for the Callable as additional arguments to the constructor.

You're passing a member function. You need an instance of the class to call the method on, which you're not providing. This is what this error should mean to you.

error: invalid use of non-static member function ‘bool cooks::nextOrder()’

It's probably easiest to use a lambda ie.

for (int i = 0; i < _numCooks; i++) {
    _threads.emplace_back([&cook = _cooks[i]]() {
        cook.nextOrder();
     });
}