c - How to use multiple pthreads to evaluate expression from a program -
i want utilize pthread solve look breaking downwards threads.
my questions is:- if look (a+b)+(c+d)+(e+f) evaluated using pthreads such that:-
create 3 threads first look (a+b) evaluated 1st thread, (c+d) evaluated sec thread , (e+f) evaluated 3rd thread. value of above variables '1', so, final reply of look evaluation should come '6'. the 3rd thread should executed @ lastly print final output '6'.then how this??
when parse expression, you'll abstract syntax tree following:
+ / \ + + / \ / \ + + e f / \ |\ b c d
you can partition problem threads @ nodes of syntax tree. when evaluating node, can give 2 different subproblems (left , right subtree) 2 different threads. each of subthreads can repeat pattern until have parallelized tree sufficiently. in code, might like:
int evaluate_subtree_threaded(node_t* parent_node) { int left_result, right_result; pthread_t thread; pthread_create(&thread, null, evaluate_subtree_threaded, parent_node->left); right_result = evaluate_subtree(parent_node->right); pthread_join(thread, &left_result); homecoming left_result + right_result; }
note how code snippet blatantly assumes actual evaluation logic in evaluate_subtree
, ignores types.
of course, aware thread creation much more expensive evaluating expression.
c linux pthreads
No comments:
Post a Comment