473,464 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Semaphore or what should I use?

Hi

I´m wondering why there are so few examples with Semaphore.
Is it obsolete?

I´ve got a Class Data.
It offers 2 Threads methods for updating, editing, .. a private
dictionary.

Now I have to make sure, that both threads are synchronal,
1 thread edits something and the other is blocked until the first
thread is ready.

Isn´t it a good idea to do this with a semaphore?

And if I should use a Semaphore here, could anybody give me an example
how it should look like?

Everything that I test throws errors :(

Thank you :)
Bye, Bastian
Jul 18 '05 #1
7 4336
>>>>> "Bastian" == Bastian Hammer <we*******@haustierworld.de> writes:

Bastian> Now I have to make sure, that both threads are
Bastian> synchronal, 1 thread edits something and the other is
Bastian> blocked until the first thread is ready.

Bastian> Isn´t it a good idea to do this with a semaphore?

Semaphore will do, but this is a classical use case for
threading.Lock.

There should be lots of stuff regarding locks (or more googleably,
"mutexes") on the net.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #2
Ville Vainio a écrit :
>>"Bastian" == Bastian Hammer <we*******@haustierworld.de> writes:

Bastian> Now I have to make sure, that both threads are
Bastian> synchronal, 1 thread edits something and the other is
Bastian> blocked until the first thread is ready.

Bastian> Isn´t it a good idea to do this with a semaphore?

Semaphore will do, but this is a classical use case for
threading.Lock.

There should be lots of stuff regarding locks (or more googleably,
"mutexes") on the net.


I don't agree. Mutexes (or locks) are best suited for critical sections
(ie. sections that cannot be run by many thread at the same time). The
kind of synchonisation Bastian want is not really semaphore either but
more event. This python "Event" object is described in the section 7.5.5
of the documentation of Python 2.3. There is no example, but I think
Event are quite strait forward : you creates it, then some thread block,
waiting the event to occure while some other thread execute until it set
the event, allowing the blocked thread to go on its own execution :)
Here a small working example :

***8<************8<***************8<**********
import threading, time

class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._event = threading.Event()
self._exit = False
def run(self):
while 1:
print "Waiting for an event to continue"
self._event.wait()
print "Ok, the thread is unblocked now :)"
if self._exit:
return
self._event.clear()
def unblock(self):
self._event.set()
def exit(self):
self._exit = True
self.unblock()

t = MyThread()
t.start()
time.sleep(1)
t.unblock()
time.sleep(1)
t.unblock()
time.sleep(1)
t.exit()
***8<************8<***************8<**********
Pierre
Jul 18 '05 #3
Pierre Barbier de Reuille <pi************@cirad.fr> writes:
Ville Vainio a écrit :
>>>"Bastian" == Bastian Hammer <we*******@haustierworld.de> writes:

Bastian> Now I have to make sure, that both threads are

Bastian> synchronal, 1 thread edits something and the other is
Bastian> blocked until the first thread is ready.
Bastian> Isn´t it a good idea to do this with a semaphore?

Semaphore will do, but this is a classical use case for
threading.Lock.

There should be lots of stuff regarding locks (or more googleably,
"mutexes") on the net.

I don't agree. Mutexes (or locks) are best suited for critical sections (ie.
sections that cannot be run by many thread at the same time).


Please don't add even more confusion to the issue. Mutex conceptually is
designed to be used for MUTual EXclusion of access to a resource (e.g.,
a peace of data). While critical section could be implemented using
mutex, the mutex itself is more general concept. Besides, the rule of
thumb using mutexes is: "protect data, not program code."

My answer to OP's question is: use either lock (mutex) or semaphore.
I'd probably use semaphore as mutexes are usually optimized for the case
when contention probability is low (i.e., they usually shouldn't be locked
for a long time).

--
Sergei.
Jul 18 '05 #4
>>>>> "Sergei" == Sergei Organov <os*@javad.ru> writes:

Sergei> My answer to OP's question is: use either lock (mutex) or
Sergei> semaphore. I'd probably use semaphore as mutexes are
Sergei> usually optimized for the case when contention probability
Sergei> is low (i.e., they usually shouldn't be locked for a long
Sergei> time).

Both Mutexes and Semaphores put the thread to sleep, so I don't think
there will be a big difference.

The OP might also want to check out read/write lock. The first thing
google finds is

http://www.majid.info/mylos/weblog/2004/11/04-1.html

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #5
Ville Vainio <vi***@spammers.com> writes:
>> "Sergei" == Sergei Organov <os*@javad.ru> writes:


Sergei> My answer to OP's question is: use either lock (mutex) or
Sergei> semaphore. I'd probably use semaphore as mutexes are
Sergei> usually optimized for the case when contention probability
Sergei> is low (i.e., they usually shouldn't be locked for a long
Sergei> time).

Both Mutexes and Semaphores put the thread to sleep, so I don't think
there will be a big difference.


Yeah, most probably from this point of view there is no difference
(mutexes could be implemented using busy-waiting, but I don't think it's
the case). However, though I don't think it's a real issue in this
particular case either, mutexes can do fancy things with the thread that
has locked the mutex (mutex owner thread), like raising its priority to
the highest one of the threads waiting for the mutex to prevent so
called priority inversion.

There could be other subtle differences between mutex and semaphore
behavior resulting in different performance of the application. The
application correctness shouldn't be affected though.

--
Sergei.
Jul 18 '05 #6

Pierre Barbier de Reuille <pi************@cirad.fr> wrote:

Ville Vainio a écrit :
>>>"Bastian" == Bastian Hammer <we*******@haustierworld.de> writes:



Bastian> Now I have to make sure, that both threads are
Bastian> synchronal, 1 thread edits something and the other is
Bastian> blocked until the first thread is ready.

Bastian> Isn´t it a good idea to do this with a semaphore?

Semaphore will do, but this is a classical use case for
threading.Lock.

There should be lots of stuff regarding locks (or more googleably,
"mutexes") on the net.


I don't agree. Mutexes (or locks) are best suited for critical sections
(ie. sections that cannot be run by many thread at the same time). The
kind of synchonisation Bastian want is not really semaphore either but
more event. This python "Event" object is described in the section 7.5.5
of the documentation of Python 2.3. There is no example, but I think
Event are quite strait forward : you creates it, then some thread block,
waiting the event to occure while some other thread execute until it set
the event, allowing the blocked thread to go on its own execution :)


You can agree or disagree as much as you want. Fundamentally, they are
all equivalent.

The only thing that makes mutex 'special' is that one can have an
optional 'call this function with this argument when it gets the lock',
but that can be implemented with a standard Lock, Condition, Event,
Semaphore, etc.
- Josiah

Jul 18 '05 #7
Sergei Organov a ecrit :
Pierre Barbier de Reuille <pi************@cirad.fr> writes:

Ville Vainio a ecrit :
>>>>"Bastian" == Bastian Hammer <we*******@haustierworld.de> writes:

Bastian> Now I have to make sure, that both threads are

Bastian> synchronal, 1 thread edits something and the other is
Bastian> blocked until the first thread is ready.
Bastian> Isn't it a good idea to do this with a semaphore?

Semaphore will do, but this is a classical use case for
threading.Lock.

There should be lots of stuff regarding locks (or more googleably,
"mutexes") on the net.

I don't agree. Mutexes (or locks) are best suited for critical sections (ie.
sections that cannot be run by many thread at the same time).

Please don't add even more confusion to the issue. Mutex conceptually is
designed to be used for MUTual EXclusion of access to a resource (e.g.,
a peace of data). While critical section could be implemented using
mutex, the mutex itself is more general concept. Besides, the rule of
thumb using mutexes is: "protect data, not program code."

My answer to OP's question is: use either lock (mutex) or semaphore.
I'd probably use semaphore as mutexes are usually optimized for the case
when contention probability is low (i.e., they usually shouldn't be locked
for a long time).


My point is : semaphore is more complex than what he needs. Event are
simpler and just do what he needs : block one thread until another one
finished some jobs and launchs the event (have a look at my example).

Afterward, I agree that the concept of mutex is the most general : you
can implement every other kind of lock using just mutexes.

Pierre
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dede | last post by:
Dear community, having written a working example for using Semphores on a windows client, I created a little "server-application" that does the following: A Server continously "listens/tails"...
1
by: rushik | last post by:
Hello all, I m facing an interesting problem related to semaphore in php. We are using a huge business application running on LAMP. For database operations we are maintaining centralized DB...
5
by: marvind | last post by:
I tried using a Semaphore class (have included the full listing reproduced from article Figure 1 at the end of this email) in .NET 1.1. It works fine most of the time, however, I see the following...
5
by: Unni | last post by:
Hello all, I m facing a memory related problem with semaphores. Our business application uses semaphores extensively and the limit imposed by the OS on the number of semaphores that can exist on...
2
by: techi_C | last post by:
Hi I'm getting a problem while removing semaphore from system. Before removing semaphore I'm checking the usage count of a smaphore. // checking usage count usage_count =...
1
by: cranfic | last post by:
Please help... To make it simple, I have a simple test C program which calls my semphore library functions only. (It doesn't call any db2 function at all). And the following is the test...
0
by: sukasa | last post by:
Greetings. After first noticing a malfunction in my application, I ran some tests which have given me the following insights on the code performance: -The " infinite" loop which waits on...
0
by: Samuel R. Neff | last post by:
I'm having trouble creating a Semaphore with read-access rights for everyone. Originally I was trying to use this code: semaphore = new Semaphore(maxLocks, maxLocks, "RwLock#" + name); but...
5
by: GHUM | last post by:
hello, in my application I am using hSem = win32event.CreateSemaphore (None, 1, 1,"stringincludinginterfaceandport") rt=win32event.WaitForSingleObject (hSem, 0) if rt !=...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.