Singleton Classes & thread safety

I’m a bit new to Ruby, so please excuse me if this is a simplistic
question.

I am trying to create a genuine singleton class. My requirements are as
follows:

  1. The class is used by a terminal program that will be run on one
    machine by multiple people with ssh connections to that machine.
  2. The class should never be instantiated more than once on the
    machine, regardless of how many people are running the program that uses
    it.

I have tried to create a singleton class using the mixin outlined in the
documentation (i.e, include singleton…xyz.instance). But when I
invoke the application concurrently (in two separate terminal windows),
the singleton class gets instantiated by both programs. What I want is
for the first program to instantiate it, and the second (concurrently
running) program to use the version instantiated by the first (which is
to say that I really do want it to be a singleton!).

Since Ruby is interpreted, I am hoping that the runtime allows it to
handle things this way, although since it doesn’t use namespaces like
Java it’s not clear to me exactly how this would be accomplished.

I am using Ruby 1.67 on RedHat Linux for Intel 7.3. My copy of Ruby is
compiled from the source using the default configuration and no compiler
options.

I’ve searched the archives and the web for info on this, and all I come
up with is the page from the design reference for the Singleton mixin.

···

David King Landrith (v) 617-227-4469x212

One useless man is a disgrace, two are called a law firm,
and three or more become a congress – John Adams

public key available upon request

  1. The class is used by a terminal program that will be run on one
    machine by multiple people with ssh connections to that machine.
  2. The class should never be instantiated more than once on the
    machine, regardless of how many people are running the program that
    uses it.

If this is more than one Ruby program, you’ll have to do inter-process
communications of some sort. Doesn’t this make the job of having a
singleton easy? (i.e. only one program on a machine can be accepting
connections on a given port).

So I’d say that each client program should:
* try to connect as a client to the port
* if can’t connect, then fork server process on that port
* and then start client

I have tried to create a singleton class using the mixin outlined
in the documentation (i.e, include singleton…xyz.instance). But
when I invoke the application concurrently (in two separate
terminal windows), the singleton class gets instantiated by both
programs.

The Singleton support (as all class/object based programming in a
non-distributed Ruby system) only deals with a single Ruby process.

···

On Thursday 25 July 2002 02:06 pm, David King Landrith wrote:


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

  1. The class is used by a terminal program that will be run on one
    machine by multiple people with ssh connections to that machine.
  2. The class should never be instantiated more than once on the
    machine, regardless of how many people are running the program that
    uses it.

If this is more than one Ruby program, you’ll have to do inter-process
communications of some sort. Doesn’t this make the job of having a
singleton easy? (i.e. only one program on a machine can be accepting
connections on a given port).

This is the same program being run twice concurrently, but from what you
say it sounds like this may be equivalent to two separate programs. I’d
like to protect the resources that the program uses, and I was hoping
that using a singleton class to access the resources would ensure this
(rather than writing and debugging my own mutex stuff).

The Singleton support (as all class/object based programming in a
non-distributed Ruby system) only deals with a single Ruby process.

That’s what I was afraid of.

···

On Thursday, July 25, 2002, at 05:26 PM, Ned Konz wrote:

On Thursday 25 July 2002 02:06 pm, David King Landrith wrote:


David King Landrith (v) 617-227-4469x212

One useless man is a disgrace, two are called a law firm,
and three or more become a congress – John Adams

public key available upon request

This is the same program being run twice concurrently, but from
what you say it sounds like this may be equivalent to two separate
programs.

From the OS point of view (and for that matter from the Ruby
interpreter’s point of view) they are separate programs. That they
share the same source code doesn’t make them the same program.

I’d like to protect the resources that the program uses,
and I was hoping that using a singleton class to access the
resources would ensure this (rather than writing and debugging my
own mutex stuff).

I wouldn’t be much surprised if someone had already made a file-based
mutex in Ruby.

If you can’t find one, it’s easy to port Perl to Ruby.

But actually, under Linux you might be able to get away with running
‘ps’ or examining /proc/[0-9]*/cmdline to see if you’re already
running.

for instance, here’s a program that I named “myname” and made
executable. Try running it twice from different shell prompts.

#!/usr/bin/ruby

IO.popen(“ps -C myname”) { |file|
file.each { |line|
line.chomp!
if line.match(/^\s*(\d+).*\s+(\S+)$/)
if $2 == “myname”
if $1 == Process.pid.to_s
puts “found my PID #{$1}”
else
puts “found another PID #{$1}”
end
end
end
}
}
sleep 30

···

On Thursday 25 July 2002 02:44 pm, David King Landrith wrote:


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

This is going to sound like an odd question, and there’s no “right"
or “wrong” answer, but when you read code, what does the iterator
"sound” like in your head as you read?

Meaning, given:

a = [1, 2, 3]
a.each {|x| p x } # <- this line

How do you read the “this line” line? My brain wants to say "with"
or “using” for x, so I tend to translate it as “a each with x, print
x”, or “a each using x, print x”. Or do you just say “a each x,
print x”?

Ok, I’m babbling.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Health - Feel better, live better

a each do on x p of x

mathmatical lingo. use ‘of’ for arguments. techinically the block is an
argument too, but ruby offers us ‘do’ as a lingustical deliminator. and
‘on’, well, i think mathamaticlly they use the term ‘over’, but
sometimes you hear ‘on’ and its shorter :slight_smile:

~transami

···

On Thu, 2002-07-25 at 16:40, Michael Campbell wrote:

This is going to sound like an odd question, and there’s no “right”
or “wrong” answer, but when you read code, what does the iterator
“sound” like in your head as you read?

Meaning, given:

a = [1, 2, 3]
a.each {|x| p x } # ← this line

for each x in a to p(x)

“for each x in a, print x”.

It’s marginally too bad that we can’t have something looking more
like:

[ \prod_{\forall x \in a} {Print x} ]

···

The world rejoiced as Michael Campbell michael_s_campbell@yahoo.com wrote:

This is going to sound like an odd question, and there’s no “right”
or “wrong” answer, but when you read code, what does the iterator
“sound” like in your head as you read?

Meaning, given:

a = [1, 2, 3]
a.each {|x| p x } # ← this line

How do you read the “this line” line? My brain wants to say “with”
or “using” for x, so I tend to translate it as “a each with x, print
x”, or “a each using x, print x”. Or do you just say “a each x,
print x”?

Ok, I’m babbling.


(concatenate 'string “aa454” “@freenet.carleton.ca”)

Marriage means commitment. Of course, so does insanity.

a = [1, 2, 3]
a.each {|x| p x } # ← this line

How do you read the “this line” line? My brain wants to say “with”
or “using” for x, so I tend to translate it as “a each with x, print
x”, or “a each using x, print x”. Or do you just say “a each x,
print x”?

a for each of your x do this…

yct

a = [1, 2, 3]
a.each {|x| p x } # ← this line

in ‘a’ each as ‘x’ p x

In 20020725224027.90485.qmail@web12403.mail.yahoo.com Michael Campbell
wrote:

This is going to sound like an odd question, and there’s no “right”
or “wrong” answer, but when you read code, what does the iterator
“sound” like in your head as you read?

Meaning, given:

a = [1, 2, 3]
a.each {|x| p x } # ← this line

How do you read the “this line” line? My brain wants to say “with”
or “using” for x, so I tend to translate it as “a each with x, print
x”, or “a each using x, print x”. Or do you just say “a each x,
print x”?

“for each in a, as x, do p of x”

Questions like this always intrigue me because the various replies seem to say
something beyond the obvious. I’ve read in some linguistics literature that
one of the reasons most adults have trouble learning foreign languages is
that their first language, their “native” language, is a filter that all new
languages must pass through to reach their internal lexer. A translation
must always be made into native language and only then is meaning assigned.
I don’t know if this is actually true. But I suspect it is close to the
truth.

As programmers, I think that we subconsciously have such a "native"
programming language, i.e. a sort of pseudo-code that we think with; that is
composed of snippets of our favorite aspects of abstract languages, real and
imagined.

So that, these kinds of questions invoke a “native” language response.

Sorry, typo:
for each x in a do p(x)

···

On Thursday 25 July 2002 06:34 pm, Albert Wagner wrote:

for each x in a to p(x)

So that, these kinds of questions invoke a “native” language
response.

I think you’re largely correct. I have noticed that when I adopt a
new (programming) language, it takes a while to start using its
idioms rather than crude ports of the last language I’d been using.
It takes a bit to “think” in the new language.

I don’t have any smalltalk background, so some of that ancestry in
ruby I’m having to learn as it is completely new, sometimes coming up
with my own “internal term” for it.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Health - Feel better, live better

that has the same meaning but might be confused with the actual use of
the for statement.

···

On Thu, 2002-07-25 at 17:41, Albert Wagner wrote:

Sorry, typo:
for each x in a do p(x)

Even when using a “native” language, there is also the problem of a "reading"
vocabulary vs. a “use” vocabulary. In English, as well as in Ruby, the
vocabulary I “speak” with is much smaller and simpler than my reading
vocabulary; i.e. When I read novel or a program I understand many more
language constructs than I habitually use.

I don’t think meaning entered into the question. It was “what does the
iterator ‘sound’ like in your head as you read?” It’s my head. I’m the only
one likely to be confused.

···

On Thursday 25 July 2002 06:44 pm, Tom Sawyer wrote:

On Thu, 2002-07-25 at 17:41, Albert Wagner wrote:

Sorry, typo:
for each x in a do p(x)

that has the same meaning but might be confused with the actual use of
the for statement.

oh. okay.

yes, i was being objective. sorry.

if your interested then do you you know what it sounds like to me in my
head?

nothing.
···

On Thu, 2002-07-25 at 22:33, Albert Wagner wrote:

On Thursday 25 July 2002 06:44 pm, Tom Sawyer wrote:

On Thu, 2002-07-25 at 17:41, Albert Wagner wrote:

Sorry, typo:
for each x in a do p(x)

that has the same meaning but might be confused with the actual use of
the for statement.

I don’t think meaning entered into the question. It was “what does the
iterator ‘sound’ like in your head as you read?” It’s my head. I’m the only
one likely to be confused.


~transami

Sorry, typo:
for each x in a do p(x)

that has the same meaning but might be confused with the actual
use of
the for statement.

I don’t think meaning entered into the question. It was “what does
the
iterator ‘sound’ like in your head as you read?” It’s my head.
I’m the only
one likely to be confused.

yes, it was a merely syntactic (phonetic?) question; what
does the line sound like.

I often confuse myself, however.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Health - Feel better, live better

No need to apologize. I should have used a smiley.
To tell the truth, I really don’t hear voices when I read code either :slight_smile: I
actually took the question to mean something like: “If you had to quickly
explain in 10 words or less what this construct means to you, what would you
say?”

···

On Thursday 25 July 2002 11:40 pm, Tom Sawyer wrote:

oh. okay.

yes, i was being objective. sorry.

if your interested then do you you know what it sounds like to me in my
head?

nothing.