random.gauss vs. random.normalvariate

C

Carl Banks

Quotinghttp://docs.python.org/3.1/library/random.html#random.gauss:
    Gaussian distribution. mu is the mean, and sigma is the
    standard deviation. This is slightly faster than the
    normalvariate() function defined below.

So since both are offered and gauss is faster, I assume it
must have some offsetting disadvantage.  What is it?

random.gauss is not thread safe.

I'm kind of surprised the html docs don't mention this; the docstring
does.


Carl Banks
 
D

Dennis Lee Bieber

What does the term "thread safe" mean exactly. I never had to program with
"threads" before

That, part way through the logic of the function, control could be
switched to a different thread which call the same function... This
second call would change some of the internal values and may then be
preempted and control returned to the first thread, which continues the
rest of the function with different values then it had when first
preempted.

A very contrived example, untested of course, consider it
pseudo-code...

startt = None

def atimer():
global startt
startt = time.time()
time.sleep(5)
print time.time() - startt

t1 = threading.thread(atimer)
t2 = threading.thread(atimer)
t1.start()
t2.start()

Say t1 gets all the way up to the sleep call, and (since sleep is a
releasing call), t2 then starts. t2 changes the value of startt; and
sleeps... both sleep and presuming the resolution is fine enough, t1
resumes, and prints a delta time that is incorrect -- it is printing
the time difference from when t2 started to sleep, not from when t1
started to sleep.
 
D

Dennis Lee Bieber

Interesting so it seems that the compiler(c/c++)interpreter(perl,
python)/vm(java) doesn't do this?

No language can guard against independent access of a shared/global
object by multiple threads... Look at Java's "synchronized" keyword, for
example.

Though Ada did build tasking and "protected objects" into the
language syntax, rather than making them a library with constraints one
must know of to code against.
 
D

Dave Angel

John said:
Interesting so it seems that the compiler(c/c++)interpreter(perl,
python)/vm(java) doesn't do this?
It is impossible for a language, vm, or operating system to avoid
threading problems without the programmer's help, except by trivial
means (eg. preventing you from having them at all).

The power of threading is entirely tied up with the features the
environment gives to the developer, and those features come with a risk.

At one extreme is the CP/M model. You start a new program only when you
finish the previous one. So the only communication between them is a
file the first one leaves behind, that the second can look at.

Next is separate processes. If you launch a second process, by default,
they're independent, and not likely to get into trouble. But you can
build pipes or shared memory, or sockets between them, and then you have
to worry about race conditions.

Next is threads, within a single process. At this point, you can share
(global) variables between them, or you can have objects known to both
when the thread is launched. The system cannot tell which ones are
deliberate and which ones are accidental. So a language might give
extra keywords to tell the compiler that certain things should be
protected in certain ways. Or it might give a way to declare a
"per-thread global" that acts like a global to each thread, but is
actually two independent variables from the process point of view.

The only real reason threads are singled out is it's easier to collide
by mistake. But that's also what makes it efficient to "collide" on
purpose.

DaveA
 
D

Dennis Lee Bieber


Well, if I read the wikipedia link given in a later post,
http://en.wikipedia.org/wiki/Erlang...oncurrency_and_distribution_oriented_language
(emphasis mine)...

"""
Like operating system processes (and unlike green threads and operating
system threads) they have NO SHARED STATE between them.
"""

which seems to reinforce my statement, not refute it.

Hmmm, except for the facet that the Amiga exec directly passed
memory addresses (no memory protection in the OS, the process sending
was not supposed to touch the memory of the sent message until receiving
the message reply), the Erlang process communication maps very closely
to the Amiga message port system.


(The Amiga was heavily dependent upon message ports: application I/O
request becomes a message to the port of the file system, file system
would map the "file pointer" to an internal table to locate the device,
send a message to the port of the device handler -- device handler would
execute the I/O request, reply to file system, file system would then
reply to application... Having to copy data buffers between processes
would have slowed the system down drastically [8MHz processor, in those
days], so the link-list of messages on a port were typically short,
having a pointer to the actual memory to use)
 
P

Paul Rubin

Dennis Lee Bieber said:
http://en.wikipedia.org/wiki/Erlang_(programming_language)
"""
Like operating system processes (and unlike green threads and operating
system threads) they have NO SHARED STATE between them.
"""
which seems to reinforce my statement, not refute it.

Id say Erlang guards against independent access of shared/global
objects by not allowing them.
(The Amiga was heavily dependent upon message ports... Having to
copy data buffers between processes would have slowed the system
down drastically [8MHz processor, in those days],

It's perfectly ok for Erlang implementation to just pass pointers
around, when the processes are on the same machine. Erlang prohibits
data mutation so there isn't an issue of a process modifying some
structure while another process is using it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top