PDA

View Full Version : Queues and MultiThreading


MattNolan
07-28-2010, 07:13 PM
I have a program that currently hashes files, and then compares hashes linearly, I'm looking to change it so that it will hash a file, stick the hash and information about that file into a queue, and then have another thread compare that hash to a separate list and perform some simple operations.

basically it looks like this

Thread 1 Thread 2
Read in file Dequeue
Hash Compare Hashes
Enqueue Do Stuff


Somehow though, my queue is getting null values into it. My first hypothesis is that it is enqueue/dequeue at exactly the same time and causing the insert to happen before the deletion is fully in effect resulting in something like:

queue[1] = item
queue[2] = item
queue[3] = item
queue[4] = null
queue[5] = item

Right now I'm getting the error with null values getting into the queue, and another exception with message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. on a call to an outside dll that had never given me problems before.

Spookster
07-28-2010, 07:18 PM
How are you defining the queue and where?

MattNolan
07-28-2010, 07:33 PM
The queue is defined globally

Please don't hurt me

Spookster
07-29-2010, 05:35 AM
When you are working with threads that need to read/write to a common location in memory whether it be a globally defined variable or use of shared memory or whatever you need to synchronize access to that memory location. Are you using some kind of synchronization method such as semaphores or mutex?

And I cannot guarantee your safety. The Spookster has spoken.

iamscottj
07-30-2010, 06:54 AM
I think you must have a locking mechanism... Please mention in which code you are trying to do this... I Java or .NET