Redesign of a class that used alot of pointers

J

Jason

Hi,

I have this old class that was used for logging error messages and I thought
it would be a good exercise (for me) to convert it to .Net specifically c#.

This class uses two typed pointer lists to process log messages. One is the
input queue for incoming messages and the other is the processing queue for
processing the messages. The thread that does the processing will process
all the objects on the procesing queue and then swap the input and
processing queues and then process those messages. This allows for the
incoming messages to be added to a queue with out the potential of being
blocked by the processing thread.

The last part about swapping the queues (which is obviously done via
pointers) is where I'm not sure what to do in c#.

Anyone have any ideas?

Thanks
 
B

Bob Grommes

It sounds like you are converting a C++ class. You can declare unsafe code
blocks and use pointers in C#, but the general idea is to avoid pointers
except when they really do help a provable performance issue. In my view,
pointers are seldom worth the trouble and risk in a line of business
application.

In C#, a reference to an object usually takes the place of a pointer. I
would imagine that you'd have two instances of a queue class, and refer to
one or the other, e.g.:

MyQueue inputq = new MyQueue();
MyQueue processingq = new MyQueue();
// sometime later
DoWork(processingq);
DoWork(inputq);
// or maybe even
processingq.DoWork();
inputq.DoWork();

--Bob
 
D

Daniel O'Connell [C# MVP]

The last part about swapping the queues (which is obviously done via
pointers) is where I'm not sure what to do in c#.

You simply swap the references. .NET references are pretty close to what
pointers will provide you with.
 
J

Jason

Daniel O'Connell said:
You simply swap the references. .NET references are pretty close to what
pointers will provide you with.

wouldn't that be a very expensive option ? If I understand it correctly
swapping the references would copy every object in the queue rather than
just the addresses. If I had alot of messages in the queues wouldn't that be
too time consuming cpu wise?

Thanks
 
D

Daniel O'Connell [C# MVP]

Jason said:
wouldn't that be a very expensive option ? If I understand it correctly
swapping the references would copy every object in the queue rather than
just the addresses. If I had alot of messages in the queues wouldn't that
be too time consuming cpu wise?

You understand it wrong. The reference is nothing but a 4byte(or possiby 8
on 64 bit machines, I"m not sure) value that is used to locate the object's
actual memory pointer. So swapping the references would simply swap that 4
byte value between the two.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top