0

I got some problem with two threads which do seem to deadlock

The idea is this:

p1 = threading.Thread(targest  =myClass.setData, args = mylist)
p1.start()
p2 = threading.Thread(target = myClass.takeData, args = mylist)
p2.start()

mylist is a list.

Everything works absolutely fine when either the list is almost empty or when only either p1 or p2 is running. If both run, they seem to deadlock. I have tried to lock them - to no avail.

setData has an infinite while-loop which resets data in mylist, whereas takedata has an infinite whileloop which reads data from mylist.

Is it possible to do what I try to do?

3
  • 1
    Instead of describing your code in fairly vague terms, why not show us an actual runnable example that demonstrates the problem? Commented Feb 12, 2013 at 12:30
  • 2
    Yeah it's possible. This is a common problem known as consumer-producer: en.wikipedia.org/wiki/Producer-consumer_problem Commented Feb 12, 2013 at 12:32
  • use the module queue. myClass.setData == queue.Queue.put myClass.takeData == queue.Queue.get Commented Feb 12, 2013 at 13:21

1 Answer 1

1

As Adrián López says is a Producer-consumer problem. You have to use semaphores to lock the global data used by myClass.setData and myClass.takeData. Here you have a functional example to get ideas.

http://smherwig.blogspot.com.es/2012/09/producer-consumer-model-with-python.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.