1 Replies - 3356 Views - Last Post: 07 December 2012 - 09:21 PM Rate Topic: -----

#1 Python_4_President   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

How do I write thread-unsafe code?

Posted 26 November 2012 - 12:17 PM

I have a GUI that crashes occasionally, but the crashing stops when I disable the counter.

The old counter code is demonstrated in baaad where a variable in a function that is executed by more than one thread is incremented by each thread.

I've since switched to the myCounter way of doing things and now the program runs much longer (but still crashes after a while), but I wanted to try to create a problem to better understand the issue, so I experimented with other ways of doing it. Unfortunately, everything works just fine.

Please help me write thread-unsafe code.

EDIT: Friendlier printing. I'm hoping for either an individual thread, or the main thread to crash.
import itertools, threading, time

class myCounter(object):
    def __init__(self, startval=0, step=1):
        self.counter = itertools.count(startval, step)
        self.at = startval
    def next(self):
        self.at = self.counter.next()
	return self.at
    def get(self):
        return self.at
    def reset(self):
	self.counter = itertools.count(0)
	self.at = 0

class myOtherCounter(object):
    def __init__(self, startval=0, step=1):
        self.counter = startval
	self.step = step
    def next(self):
        self.counter += self.step
	return self.counter
    def get(self):
        return self.counter
    def reset(self):
	self.counter = 0

def torture(name, counter):
	while counter.next() < 50000:
	print "{0}:Done -> {1}".format(name, counter.get())

global x
x = 0

def baaad(name):
	global x
	while x < 50000:
		x += 1
	print "{0}:Done -> {1}".format(name, x)

if __name__ == "__main__":
	b = myCounter()
	c = myOtherCounter()

	[threading.Thread(target=torture, args=["myCounterThread{0}".format(i), b]).start() for i in range(1, 4)]
	[threading.Thread(target=torture, args=["myOtherCounterThread{0}".format(i), c]).start() for i in range(1, 4)]  
	[threading.Thread(target=baaad, args=["myBADCounterThread{0}".format(i)]).start() for i in range(1, 4)]

This post has been edited by Python_4_President: 26 November 2012 - 12:53 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How do I write thread-unsafe code?

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • Posts: 2,236
  • Joined: 23-December 08

Re: How do I write thread-unsafe code?

Posted 07 December 2012 - 09:21 PM

A lot of the indentation in this code is really off. Lines that need indented: 9,13,14,19,22,26.

A better question might be "What makes it thread-unsafe"? The simplest example that comes to my mind is to imagine an atm. A husband and a wife have a shared bank account. This account has $100 in it. At the exact same moment, both husband and wife log into different atms and try to withdraw all the money. If both atms request the money be withdrawn at the same time, we could have a serious problem. A simple solution is to implement a lock on the account, which only allows 1 person to be logged in at a given time.

Use your head and it's pretty easy to write bad code lol.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1