Friday 15 June 2012

.net - Dequeue not the same as what is enqueued -



.net - Dequeue not the same as what is enqueued -

hello there stackoverflow! i'll cutting chase: have server / client programme using tcp , having problem queue utilize set read packets in. works 1 time packet read it's enqueued queue, , if programme isn't busy handling packet handle , dequeue queue.

here code enqueuing data:

if readindex = readbuffer.length synclock readqueue readqueue.enqueue(readbuffer) end synclock file.writeallbytes(application.startuppath & "\test\" & & ".bin", readbuffer) += 1 if not processing(2) processing(2) = true if not threadpool.queueuserworkitem(addressof handlereadqueue) handlereadqueue() end if end if

note writing packets each own file (this debugging purposes). utilize threadpool handle readqueue on different thread.

and here method handling queue:

private sub handlereadqueue() dim info byte() synclock readqueue info = readqueue.dequeue() end synclock file.writeallbytes(application.startuppath & "\testreadqueue\" & y & ".bin", data) y += 1 if _parent isnot nil handleclientreadpacket(_parent, _pipename, data) else handleclientreadpacket(me, nothing, data) end if end sub

the files in testreadqueue different ones in test folder. have no thought why. , know fact info received info enqueued identical 1 client sends.

here can see it's different: http://gyazo.com/feb4a192e47f262a9f23812ae8bc6cf6

this problem not occure when utilize threadpool, when dont utilize synclock isn't necessary.. can tell me what's wrong?

the readbuffer indeed modified right after enqueued, thought enqueuing value , not reference?

the reference enqueued by value.

arrays reference types. ends in queue reference byte array. illustration (c#)

var queue = new queue<byte[]>(); byte[] buffer = { 1 }; queue.enqueue(buffer); buffer[0] = 100; var dequeued = queue.dequeue(); console.writeline(dequeued[0]); // 100

if want reference queued independent of byte array you're going overwriting, need clone it. in c# illustration use:

queue.enqueue((byte[]) buffer.clone());

use whichever casting view appropriate in vb - of import part array cloned, , hence independent of original array you're still modifying.

.net vb.net multithreading queue synclock

No comments:

Post a Comment