[Win32utils-devel] win32-mutex

win32utils-devel at rubyforge.org win32utils-devel at rubyforge.org
Sat May 1 23:53:13 EDT 2004


Hi,

> I've committed documentation and a test suite for win32-mutex.  Once
again,
> I'm looking for a good sample program that we can use for the test.rb file
> that really demonstrates what you can accomplish with it.  I scoured the
web
> but didn't find anything good.
>
> On another note, I came across another implementation called "fmutex" that
I
> thought might be worth investigating at
> http://www.software-path.com/scripts.html.
>
> I also came across this bit of C++ code that I thought might be of
interest:
> http://www.codeproject.com/threads/opbmutex.asp
>
I found a bug in mutex.c related with Mutex.open and committed fix of it.
Following is a test code for win32-mutex.

Regards,
Park Heesob

##########################################
MUTEXNAME = "This is a very long name"

require "win32/mutex"
require "win32/process"
require "win32/mmap"
include Win32

pid = Win32::Process.fork

#child
if pid.nil?
  mm = Mmap.new(nil,nil,nil,true)   # reuse existing mmap
  mx = Mutex.open(MUTEXNAME)
  while(true)
    mx.wait
  puts "child1 wait "
    mm.setvar("GValue",mm.getvar("GValue") + 123)
    sleep(1)
    mm.setvar("GValue",mm.getvar("GValue") - 123)
    mx.release
  puts "child1 release "
  end
  exit(1)
end

pid2 = Win32::Process.fork

#child2
if pid2.nil?
  mm = Mmap.new(nil,nil,nil,true)   # reuse existing mmap
  mx = Mutex.open(MUTEXNAME)
  while(true)
    mx.wait
  puts "child2 wait "
    mm.setvar("GValue",mm.getvar("GValue") + 456)
    sleep(2)
    mm.setvar("GValue",mm.getvar("GValue") - 456)
    mx.release
  puts "child2 release "
  end
  exit(1)
end

#parent
mm = Mmap.new
mm.setvar("GValue",0)
mx = Mutex.new(false,MUTEXNAME) #  named mutex object

while(true)
  mx.wait
  puts "parent wait"
  sleep 5
  printf("Value of GValue=%d\n",mm.getvar("GValue"))
  mx.release
  puts "parent release"
end

p Win32::Process.waitpid2(pid)
p Win32::Process.waitpid2(pid2)

puts "Continuing on..."
#############################################





More information about the win32utils-devel mailing list