- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
what is the right way to use std::shared_ptr with tbb::task? Is it possible or does it make sence? I need to access tbb::task through smart pointer, because the project I'm working on utilizes smart pointers and I don't want to rewrite everything. Here is the modified Fibonacci-example. I get "terminate called after throwing an instance of 'std::bad_weak_ptr'
what(): std::bad_weak_ptr" during the execution.
I'm grateful for every suggestion.
#include "tbb/tbb.h"
#include <iostream>
#include <memory>
using namespace tbb;
long SerialFib(long n) {
if (n < 2)
return n;
else
return SerialFib(n - 1) + SerialFib(n - 2);
}
class FibTask: public task, public std::enable_shared_from_this<FibTask> {
public:
const long n;
long* const sum;
FibTask(long n_, long* sum_) :
n(n_), sum(sum_) {
}
task* execute() {
if (n < 10) {
*sum = SerialFib(n);
} else {
long x, y;
FibTask* a1 = new (allocate_child()) FibTask(n - 1, &x);
FibTask* b1 = new (allocate_child()) FibTask(n - 2, &y);
std::shared_ptr<FibTask> a = a1->shared_from_this();
std::shared_ptr<FibTask> b = b1->shared_from_this();
set_ref_count(3);
FibTask& aa = *a.get();
FibTask& bb = *b.get();
spawn(bb);
spawn_and_wait_for_all(aa);
*sum = x + y;
}
return NULL;
}
};
long ParallelFib(long n) {
long sum;
FibTask* a1 = new(task::allocate_root()) FibTask(n, &sum);
std::shared_ptr<FibTask> aa = a1->shared_from_this();
FibTask& a = *aa.get();
task::spawn_root_and_wait(a);
return sum;
}
int main()
{
std::cout << ParallelFib(20);
}
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page