Friday, 15 March 2013

c - Threaded Binary Tree using semaphores for concurrency -



c - Threaded Binary Tree using semaphores for concurrency -

i'm pretty unfamiliar multithreading , synchronization , have add together semaphore code given pseudocode allow multiple threads access threaded binary tree max concurrency.

the pesudocode gives node struct contains:

int info node *leftchild, *rightchild, *prev, *next;

and function insert(node *root, int data) searches find parent in current tree, , inserts new node (given isn't in tree) , changing prev , next pointers.

i'm not sure how best implement semaphores. initial idea(s) are:

lock/unlock each node it's beingness searched. prevent other threads inserting while search. should parent node locked well?

then insertion:

lock parent node , current node able prevent thread inserting @ same place @ same time.

is there improve way go this? (assuming it's right in first place)

in case should utilize read-write lock. locking on individual nodes or parts of tree become exceptionally hard if later decide switch tree implementation , more work needed.

int32_t tree_insert(tree *tree, int32_t data) { pthread_rwlock_wrlock(&tree->rwlock); int32_t ret = insert(&tree->root_node, data); pthread_rwlock_unlock(&tree->rwlock); homecoming ret; }

c multithreading thread-safety binary-tree

No comments:

Post a Comment