3
class worker { std::atomic_bool done; std::string segment_name; bi::named_mutex mutex; std::string file_name; data_segment_state state; unsigned long data_size; std::unique_ptr<unsigned char> data; protected: void operator()() { while(!done) { } } public: worker(const std::string& _segment_name, bi::managed_shared_memory& segment) : done{false}, segment_name{_segment_name}, mutex{bi::open_or_create, segment_name.c_str() }, file_name {}, state {data_segment_state::not_received }, data_size{0}, data {segment.construct<unsigned char>(segment_name.c_str())[chunk_size](0) } } worker(worker&& rhs) : done {rhs.done.load()} , mutex(bi::open_or_create, rhs.segment_name.c_str()), segment_name{rhs.segment_name}, file_name {rhs.file_name}, state {rhs.state }, data_size{rhs.data_size}, data {std::move(rhs.data)} { } }; 

...

 std::string worker_name; std::thread t{worker{worker_name, segment}}; In instantiation of 'struct std::_Bind_simple<worker()>': thread:137:47: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = worker; _Args = {}]' error: no type named 'type' in 'class std::result_of<worker()>' typedef typename result_of<_Callable(_Args...)>::type result_type; 

what is a valid thread initialization with functional object? full text of error: error: no type named 'type' in 'class
std::result_of' typedef typename result_of<_Callable(_Args...)>::type result_type; ^

4
  • 1
    create a worker then pass it to the thread constructor. Commented Oct 1, 2015 at 12:18
  • "does also produces the same error with a using default ctor" No it doesn't. I guess you are not showing us the code that you are compiling, and you're actually getting some other error that we can't possibly guess. Commented Oct 1, 2015 at 12:30
  • let me update the question with a code as full as I can, and output of GCC from console Commented Oct 1, 2015 at 12:36
  • You should really have done that before asking a question. See stackoverflow.com/help/mcve Commented Oct 1, 2015 at 12:37

2 Answers 2

4

The issue was in the my inattention: operator() was in protected section . moving to public solves the problem

Sign up to request clarification or add additional context in comments.

Comments

3

Just pass the constructed functor:

std::thread t(worker{str, 10}); 

Live demo

The constructor of std::thread you are referring to takes a constructed function object and arguments that will be passed when calling that function object. So in your case the arguments would be passed to operator().

1 Comment

Using std::ref is pointless there, str will bind directly to the constructor parameter, you don't need to protect it from decaying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.