#include #include #include "include/tbb/task_scheduler_init.h" #include "include/tbb/task.h" #include "include/tbb/mutex.h" using namespace tbb; mutex Mutex; class SimpleTask: public tbb::task { int const n; public: SimpleTask(int n_) : n(n_){} task* execute() { int i = 0; while(i < 10) { mutex::scoped_lock lock; { while(!lock.try_acquire(Mutex)) { sleep(0.2); printf("%d está esperando o lock...\n\n", n); } printf("%d entrou na RC pela %d vez\n", n, i); printf("%d saiu da RC\n", n); lock.release(); i++; sleep(1); } } return NULL; } }; int main() { task_scheduler_init init; tbb::task_list list; tbb::task *root = new(tbb::task::allocate_root()) tbb::empty_task(); int numThreads = 2; for (int i = 0; i < numThreads; i++) { tbb::task *simpleTask = new(root->allocate_child()) SimpleTask(i); list.push_back(*simpleTask); } root->set_ref_count(numThreads + 1); root->spawn_and_wait_for_all(list); root->destroy(*root); return 0; }