πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.tools.mfc
Article #110

Re: semaphore

#110
From: Steve Adams
Date: Tue, 13 Feb 2001 21:39
74 lines
4707 bytes
Semaphores are effectively usage counters.

You create a semaphore with an initial value and a maximum it can be. The 
create semaphore prototype is:

HANDLE CreateSemaphore(
  LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
                       // pointer to security attributes
  LONG lInitialCount,  // initial count
  LONG lMaximumCount,  // maximum count
  LPCTSTR lpName       // pointer to semaphore-object name
);

NB a semaphore can be shared across multiple processes, the lpName is a 
name that can be used in the 
HANDLE OpenSemaphore(
  DWORD dwDesiredAccess,  // access flag
  BOOL bInheritHandle,    // inherit flag
  LPCTSTR lpName          // pointer to semaphore-object name
);
 
function to open a pre-existing semaphore if known. So if you have more 
than one instance of your application running ensure they have unique 
semaphore names if you don't want the behaviour.
CreateSemaphore may return ERROR_ALREADY_EXISTS if this is the case.

In use you see if you can create a thread by calling 
WaitForSingleObject(hYourSemaphore,...) and either wait for a thread to 
finish or bomb out straight away.
If it succeeds then the usage count has been increased by 1.
As the thread exits get it to call ReleaseSemaphore(hYourSemaphore,1,NULL 
or a pointer to a long) Which decreases the usage count.
This means that all your threads will be synchronised with a little effort, 
consider using some __try __finally __except in your application to ensure 
that the semaphore gets released come what may.



lucas_montes@club-internet.fr (lucas montes) wrote in
<963plr$iml$1@front3.grolier.fr>: 

>
>Rufus Smith <RufusVS@aol.com> a οΏ½crit dans le message :
>#eU#AygkAHA.1892@tkmsftngp05...
>> Why not create a shared "active download" counter that
>> each downloader increments when download starts and decrements
>> when it stops.  When counter hits maximum allowed, don't start
>> a download.
>>
>> Or don't I understand the issue.
>>
>> lucas montes wrote in message <944g6a$8st$1@front7.grolier.fr>...
>> >Hello, does someone know how to use CSemaphore (MFC) in my case:
>> >i use a worker thread.
>> >the thread function download files.
>> >this function is a member of a class CDownload.
>> >i've got a list of CDownload i passed calling the function Download.
>> >i want to limit the simultaneous downloads.
>> >i think i should use a semaphore to limit the number of running
>> >threads. i'd like to know the method.
>> >
>> >Thanks.
>> >
>> >
>>
>>
>
>I don't do that because to increment or decrement this variable, u have
>to synchronize threads because they can't access the same var at the
>same moment. someone told me semaphore is a number incremented and
>decremented wich limit number of threads running. but i ve got some pbs
>using semaphore. 
>
>
Technical Details
Message-ID: <9047D9E5Fsadamsiscouseworldid@158.152.254.74>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-04!supernews.com!xfer13.netnews.com!netnews.com!feed2.news.rcn.net!rcn!dispose.news.demon.net!news.demon.co.uk!demon!scouseworld.demon.co.uk!not-for-mail
References: <944g6a$8st$1@front7.grolier.fr> <#eU#AygkAHA.1892@tkmsftngp05> <963plr$iml$1@front3.grolier.fr>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 1138df,8ca6d3a0ceae79d4,start
X-Google-Attributes: gid1138df,public
X-Google-Thread: 1053e6,8ca6d3a0ceae79d4,start
X-Google-Attributes: gid1053e6,public
X-Google-Thread: f5082,8ca6d3a0ceae79d4,start
X-Google-Attributes: gidf5082,public
X-Google-Thread: 111825,8ca6d3a0ceae79d4,start
X-Google-Attributes: gid111825,public
X-Google-Thread: fa08f,8ca6d3a0ceae79d4,start
X-Google-Attributes: gidfa08f,public
X-Google-Thread: f4307,8ca6d3a0ceae79d4,start
X-Google-Attributes: gidf4307,public
X-Google-Thread: 10171e,8ca6d3a0ceae79d4,start
X-Google-Attributes: gid10171e,public
X-Google-Thread: 102e15,8ca6d3a0ceae79d4,start
X-Google-Attributes: gid102e15,public
X-Google-ArrivalTime: 2001-02-13 13:40:12 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-04!supernews.com!xfer13.netnews.com!netnews.com!feed2.news.rcn.net!rcn!dispose.news.demon.net!news.demon.co.uk!demon!scouseworld.demon.co.uk!not-for-mail
From: sadamsi@scouseworldi.demoni.coi.uki (Steve Adams)
Newsgroups: comp.os.ms-windows.programmer.tools.mfc,fr.comp.developpement,fr.comp.lang.general,fr.comp.os.ms-windows.programmation,francom.programmation,microsoft.public.fr.vc,microsoft.public.fr.vstudio,microsoft.public.vc,microsoft.public.vc.mfc
Subject: Re: semaphore
Followup-To: microsoft.public.vc.mfc
Date: Tue, 13 Feb 2001 21:39:30 GMT
Organization: Your Company
Message-ID: <9047D9E5Fsadamsiscouseworldid@158.152.254.74>
References: <944g6a$8st$1@front7.grolier.fr> <#eU#AygkAHA.1892@tkmsftngp05> <963plr$iml$1@front3.grolier.fr>
NNTP-Posting-Host: scouseworld.demon.co.uk
X-NNTP-Posting-Host: scouseworld.demon.co.uk:194.222.171.177
X-Trace: news.demon.co.uk 982100370 nnrp-10:29903 NO-IDENT scouseworld.demon.co.uk:194.222.171.177
X-Complaints-To: abuse@demon.net
User-Agent: Xnews/03.08.26
Lines: 75