1

I am quite new to Multi-Thread systems and have a question regarding a suitable data structure for objects.

I have an object image which has several attributes, two of them are a byte array data storing the image data and an int variable status storing the status.

I have some form of a conveyor belt architecture, meaning having a camera-thread storing the data into the image attribute, followed by a converter-thread, followed by a processing-thread. So one thread has to work on the output of the predecessor thread.

To avoid synchronizing and increasing the efficiency, my idea is to have a LinkedList<Image> storing image objects. The webcam-thread creates the first image object, assigns the status=1 (meaning data writing in progress) and assigns status=2 if it finishes and heads over to the next image. The following converter-thread only reads the status attribute, if it is 1 it sleeps or awaits (can be signaled by the webcam-thread), if it is 2, it assigns status 3 and starts its work. When it is finished it sets the status=4 and continues with the next image object. The next Thread again reads the status, and will sleep if the status < 4 and so on.

Finally my question is, whether there will be problems caused by the possible simultaneous access by two different threads, while assuring that only one is a write operation and the other (read) access is even conducted on a different attribute/variable (of the same object).

assylias
  • 321,522
  • 82
  • 660
  • 783
Blahnik
  • 59
  • 3

1 Answers1

1

Even if a single thread modifies the same shared data and others perform only read, races and data corruption possible.

Consider the following:

                                   Thread 1                       | Thread 2
                                   ...                            | ...  
--> not atomic!                    start writing shared resource  | ...
--> data state is undefined here   ...                            | read shared resource
                                   end writing shared resource    | ...

If you can ensure that write access to the same shared data occurs without any additional access (read or write), you're good without protection.

Otherwise, you need to protect your shared resource with some locking mechanism, such as semaphore or mutex. From the description it sounds like a typical consumer-producer problem, take a look at Synchronizing producer, consumer and a producer queue and similar questions.

Community
  • 1
  • 1
SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
  • Thank you for your answer. I forgot to mention that I would use an AtomicInteger as the status variable, so increasing the status using the provided atomic getAndIncrement() function would avoid, imho that there will be any thread conflicts on the data array as the follow-up thread will either see the atomically changed status 1,3,5.. -> writing in progress, wait or 2,4,6..->writing finished, work on image data. So still the possibility of data corruption? – Blahnik Nov 29 '12 at 16:48
  • Sounds that it should be OK, but it's difficult to say without seeing actual code. – SomeWittyUsername Nov 29 '12 at 16:56