You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using the 'usage' code from your previous blog post doesn't compile:
#include "ThreadPool.h"
#include <stdio.h>
#include <iostream>
int main() {
// create a thread pool of 4 worker threads
ThreadPool pool(4);
// queue a bunch of "work items"
for(int i = 0;i<8;++i)
{
pool.enqueue([i]
{
std::cout << "hello " << i << std::endl;
std::cout << "world " << i << std::endl;
});
}
}
errors out with:
main.cpp: In function 'int main()':
main.cpp:17:7: error: no matching function for call to 'ThreadPool::enqueue(main()::<lambda()>)'
main.cpp:17:7: note: candidate is:
In file included from main.cpp:1:0:
ThreadPool.h:117:15: note: template<class T, class F> Result<T> ThreadPool::enqueue(F)
ThreadPool.h:117:15: note: template argument deduction/substitution failed:
main.cpp:17:7: note: couldn't deduce template parameter 'T'
scons: *** [build/main.o] Error 1
Yes I'm currently thinking about adding an example to the repository. This version here also allows return values and waiting on the result (somewhat similar to std::future).
ooh cool - I was thinking that was what the Result class was for.
Out of curiousity, why not just use the std::future instead of making your own class? (forgive me if this is a silly question, I'm still learning the threading/c++ ropes)
Not silly at all. One of the reasons for writing this was to understand the internals of a thread pool/futures better. So launching std::packaged_tasks in separate threads and using the future you get from that is probably better. Using std::promise would also be a way to to "return" a value from a thread without requiring this custom Result construct. I'm not really done with understanding all the new toys in the standard either ;).
I moved the old version to a "legacy" subdirectory and rewrote it so it uses std::future instead of the custom Result type. That simplifies the implementation and future is probably higher quality than my homebrew Result type anyway.
Activity
jarrettchisholm commentedon Oct 15, 2012
Needed to specify what
Twas. After setting it to<void>it works....the code that works:progschj commentedon Oct 15, 2012
Yes I'm currently thinking about adding an example to the repository. This version here also allows return values and waiting on the result (somewhat similar to std::future).
jarrettchisholm commentedon Oct 16, 2012
ooh cool - I was thinking that was what the Result class was for.
Out of curiousity, why not just use the std::future instead of making your own class? (forgive me if this is a silly question, I'm still learning the threading/c++ ropes)
progschj commentedon Oct 16, 2012
Not silly at all. One of the reasons for writing this was to understand the internals of a thread pool/futures better. So launching std::packaged_tasks in separate threads and using the future you get from that is probably better. Using std::promise would also be a way to to "return" a value from a thread without requiring this custom Result construct. I'm not really done with understanding all the new toys in the standard either ;).
progschj commentedon Oct 16, 2012
I moved the old version to a "legacy" subdirectory and rewrote it so it uses std::future instead of the custom Result type. That simplifies the implementation and future is probably higher quality than my homebrew Result type anyway.
progschj commentedon Oct 16, 2012
There are also examples now for both implementations so I'll close this issue for now.
jarrettchisholm commentedon Oct 16, 2012
cool cool :) thanks for helping me out :)