Save to use const Regexp in several threads?

Hi all,

out of curiosity: is it safe to define a global constant regular
expression and use that concurrently from several threads? I put together
a test script that seems to indicate it is safe, but a test is no definite
answer.

Kind regards

robert

#!/usr/bin/ruby

require ‘set’

RX = /./

threads = []

def create(s)
Thread.new(s) do |str|
set = Set.new

str.scan(RX) do |m|
  set << m
  Thread.pass
end

# set should contain more than one entry if
# it is not safe
p set.to_a

end
end

%w{a b c d e f}.each do |c|
threads << create(c * 1000)
end

threads.each {|th| th.join}

Hi,

At Fri, 26 Mar 2004 23:19:28 +0900,
Robert Klemme wrote in [ruby-talk:95917]:

out of curiosity: is it safe to define a global constant regular
expression and use that concurrently from several threads? I put together
a test script that seems to indicate it is safe, but a test is no definite
answer.

Yes if it is without `o’ option or in recent version.

···


Nobu Nakada

without the ‘o’ - i would have thought the compile once option would be
required for this since you wouldn’t want all the thread re-compiling it?

so you are saying

pat = %r/foo/ # right
pat = %r/foo/o # wrong

Thread.new{ pat.match s}
Thread.new{ pat.match s}

??

-a

···

On Sat, 27 Mar 2004 nobu.nokada@softhome.net wrote:

Hi,

At Fri, 26 Mar 2004 23:19:28 +0900,
Robert Klemme wrote in [ruby-talk:95917]:

out of curiosity: is it safe to define a global constant regular
expression and use that concurrently from several threads? I put together
a test script that seems to indicate it is safe, but a test is no definite
answer.

Yes if it is without `o’ option or in recent version.

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

Hi,

At Sat, 27 Mar 2004 01:44:31 +0900,
Ara.T.Howard wrote in [ruby-talk:95924]:

without the ‘o’ - i would have thought the compile once option would be
required for this since you wouldn’t want all the thread re-compiling it?

so you are saying

pat = %r/foo/ # right
pat = %r/foo/o # wrong

Thread.new{ pat.match s}
Thread.new{ pat.match s}

No, regexp with o option will be evaluated at the written
place, so the above has no multi-threaded problems.

I meant:

n.times {
Thread.new {
%r/#{yields_to(other_threads) while evaluating(long, time)}/o
}
}

···


Nobu Nakada