Thread Synchronization #2

Hi,

  I have a shared resource, say a file which is to be accessed between 
multiple 
  threads.  There are say 20 Read threads and 1 Write thread. When the Write 
  thread is writing into the file, the Read threads should be blocked, and 
when even 
  1 read thread reads, the Write thread should be blocked. The Read threads 
can 
  read simultaneously, but only say 5 at a time. What kind of synchronization 
  mechanism can I use for this?

 TIA,
 Sucharit
0
SD (74)
5/31/2005 8:50:08 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
612 Views

Similar Articles

[PageSpeed] 15

You can create a mutex a adquire it when a read operation start, check it 
before start any write operation.
You will need another mutex for check where there is a write running. You 
must check this second mutex before start any read operation.

You can use WaitForSingleObject functions for wait to read or write.

In order to ensure no more than five readers at a time you can user a 
semaphore.

-- 
Un saludo
Rodrigo Corral Gonz�lez [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


0
5/31/2005 9:29:54 AM
In that case, assume a Read thread acquires a mutex and starts reading,
and I use a semaphore to allow 5 Readers. If the Read thread that acquired
the mutex releases it, and the other Readers are not done yet, the Writer
will get the impression that the Readers are done, and start writing, and 
this will
lead back to the original problem.

"Rodrigo Corral [MVP]" wrote:

> You can create a mutex a adquire it when a read operation start, check it 
> before start any write operation.
> You will need another mutex for check where there is a write running. You 
> must check this second mutex before start any read operation.
> 
> You can use WaitForSingleObject functions for wait to read or write.
> 
> In order to ensure no more than five readers at a time you can user a 
> semaphore.
> 
> -- 
> Un saludo
> Rodrigo Corral González [MVP]
> 
> FAQ de microsoft.public.es.vc++
> http://rcorral.mvps.org
> 
> 
> 
0
SD (74)
5/31/2005 10:07:08 AM
A rough solution:
Prepare 20 critical sections for the 20 readers and the writer:
look at this pseudocode:
UINT writer(..)
{
   crit_sec1.Enter();
   crit_sec2.Enter();
   // the others

   // writing process

   crit_sec20.Leave()
   crit_sec19.Leave();
   // the others
}

UINT Reaer1(..) {
   crit_sec1.Enter();
   // ..
   crit_sec1.Leave();
}
// other readers..

//////////////

For 5 readers managing use a semaphore.

-- 
======
Arman
0
armancho_x1 (249)
5/31/2005 10:34:01 AM
You should check the semaphore when you are going to read. The state of a 
semaphore is set to signaled when its count is greater than zero, and 
nonsignaled when its count is zero. You can check the samphore status using 
WaitForSingleObject.

if (WaitForSingleObject(hSemaphore, 0) == WAIT_TIMEOUT)
{
    //Semaphore not signaled, you can write.
};

-- 
Un saludo
Rodrigo Corral Gonz�lez [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org



-- 
Un saludo
Rodrigo Corral Gonz�lez [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


0
5/31/2005 11:07:10 AM
http://www.codeproject.com/threads/ProducerConsumerModel.asp


-- 
Un saludo
Rodrigo Corral Gonz�lez [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org


0
5/31/2005 11:11:56 AM
Reply:

Similar Artilces:

MFC and threads #3
Greetings, I am running into a problem when trying to pause a thread in MFC based application. This is my first time using user defined Events for synchronization. The scenario is like this. Code executes in a worker thread and based on some conditions thread launches another new dialog window. That window displays some message to user ie, Hello There! in an Edit control. I provide a Resume button on the bottom of that message Window so that user can continue after reading the message and the worker thread should continue from where it left off. This is the code for that Resume button. v...

Problems with Outlook hanging (possibly CRM1.2 problem)
Hi, Apologies for the cross posting, but I'm not sure which is th ebest newsgroup to help... I'm Running a server with SBS 2003 & exchange. Installed CRM1.2 (at the moment it's only on the server & I can access via the web). I've not yet installed the Outlook client on my laptop. The problem is that I've been experiencing major "hangup" problems when connecting my laptop to the server. Everything works fine until I open up Outlook, then, although I an "ping" the server I am unable to access it either from Outlook to synchronise emails, ...

Outlook templates #2
I am expecting to receive email from a large group of people. I want to automatically reply to each of them with a template. I can set up a rule to do this. However, what I am not finding is the details. I found the rule function, and I have the .oft templates installted. I am not successful editing a template for my specific needs. I have 5 seperate replies I want to send based on the subject of the message received. Is this possible? AdvaThanksnce Daniel. ...

yet another resource editting thread
I need to edit resources at runtime (i.e. the string table, menus, and dialog control sizes). I would like to use the BeginUpdateResource, UpdateResource, and EndUpdateResource functions. One of the problems I have is this: If I load the menu from file with FindResource, LoadResource, LockResource, I have the binary menu data, and I can't really manipulate raw binary. If I load the menu with LoadMenu, i have a CMenu, that I can change but I don't know how to convert the final menu back to binary data that I would update with the UpdateResource command. So, how do you update? Another...

Passing Outlook objects between threads C++
Im using C++ VS2005 and in the main thread I establish a reference to an Outlook Contact Item. I call the Display method and it correctly creates an inspector and displays the Contact. While the Contact is open I pass a IDISPATCH pointer to the Contact to another thread. In the new thread I AddRef the Contact, the pointer seems good because I can get various properties, FileAs etc. However when I try to call the Close method I get an invalid access exception (the method does not return with an hr result). Watching the Inspector events, OnClose and OnCloseComplete do get called before ...

Exchange Reporting #2
Hello there. We are using Exchange Server 2003 in our company. My management would like me to pull a report stating how many inbound messages are received each day by each recipient in exchange. Is this possible? I have not seen any reporting functions in the Exchange Manager which would allow me to do this. Sean MIS Systems Manager It is not possible natively. You would need to look into a 3rd party monitoring/reporting solution in order to achieve this. Something like Promodag Reports, or Quest MessageStats ought to be able to provide this information. -- Ben Winzenz Exchange M...

Mail merge from Excel #2
Hello I am trying to use the 2nd worksheet of an Excel spreadsheet for a mail merge, but the mail merge helper only allows me to select the 1st worksheet. I don't want to change the worksheet order because I have another mail merge running from the 1st sheet. Is there a way of selecting the 2nd sheet Thanks in advance Jo, see the like below about using Microsoft Excel Worksheet via Converter to do it. You could also copy the page to a new workbook and use it as your temporary data source. I have run into problems with using some date in excel when I try to merge with word and have overc...

Windows CE and Databases in VS 2005 #2
I want to create a DB app in windows CE. I do not have experience with t CE. Can you still use .cdb files in Windows CE? Or must one use SQL databases? It seems there's not much documentation for this. Any help appreciated!!! TIA b ...

Do not have permission to send to this recipient #2
Hi there, I'm having a problem with Advanced Mailbox rights in native mode Exchange 2003 in a mixed-mode 2000/2003 AD domain. Client is Outlook 2003 on either Win2k or XP sp1/2 I would like to give a set of users permissions to a common service account mailbox from which they can generate a response "From" that mailbox (i.e., not their personal account). I do this by going to the Exchange Advanced Tab|Mailbox Rights and assigning the user Full Mailbox Access. The user is then able to add the mailbox to Outlook, except when they try to send the email FROM that account, t...

XP Home SP 2 and Web based Outlook
I used to be able to flawlessly remotely access Outlook remotely using IE and SP1. However, once I installed SP2, I can longer reply to any messages. My INBOX appears correctly and I can view messages, but once I hit the REPLY icon, the message I was viewing disappears and I am returned to my INBOX rather than being able to compose a message. I upgraded to SP2 on two different computers and the same problems exists on both machines. SP2 for Windows XP includes a built-in popup stopper for Internet Explorer. Open Internet Explorer and select Tools | Internet Options | Privacy tab. Se...

Cannot view itme in Public Folder between 2 different exchange 2003
I have 2 Exchange 2003 on WIN2k server. Exchange 2003 (A) has 300 mailboxes and store the public folder. Exchange 2003 (B) has 100 mailboxes. User at Exchange 2003 (B) found that they can view the public folder but no items in it. User at Exchange 2003 (A) do not have this problem. Is the public folder must replicate to other server? Is any suggestion to view the item if no replication on public folder ?? Thanks Dennis JoJo These 2 servers in the same routing group. Exchange 2003 (A) is the Master Exchange 2003 (B) is the Member "omer maydan" <omermaydan@yahoo.com> wr...

Publisher2003 #2
I'm using Publisher2003 for the first time and I'm creating a letter to send out to businesses. Down at the bottom of the template is says "If you wish to be removed from our mailing list, click here. The 'click here' is underlined and in blue, but it doesn't go anywhere. How do I create a link to my Yahoo email so they can be removed? The only way this is going to work if you send the letter as a web page. Or a PDF with a link. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Judith" <Jud...

Minimum Exchange 2003 for CRM 1.2?
Hello We are trying to setup a test deployment of CRM 1.2 for 5 users We (parent organisation) already has a email system (UNIX , pop, smtp) in place. I wish to learn what is the minimum Exchange setup that I need to do to deploy the test environment Is there a way to to configure the Exchange server such that it still uses the parent email system and still be used with the CRM thank you very much for your help vikrantca Hello, vikrantca wrote: > We are trying to setup a test deployment of CRM 1.2 for 5 users. > We (parent organisation) already has a email system (UNIX , pop, > smt...

No Mail Delivery #2
possible duplicate-original didn't show in 30 minutes. Last night I installed SP1 to my exchange server, it went through the install and at the end informed me that the services could not install causing the install to fail. Server roobooted fine. The next day now no mail is being delivered internally or externally. When I go to system manager under that server and qeues most of the states are set to retry. I have under local delivery on the default smtp virtual server the state is retry with several hundred messages awaiting delivery. I have stopped and started the smtp se...

Journal stopped working #2
I have noticed that after upgrading from Outlook XP (2002) to Office Outlook 2003 my journal has completely stopped logging entries. I have turned it off and re-initialized it and still not entries. I mailing want it to log Word and Excel files. Any help would be appreciated I am running Exchange 2003 in cached outlook mode on a SBS 2003 server. ...

Printing #2
I understand that NLQ printing is default in windows. Is there any way one can get a quick draft print of excel reports on a dot matrix printer by changing the printer driver or fonts for instance? Thank you. Hiren, I'm not sure about this setting, but try "Draft quality" in File - Page setup - Sheet. -- Earl Kiosterud mvpearl omitthisword at verizon period net President Bush on sovereignty: http://www.tastefullystated.com/sovereignty.wmv ------------------------------------------- "Hiren Shah" <hiren_shah50@hotmail.com> wrote in message news:054901c4961...

Calendar Templates #2
2 Questions One; how do you get a calendar to display an academic year on one page? Two; how do you highlight or put a square or circle around or week on the monthly text boxes on a yearly calendar? What version Publisher? All the calendar options are on the template page if you are using 2007. There are date range, page orientation and applying all the months on one page are in these options. You cannot designate a date range when using all the months on one page. The workaround is to first create a full size calendar with your date range, okay out. Click change template, change the...

IF(This=2 and That=1,X,Y)
Im comparing 2 values per. I need to evaluate if THIS = 1 and THAT = 2, then 1 otherwise 0 How do I do that in an expression? I always need to evaluate BOTH and they both need to be true. Any help would be appreciated. This is what I guessed, but doesn't work, surely there must be a method to evaluate both as the same time? IF R14 is 1 AND S14 is 1 then 7, otherwise 0 as: =IF(R14=1+S14=1,7,0) Use the AND() function: =IF(AND(This=1,That=2),1,0) "No Spam" <no_spam@eparisek.com> wrote in message news:8453884A-EBEC-44E8-9AD8-E7690F361AB0@microsoft.com... > Im...

Moving Exchange 2003 to new Hardware #2
I'm in the middle of my main server move.. this server is Primary Domain Controller, exchange 2003 server, DNS and DHCP hosts, AntiVirus (Symantec), and print server. This is on a Win2k3 sp 1 OS. I've moved Symantec first, that was easy. Now I'm moving Exchange 2003 - I've installed Exchange, both servers are now running the same version and I see both in the EMS. I've started to set the public folders to replicate from my oldserver to newserver.. The public folders replicated twice, and now stopped.. they are out of sync (I think this is related to a problem I'l...

How do I import old Publisher files? #2
I was told to turn off Norton, can you help me? ...

Pivot table field dropdown list reset #2
I would like to know if there is a way to reset a field dropdown list in a pivot table. In some field sometimes I delete data from the source and some items of the field will no longer be there. However, when I refresh the pivot table the dropdown list will still show the old items even if they're not in the source data anymore. I really need your help with this. Thanks a lot in advance for your help, Francisco Molina ...

Arrays #2
Hello... I was be so grateful if some Excel Expert could offer a solution to this problem I have understanding how to handle arrays in Excel. I have a worksheet contains a list of events down the page [in column one], and a list of athletes aross the page [in row one]. I need to note which athletes will compete in which events by placing a character in the corresponding cell to make an array of data showing athlete participation in events. Athletes can be in more than one event. I need to then work out how to write a formula or function to generate a list from this array showing which ath...

How do I compare output data from 2 pivot tables in a graph forma.
I have a pivot table created for 2004 that shows the number of inspections we received on a particular day of the week (ie. 5 on saturdays, 2 on wednesdays, etc.) I am creating the same pivot table for 2005 and am looking for a way to graph the data (bar graph) of the pivot table from 2004 to compare with my 2005 data (ie. last year we received 5 inspections on saturdays, this year we received xx amount on saturdays). Is this even possible to do? I am using Office Pro 2003 and any suggestions would be greatly accepted. Doobi, One option is to combine the pivot tables for 2004 and...

Bar chart & Category (x) axis labels #2
If you make a bar chart in Excel 2003, and have two columns of text as Category (x) axis labels, the first column is shown vertical and the text in the second column is shown horizontal in the chart. If you open/make the same chart in Excel 97, the text in both columns is shown horizontal. If there is a lot of text, it becomes unreadable when it's vertical. Is this by design or can it be fixed somehow? ...

Time problem #2
1st thanks for reading this... I give up and need help. I'm trying to put in a colon (:) automatically in a colunm when I enter a number. This is minutes I put this number in A1 34 and want it to convert to :34 it doesn't have to be a time format, I just need the colon. Thanks-a-bunch Bobby Hi Try using a custom format of ":"general -- Andy. "Bobby" <camaro46368@yahoo.com> wrote in message news:b6b8e0h22tlfndiq0ajb0jfck4s5erm1jp@4ax.com... > 1st thanks for reading this.....