When to use CCriticalSection?

I have an app in which there are 4 threads running simultaneously.
Right now am using "Events" to do the synchronization between
threads.

Like, there is a Server writing thread and then there is a Server
reading thread. Once Read thread, reads data from Server, it will Set
an Event for Write thread to continue.


So my question is, In this scenario do i need to use CCriticalSection?

0
donguy76 (15)
10/24/2007 5:08:59 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
647 Views

Similar Articles

[PageSpeed] 9

>I have an app in which there are 4 threads running simultaneously.
>Right now am using "Events" to do the synchronization between
>threads.
>
>Like, there is a Server writing thread and then there is a Server
>reading thread. Once Read thread, reads data from Server, it will Set
>an Event for Write thread to continue.
>
>So my question is, In this scenario do i need to use CCriticalSection?

If there exists the possibility that both threads (reader & writer)
will be accessing some shared data at the same time, then you need a
critical section for the accesses to that shared data. The way you've
described it, it doesn't sound like you have that situation - however
I then wonder why you have 2 threads.

Dave
0
davidl7375 (2060)
10/24/2007 5:35:49 PM
The Read thread after reading the data from server will update it to a
QUEUE object. And the other thread will access this QUEUE to read the
data.
So i guess in this case i will need a CriticalSection?

The purpose of having 2 threads is to keep sending and recieving side
independent/

0
donguy76 (15)
10/24/2007 6:14:47 PM
"Donos" <donguy76@gmail.com> wrote in message 
news:1193249687.441396.318540@i13g2000prf.googlegroups.com...
> The Read thread after reading the data from server will update it to a
> QUEUE object. And the other thread will access this QUEUE to read the
> data.
> So i guess in this case i will need a CriticalSection?
>
> The purpose of having 2 threads is to keep sending and recieving side
> independent/
>

If the threads block each other until they're done, then they are not 
independent.  One thread will do the same thing.

Access to your queue may need to be synchronized with a critical section or 
mutex if your threads can read and write at the same time.

If your threads canNOT read and write at the same time, you may as well just 
use one thread.  In your current implementation using events, this is the 
case.

Mark

0
Mark
10/24/2007 9:28:33 PM
Never.

The MFC synchronization primitives are not very well done and should be avoided.

Events are almost NEVER appropriate for synchronization between threads when you are
trying to protect shared state.

What you are doing here is a producer/consumer model with a pool of size 1 and a queue of
size 1, and special-casing it.  A better practice is to actually implement queues.  You
can do this in the most general case with semaphores (see my essay on semaphores on my MVP
Tips site) or by using a UI thread for the threads (they cross-post messages to indicate
state, thus they are implementing a Distributed Finite-State Machine (DFSM), or use a
model such as I/O Completion Ports used as simple queues (same DFSM model with a different
implementation).

You would use a CRITICAL_SECTION (the raw API) if it were possible to both read and write
the data concurrently.  You don't need synchronization of this nature if higher-level
synchronizations prevent concurrent access.

Therefore, the special-case method with Events, where there is no need to synchronize
concurrent reading and writing because there is none, is sufficient, if rather limited.
						joe

On Wed, 24 Oct 2007 10:08:59 -0700, Donos <donguy76@gmail.com> wrote:

>I have an app in which there are 4 threads running simultaneously.
>Right now am using "Events" to do the synchronization between
>threads.
>
>Like, there is a Server writing thread and then there is a Server
>reading thread. Once Read thread, reads data from Server, it will Set
>an Event for Write thread to continue.
>
>
>So my question is, In this scenario do i need to use CCriticalSection?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
10/24/2007 11:42:39 PM
"Donos" <donguy76@gmail.com> wrote in message
news:1193245739.449626.149070@z24g2000prh.googlegroups.com...
> I have an app in which there are 4 threads running simultaneously.
> Right now am using "Events" to do the synchronization between
> threads.
>
> Like, there is a Server writing thread and then there is a Server
> reading thread. Once Read thread, reads data from Server, it will Set
> an Event for Write thread to continue.
>
>
> So my question is, In this scenario do i need to use CCriticalSection?
>
>

Use events to synchronize the activites of threads, as you have done.

Use critical sections to synchronize access to shared data by multiple
threads.  If there is no shared data (such as in situations where each
thread has its own copy of data, like in pipes and sockets), then a critical
section is not needed.


0
10/25/2007 3:53:34 AM
Reply:

Similar Artilces:

Using combobox results when bound field is null
Access 2003: This combobox is a list of all the teachers, and also displays the number of children, and the number of families assigned to each teacher. Some children do not have a teacher assigned yet. The drop-down list, and the query it is based upon, correctly displays a null in the Teacher column, and the correct count of children and families in columns(1) and (2). However, when I look at the value of cboTeachers.Column(1) or cboTeachers.Column(2) in the Immediate window, the Locals window, or passed in a Message Box, they all show up as null values. I'd like to get a coun...

Passport information lost when using msn
Now that I am using Money 2006 and my msn password to login every time I go to my msn the login only has my account name. No @hotmail.com and no password. Even if I tell msn to save my account and password. I had the same problem. If you do NOT need to import financial data into Money 2006, you can go to 'File - Password Manager' and set up a normal password for this program. -- jim "John" wrote: > Now that I am using Money 2006 and my msn password to login every time I go > to my msn the login only has my account name. No @hotmail.com and no > passwor...

Can use VSTO to reduce the size of a file?
I have a really large spreadsheet that is saved in a sharepoint location but takes about five minutes to load. Would it make any difference if I recreate the file using VSTO? Thanks in advance ...

Dedicated hdd for page file, outer disc space used
Windows XP latest SP Hdd dedicated for page file installed, single partition. Page file of fixed size generated on this disc. While reviewing the partition with Windows on-board defragmentation utility, the page file is presented in outer partition sections. It doesn't start from partition beginning. I guess files critical for system performance should be placed at inner section - faster read/write access. Does windows on-board defrag utility really show the facts? Does this file is really placed in outer partition section? If yes, why does windows do that this way? k...

Using SSL with Outlook
Hi, I just found the initial question from Andi Dunstan (2004/03/17) but I'm afraid it does not answer my question:-( I'm having trouble to connect to our IMAP and SMTP servers over TLS/SSL where TLS/SSL is configured to require client authentication - what happens is that Outlook 6 and also Outlook Express 6 refuse to send the appropriate certificate. As a quick test I tried to connect with IE via https to the secure IMAP port. In the course of the SSL connection setup I was asked to select one of my installed certificates (actually, only the appropriate one showed up). After ...

Fields cannot be used in query parameter expressions
Hello, using SQL Reporting Services 2008. I trying to allow users to enter parameters to filter a report so I'm using stored procedures....common right. But I want a parent/child relationship between 2 datasets. The first ds pulls records based on the visible parameters entered by the user. The 2nd ds needs to use a key field in the first ds to populate a hidden parameter linked to the 2nd ds but I keep getting and error: "Fields cannot be used in query parameter expressions" How to use 2 stored procs that create datasets and link them using PK/ FK relationship? I ...

using macros to draw charts
I'm trying to write a simple macro which draws a bubble chart using a few cells of data from a worksheet. I designed the chart from data on the worksheet while recording, then got rid of the chart and ran the macro. No joy. It's stopping on the first line after Charts.Add - ActiveChart.ChartType = xlBubble and I don't understand why! Anyone got any bright ideas? Hi Josie - It's not your fault. In a few cases, the macro recorder gets confused and writes down the steps out of order. For some types of chart it doesn't matter, but for bubble charts, you have to use A...

using CTreeCtrl Select() w/out firing TVN_SELCHANGED
I have a tree view that has n items with n children each when a tree view item is selected, it causes a ListBox to update displaying the children of the tree view item. This is not my design and I can't change it :( here is where it gets tricky. When a user clicks an item in the ListBox(It can also be populated from a search, this would cause it to show items from many different tree view items(parents)) I need to go backwards and select the item in the treeview that is the parents of the item in the listbox. When I find the item, then call Select() it causes the listbox to update agai...

refer to a cell using a formula?
Hi, I want to refer to the contents of a cell with a variable representing the column, but I can't figure out how to make Excel do this in A1 (preferred) or R1C1 format - sort of the reverse of the ADDRESS function. More specifically, I want to sum the values for months of a year where the month is the variable. What's the syntax? A B C D 1 J F M A 2 2 3 2 4 3 2/1/05 4 =sum(text("A1:"&month(A3),)) other ideas that I can't seem to make work: char(month(a3)+64) column(a3) Thanks, Mike If I understand you correctly, on...

Practice Test Using Excel
I want to design a practice test for my students, who need to memorize -- verbatim -- a list of 37 characteristics. I would like to use an Excel 2007 spreadsheet to allow them to practice. Ideally, the practice spreadsheet would possess the following characteristics, based upon text-based input: - keyboarding the phrase verbatim changes the block to green; or, - keyboarding a phrase which does not match, but which does contain keywords (which I designate), changes the block to yellow; or, - entering a complete miss changes the block to red; or - leaving it blank turns it to red. ...

use sbs 2003 as webserver only
hello, i have some questions related to sbs 2003. i would like to host some community websites on sbs 2003 box. everything is installed like in the licensing agreement (ads, exchange ...). now my questions: 1. what are the differences between sbs 2003, server 2003 and server 2003 web edition? 2. are there any licensing violations i can't find any licensing information that would forbid to do so. any help would be appreciated This is a multi-part message in MIME format. ------=_NextPart_000_0075_01CAE21A.F659D570 Content-Type: text/plain; charset="UTF-8&qu...

Using IF formulas on multiple columns
I have a table with Multiple columns and rows like this: Transaction Month1 Month2 Month3 etc.. Location Sales 500 550 340 Glasgow Sales 780 825 575 Edinburgh Sales 260 345 210 Inverness Expenses 500 550 340 Glasgow Expenses 780 825 575 Edinburgh Expenses 260 345 210 Inverness In another excel sheet I need to have an IF formula which says IF(B3:B50="Sales",IF(F3:F50="Glasgow,C3:C50,0)) so that when the value in column B = Sales and ...

use both Office 2004 and Office X
I have just bought office 2004. do I need to uninstall office X? Can I = use both=20 versions without problems? I would like to start progressively ... thanks Loris On 9/7/04 4:51 AM, in article 069f01c494b7$d3d7fc90$7d02280a@phx.gbl, "Loris" <loris.biaggio@bluewin.ch> wrote: > I have just bought office 2004. do I need to uninstall office X? Can I use > both > versions without problems? > I would like to start progressively ... > > thanks > Loris Unless you have a very specific need for both versions of office on the same machine (like development back...

Chart Series using non-contiguous calls in VBA
I'm trying to create charts in Excel from a bunch of non-contiguous cells using VBA. The cells are Sheet1!$C1:C2,Sheet1!$C4:C5,Sheet1!$C6:C7,Sheet1!$C9:C10, etc. I first tried to set Series.Formula with a string that I generated from the sheet & cell names, but I had enough cells that I hit that 250 character limit in the Series.Formula arguments. I tried a number of other tactics, including: Trying SeriesCollection.Extend (didn't work, since I'm trying to put data from >1 sheet into the chart) Trying to just set Series.Values & Series.XValues separately ...

KB 904942 Fix Fails if Outlook is NOT using an Exchange Server
Problem - KB 904942 When you use Microsoft Outlook or Microsoft Outlook Express to try to log on to a HTTP-based mail server, such as Hotmail Plus, authentication fails. This problem occurs when the following conditions are true: =B7 The client computer that you use to try to log on to log on is running Microsoft Internet Explorer version 7.0. This KB provides a download patch to fix the authentication problem. Customer Comment I have two Outlook Email Profiles on my home PC. One is an Exchange Server profile for work email. It includes the work email account via the exchange server and ...

What view shall i use?
hi, im creating a virtual lab program and i dont know what choose if im using the doc/view architecture (since i need to insert a tab control and more)... i thought about the plaib cvuiew but there is a problem using it just as it is... what do you think i should do? Hi, "fima" wrote: > hi, > im creating a virtual lab program and i dont know what choose if im using > the doc/view architecture (since i need to insert a tab control and more)... What does it mean "... and more)....". If you mean controls such as buttons, edit boxes, ..., you would rather ...

Can i turn a cell into a listbox and refer to the list value using the cell's reference
I would like to turn a cell ie (B2) into a listbox , the listbox will contains a range of cells ie (A2:A20) from another sheet in the same file. I would then like to be able to refer to B2 in VB or if statements i.e. =if(B2 = "Wheat", 0,1) I played around with "Create List" by right clicking a cell, but i didnt seem to do what i wanted. TIA Doug Hi Douglas maybe data / validation - list, with A2:A20 as the source which gives a drop down arrow in the cell would give you what you need? Cheers JulieD "Douglas" <dougsdir24@yahoo.com> wrote in message ...

SOP Req Ship Date and it's use
Penny, I am looking for assistance on the Req Ship Date on Transactions > Sales > Sales Transaction Entry. I have been looking into this field for better understanding of its impact and I am hard press to find any. Ultimately we would like this field to be the driving factor when fulfilling the order taking into account the lead time of the items themselves. Currently I am under the impression once the order is complete the items are placed into back order and the Purchase Order Generator report tells our warehouse staff what to order. This is a serious issue with us because if...

Using NOT operator with Advanced Find
We can group ROW in Advanced Find with AND or OR operator. How can I use NOT operator? For example: I need to find LEADs which have activity LETTER with some conditions AND NOT have activity PHONE CALL with some conditions. This is a shortcoming of the current advanced find. There isn't a NOT operator, so the query you're trying to build is not possible. The best you can do is creating a report for it. -- Michael H�hne, Microsoft Dynamics CRM MVP Web: http://www.stunnware.com/crm2 Feed: http://www.stunnware.com/crm2/atom.aspx Custom Lookup Dialog: http://www.stunnware.com/crm2...

The use of SOURCE in categories
I use the "source" field, which can be setup under "categories". This field is then editable in cash transactions. However, when dividend payments from an investment account are recorded, which are posted to a cash account, the "source" field is not editable anymore. There seems to be no way of accessing this field for investment transactions. Any work-arounds to access this field? Thanks, Michael Please keep it to one thread. "Mike" <phx9@msn.com> wrote in message news:a8fefb94.0409062109.200c3727@posting.google.com... >I use the "sou...

Using taRMApply via Webservices?
Is it possible to extend Webservices (GP 9) to apply payments via Econnect's taRMApply? I have tried modifying Microsoft.Dynamics.GP.CashReceiptCreate.xslt, however Econnect seems to be ignoring the taRMApply object. -Tony It would be easiest to just create your own webservice that wraps the taRMApply transaction in eConnect. The only downside is that your webserive will obviously not fall under and of the defined policies within GP Web Services. HTH Scott "Tony" wrote: > Is it possible to extend Webservices (GP 9) to apply payments via Econnect's > taRM...

Adding CommandBars to an Inspector while using Outlook as the editor
I am having am problem doing this. I am only having this problem when the email I am reading is of type rtf. When the email is of type plain text or html, I don't have an issue. The commandbar is created with no problem. I am using this line of code: Inspector.CommandBars.Add('ADXHT_003',msoBarTop,EmptyParam,True); From doing some research, I understand that adding commandbars while using Word as your editor is not possible in Outlook 2000. However, I am testing on Outlook 2002 & 2003. Any help would be greatly appreciated. What kind of problem exactly are you having? Dm...

Error in using Whoami
Hi We are trying to write a simple program using sdk. We get 405 method not allowed error while using Microsoft.CRM.Proxy.CUserAuth userAuth = bizUser.WhoAmI(); Our setup Win 2k Server, sp3, MSCRM, .net framework 1.0 VS.net with .net framework 1.1 Could not find samples written in VC++.net why? Lakshmi p.s: remove nospam in email id to send email. have you tried going to that virtual directory, and removing anonymous from the directory security? "Lakshmi" <lakshminiku@sifynospam.com> wrote in message news:0be901c393d0$5d18c190$a401280a@phx.gbl... > Hi > > W...

Import data to crm 4.0 using crm 3.0 .bak files
I have sql backup files (*.bak) from a crm 3.0 installation. I no longer have 3.0 installed. I have a new, blank, 4.0 installation and wish to import the .bak records into the 4.0 installation. Can this be done without totally recreating the 3.0 installation? Unfortunately no, you will have to re-create the 3.0 installation and perform an upgrade to 4.0. Dave Ireland <brianburks@yahoo.com> wrote in message news:e8cccfdc-e30e-4bff-bcbe-8b8a062c4d87@v42g2000yqv.googlegroups.com... >I have sql backup files (*.bak) from a crm 3.0 installation. I no > longer have 3.0 installed...

Print Screen using dual screens?
I have recently set up to use dual screens.....this is great for productivity! If I now do a PrtScn, however, I get the screen shot of both monitors. Is there any clever way of achieving the screen shot only of one, or the other screen? Al Al wrote: > I have recently set up to use dual screens.....this is great for productivity! > > If I now do a PrtScn, however, I get the screen shot of both monitors. Is > there any clever way of achieving the screen shot only of one, or the other > screen? > > Al By design, as it has always been with ...