Click here to Skip to main content
15,901,035 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to add event handler to control created at run time. Pin
TrooperIronMan8-Jan-07 6:32
TrooperIronMan8-Jan-07 6:32 
GeneralRe: How to add event handler to control created at run time. Pin
TrooperIronMan8-Jan-07 13:33
TrooperIronMan8-Jan-07 13:33 
QuestionSystem Hook Pin
lost in transition 8-Jan-07 6:03
lost in transition 8-Jan-07 6:03 
AnswerRe: System Hook Pin
Stefan Troschuetz8-Jan-07 6:19
Stefan Troschuetz8-Jan-07 6:19 
Questioninterlocked linked list [modified] Pin
mikewinny8-Jan-07 5:11
mikewinny8-Jan-07 5:11 
AnswerRe: interlocked linked list Pin
Luc Pattyn8-Jan-07 7:31
sitebuilderLuc Pattyn8-Jan-07 7:31 
GeneralRe: interlocked linked list Pin
mikewinny8-Jan-07 7:48
mikewinny8-Jan-07 7:48 
GeneralRe: interlocked linked list [modified] Pin
Luc Pattyn8-Jan-07 8:47
sitebuilderLuc Pattyn8-Jan-07 8:47 
Well, yes and no:

Yes, if you have a single-linked list and a _last reference, then you can append
a node to it correctly with your code, but that's about it. If there is no _head,
and no backward link (prev), then you can not reach any other node.

A single-linked list with a head only typically uses the following logic
(I use a dummy head node to simplify code: when head.next is the first node (or null),
then head itself never changes and never is null):

void addNode(Node newNode) {    // unsafe version
    newNode.next=head.next;
    head.next=newNode;
}
Node removeFirstNode() {    // unsafe version
    Node firstNode=head.next;
    head.next=firstNode.next;   // <<--- modified
    return firstNode;
}


the safe versions would be:

void addNode(Node newNode) {    // safe version
    do {
        Node firstNode=head.next;
        newNode.next=firstNode;
    } while (firstNode!=Interlocked.ExchangeCompare(head.next,newNode,firstNode));
}

Node removeFirstNode() {    // safe version
    do {
    Node firstNode=head.next;
    } while (firstNode!=Interlocked.ExchangeCompare(head.next, firstNode.next, firstNode));
    return firstNode;
}


But now there are a lot of functional limitations:
there is no access to any other node
1) we do not maintain a tail
2) we do not maintain a backward link
so we have what is known as a stack, certainly not a queue.


If we want more than stack functionality, we'd better have head+tail+nextlinks+prevlinks;
but we must perform multiple stores atomically, and you cannot achieve that
with a single Interlocked method.

You can of course synthesize a lock yourself, something like:

while (0!=Interlocked.ExchangeCompare(lockVar, 1, 0)) Thread.Sleep(1);
// do whatever you want now, even multiple stores
lockVar=0;


but that is not what you intended, is it ?

Smile | :)









-- modified at 15:10 Monday 8th January, 2007

Luc Pattyn
//
GeneralRe: interlocked linked list Pin
mikewinny8-Jan-07 9:01
mikewinny8-Jan-07 9:01 
GeneralRe: interlocked linked list Pin
Luc Pattyn8-Jan-07 9:19
sitebuilderLuc Pattyn8-Jan-07 9:19 
GeneralRe: interlocked linked list Pin
mikewinny8-Jan-07 9:23
mikewinny8-Jan-07 9:23 
QuestionTab Control Pin
Gal Edvi8-Jan-07 4:36
Gal Edvi8-Jan-07 4:36 
AnswerRe: Tab Control Pin
Eric Dahlvang8-Jan-07 4:51
Eric Dahlvang8-Jan-07 4:51 
GeneralRe: Tab Control Pin
Gal Edvi8-Jan-07 4:59
Gal Edvi8-Jan-07 4:59 
GeneralRe: Tab Control Pin
Eric Dahlvang8-Jan-07 5:11
Eric Dahlvang8-Jan-07 5:11 
GeneralRe: Tab Control Pin
Gal Edvi8-Jan-07 19:39
Gal Edvi8-Jan-07 19:39 
GeneralRe: Tab Control Pin
Eric Dahlvang9-Jan-07 3:22
Eric Dahlvang9-Jan-07 3:22 
QuestionGenerating a new button, from a button click? Pin
JayBoychuk8-Jan-07 3:58
JayBoychuk8-Jan-07 3:58 
AnswerRe: Generating a new button, from a button click? Pin
Mircea Puiu8-Jan-07 4:01
Mircea Puiu8-Jan-07 4:01 
GeneralRe: Generating a new button, from a button click? Pin
JayBoychuk8-Jan-07 5:50
JayBoychuk8-Jan-07 5:50 
GeneralRe: Generating a new button, from a button click? Pin
Mircea Puiu8-Jan-07 20:48
Mircea Puiu8-Jan-07 20:48 
GeneralA little bit more help Pin
Mircea Puiu8-Jan-07 21:38
Mircea Puiu8-Jan-07 21:38 
AnswerRe: Generating a new button, from a button click? Pin
Eric Dahlvang8-Jan-07 5:32
Eric Dahlvang8-Jan-07 5:32 
QuestionmyClass events Pin
Seishin#8-Jan-07 3:53
Seishin#8-Jan-07 3:53 
AnswerRe: myClass events Pin
Mircea Puiu8-Jan-07 3:55
Mircea Puiu8-Jan-07 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.