Regarding locks and global variables 
Author Message
 Regarding locks and global variables

Hi,

I have a global linked list to which I add nodes through func_a() and
consume nodes from func_b().

func_a() creates a new node and add it to the list, no existing node
in the linked list is modified. The linked list is accessed by
func_a() only to add a node.

func_b() simply uses the nodes in the linked list.

The linked list is a singly-linked list. func_a() will add nodes to
the end of the linked list and func_b() will use nodes from the start
of the linked list.

Since it is a global linked list should I be using locks ?? I am
relatively new to the concept of locks. The code would be executed on
a SMP(multi-processor) and non-SMP machine.

Would help if I could get some inputs on the above.  Any site which
talks of locks and multi-processor machines would also help.

Would I have a problem only on multiprocesor machines or would the
problem also occur if I have multiple processes calling func_a() and
func_b() randomly.

Thanks and Regards,
Mala
--



Mon, 24 Oct 2005 11:15:26 GMT  
 Regarding locks and global variables

Quote:

> Since it is a global linked list should I be using locks ??

Only if the data is shared among multiple threads of
execution.  Just using a multiprocessor system doesn't
automatically multithread your code.
--



Tue, 25 Oct 2005 07:03:27 GMT  
 Regarding locks and global variables

Quote:

> func_a() creates a new node and add it to the list, no existing node
> in the linked list is modified. The linked list is accessed by
> func_a() only to add a node.

These two statements contradict each other.  To add a node to a linked
list, you *must* be modifying at least one other node.  To be precice,
must modify the "next" pointer of the element in front of it to point
to the newly added element.

Quote:
> Since it is a global linked list should I be using locks ??

If you foresee the two functions to be running in separate, concurrent
threads: yes.  If your program doesn't use multi-threading, there
should be no need to worry about this.

Quote:
> I am relatively new to the concept of locks. The code would be
> executed on a SMP(multi-processor) and non-SMP machine.

The type of machine is largely irrelevant.  The way you call those
functions is what you should check.

Quote:
> Would I have a problem only on multiprocesor machines or would the
> problem also occur if I have multiple processes calling func_a() and
> func_b() randomly.

Not as long as those multiple processes don't share a single linked
list in some way.  Nothing in your post says they do.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 25 Oct 2005 07:04:19 GMT  
 Regarding locks and global variables
On 08 May 2003 23:04:19 GMT, Hans-Bernhard Broeker

Quote:


> > func_a() creates a new node and add it to the list, no existing node
> > in the linked list is modified. The linked list is accessed by
> > func_a() only to add a node.
> These two statements contradict each other.  To add a node to a linked
> list, you *must* be modifying at least one other node.  To be precice,
> must modify the "next" pointer of the element in front of it to point
> to the newly added element.

No.  It is a singly linked list and an insert at the front.  No node is
modified.  The list is, but none of the nodes.

That may or may not be true for the original post, but it is a way to
add a node without modifying a node.

John
--



Tue, 25 Oct 2005 14:06:08 GMT  
 Regarding locks and global variables

Quote:



> > > func_a() creates a new node and add it to the list, no
> > > existing node in the linked list is modified. The linked
> > > list is accessed by func_a() only to add a node.

> > These two statements contradict each other.  To add a node to
> > a linked list, you *must* be modifying at least one other node.
> > To be precice, must modify the "next" pointer of the element in
> > front of it to point to the newly added element.

> No.  It is a singly linked list and an insert at the front.  No
> node is modified.  The list is, but none of the nodes.

> That may or may not be true for the original post, but it is a
> way to add a node without modifying a node.

IIRC the OP specifically specified the addition was to the tail of
the list.  In which case Hans is correct.  However if there are
list generation followed by list use phases, the easiest method is
often insertion at the head followed by a single list reversal,
which can save an awful lot of list walking.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Thu, 27 Oct 2005 05:54:10 GMT  
 Regarding locks and global variables

Quote:

> No.  It is a singly linked list and an insert at the front.  

The OP explicitly said that new insertions were supposed to be done at
the *end* of the list, so this doesn't count.

Either way, you're still going to modify one object that belongs to
the list, even though it may not technically count as a "node": to
insert at the front, you overwrite the globally visible variable that
serves as the anchor.  Or at the very least its 'next' pointer, if you
use a dummy element instead of a pointer as the anchor.

--

Even if all the snow were burnt, ashes would remain.
--



Thu, 27 Oct 2005 05:54:13 GMT  
 Regarding locks and global variables

Quote:

> However if there are list generation followed by list use phases,
> the easiest method is often insertion at the head followed by a
> single list reversal, which can save an awful lot of list walking.

Or you can just maintain an additional pointer to the tail of the list
while doing lots of tail insertions in a row.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 30 Oct 2005 05:10:48 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Regarding locks and global variables

2. quesiton regarding initializing global variables

3. Global variable or not global variable

4. Uninitialized global data (was Re: Global variables)

5. static local variables vs global variables

6. Variables & Global Variables

7. global variables and external variables?

8. reading global LOCK toggles

9. num lock, cap lock, and scroll lock

10. Caps Lock / Num Lock / Scroll Lock

11. Newbie C question regarding static & const variables

12. question regarding static variables

 

 
Powered by phpBB® Forum Software