Locking Resources

hi, may be this is not the right place to ask this question.
In my MFC application i need to lock a resource, i dont now what kind of 
mechanism to use exactly in microsoft environment .

Let the resource be a STL vector. this is a shared vector in a multithreaded 
application.

i need somehting like;
<code>

mutex.lock()
  // vector operations ..
mutex.unlock()

</code>

each thread thats wants to access this vector must sleep if resource is 
locked.

Best Regards...


0
oakin (17)
4/24/2005 8:46:42 PM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
591 Views

Similar Articles

[PageSpeed] 20

>each thread thats wants to access this vector must sleep if resource is 
>locked.

Use a critical section object - if you're using MFC, there's
CCriticalSection & CSingleLock classes you can use.

Dave
-- 
MVP VC++ FAQ: http://www.mvps.org/vcfaq
0
davidl7375 (2060)
4/24/2005 10:55:40 PM
Coder wrote:
> hi, may be this is not the right place to ask this question.
> In my MFC application i need to lock a resource, i dont now what kind of 
> mechanism to use exactly in microsoft environment .
> 
> Let the resource be a STL vector. this is a shared vector in a multithreaded 
> application.
> 
> i need somehting like;
> <code>
> 
> mutex.lock()
>   // vector operations ..
> mutex.unlock()
> 
> </code>
> 
> each thread thats wants to access this vector must sleep if resource is 
> locked.
> 
> Best Regards...
> 
> 

MFC provides CCriticalSection for this, but its design and documentation 
are obscure.  I suggest the simpler Win32 CRITICAL_SECTION object. 
Initialize the object with InitializeCriticalSection, then do this in 
each thread that accesses the shared data...

EnterCriticalSection(&cs);
    // vector operations ..
LeaveCriticalSection(&cs);

-- 
Scott McPhillips [VC++ MVP]

0
Scott
4/24/2005 11:55:34 PM
On Sun, 24 Apr 2005 18:55:34 -0500, "Scott McPhillips [MVP]"
<org-dot-mvps-at-scottmcp> wrote:

>MFC provides CCriticalSection for this, but its design and documentation 
>are obscure.  I suggest the simpler Win32 CRITICAL_SECTION object. 
>Initialize the object with InitializeCriticalSection, then do this in 
>each thread that accesses the shared data...
>
>EnterCriticalSection(&cs);
>    // vector operations ..
>LeaveCriticalSection(&cs);
>
>-- 
>Scott McPhillips [VC++ MVP]

Hear, hear. Unfortunately the MFC thread synchronization classes have
been under-documented for years, it would take about a day for a MSFT
to fix, however, it stays unfixed in release after release after
release. Unless you're already familiar with the classes, or want to
learn through experiment (as many of us have), avoid them altogether
and use win32. If you would prefer a different abstraction you could
look at Boost threads (http://www.boost.org/doc/html/threads.html).
0
code4u (9)
4/25/2005 1:29:09 AM

0
oakin (17)
4/25/2005 6:01:10 PM
Reply:

Similar Artilces:

Views not able to read resource.h
Hi, I've got a project (VC++ 6.0) with several views and dialogs. Tey were created with the dialog editor just as it should be, got an ID in resource.h (like IDD_DLGOBJEKTE), got a class attached to them; everything seemed fine. Now, after some months in coding, the views started not to be able to read resource.h as it seems. There is a line in every of the view-classes: "enum { IDD = IDD_ANYNAME };". The IDD_ANYNAME variable is corretly defined in resource.h. Nevertheless the compiler seems not to be able to resolve the constant's name to its value. When I manually do so (...

locking up
This might be more of a GDI question, but since I'm in MFC, I'll ask: Would BitBlt-ing to the screen repeatedly tend to lock up a program, after alt+tabbing to another application? If so, why? This seems to be happening to me, and I want to get to the bottom of the problem. What I have is either a fade loop or a screen-size redraw, both based on a timer and bitblt. Quite often, after I leave the program to go to another app, I cannot return and must "end task". "Steve Russell" <srussell@removethisinnernet.net> wrote in message news:%23YFL5Ks%23F...

shareing resources within a project
I build 80+ homes a year, and need to manage it as one project-i think??? the thing is, i have for example 3 Plumbing contractors to do the rough-in for 15 homes, i can only give them 5 homes each and set Priorities for the homes. so oboviously home #2 can start until #1 is completed. and the same goes for plumbing finishes which have to be linked wt the plumber who did the rough-in. All of plumber's #1 tasks need to be linked not only for each unit but across all houses that plumber starts for me. for example plumber #1 may not be able to do the rough-in on 3 Apple St. becau...

Calendar resource question.
I have a delegate set up to automatically manage appointments for multiple conference room calendars. The delegate is running Outlook 2003 and my users are running every version of Outlook from 98 - 2003. We're running an Exchange 5.5 system. My users can send an appointment to a conference room and it's accepted. Then if they try to update the appointment (without changing date or time) they get a declined message saying the appointment conflicts with another appointment and their appointment is removed from the calendar. If they update again the appointment is accepted...

Desktop Alert Locks Computer
Greetings, I am seeing a little quirk on some of my desktops. It only happens occasionally, but it happens to different models of PC. Not all the time, but once in awhile, I have users that complain of their PC freezing. The only remedy is to do a hard reboot. I have noticed in these instances, that the desktop mail alert ( the preview that pops up by the system clock when you get a new mail) is showing when it froze. If I go into Outlook and turn off the desktop alert, then the PC freezing stops. So, I know that the Desktop Alert is the cause of the problem. Has anyone else ever seen this? Th...

Lock formula to cell
I want a formula to lock to a cell so that when I add or delete a column the formula will always remain with say column H I know if the sheet is Password protected this will prevent this happening but that is no use when I have to unprotect the sheet to delete a column. Anyone got thoughts on this? regards Pat Hi Pat without using VBA you can't achieve this Frank Pat wrote: > I want a formula to lock to a cell so that when I add or delete a > column the formula will always remain with say column H > > I know if the sheet is Password protected this will prevent this >...

Cannot Use 2007 Office Resource Kit Technical Library in Compiled Help format
I downloaded the 2007 Office Resource Kit Technical Library in Compiled Help format, double clicked on it, cleared the Always ask before opening this file check box and clicked Open. All I got was a window with a little icon in the top left. How do I get this to display properly? Thanks for reporting. It appears something is wrong with the latest published version (version 5 published on 6/12/2009). I've passed it on to the Office Resource Kit writer team at Microsoft. In the mean time, you can still access the content on-line via; http://go.microsoft.com/fwlink/?LinkId=127122 The c...

Locked out of database
Not sure what happened but get a message saying "database has been converted from a prior version of Access by using DAO compactdatabase and is now in a partially converted state." It says I should be able to import tables into a new data base but when I try this it just gives the same message about converting and the data base just doesn't open. Please help because lots at stake, have to do a report based on these tables so am waiting with anticipation!!!x Hi Pete, Usually this means you are opening an earlier version of Access in a new version. Make a copy of the data...

locked excel File ??
Hi . I am working with my colleges with one Excel file in a shared file system. Sometime - when I open the file, there is the message "locked from College" - but he is definetly out of office and shut down his PC. We are using Excel 2003 + Excel 2007. What can I do?? THnks for help Susan ...

Outllook2007 to open my Outlook2002 Win7 says files locked.
I'm following 2002 to 2007 instructions step by step. When I point Outlook to my ..pst file, I get access restriction messages. How do I make sure these files can be accessed by Outlook 2007? Thanks. Which exact steps are these? From your Outlook 2002 machine, copy the pst-file to your Outlook 2007 machine and place it in a convenient location such as My Documents\Outlook\ In Outlook 2007 you can then reconnect to the pst-file via; File-> Open-> Outlook Data File... For details see; http://www.howto-outlook.com/howto/backupandrestore.htm -- Robert Sparnaai...

excel locks up after selecting a cell #2
excel locks up after selecting a cell. When ever, I select a Cell, that will automatically selects all the cell and this freezes the entire computer. Can any body who would help me resolve this issue? Please help.... ...

Lock autoshape to fixed position
Hello, Does anybody know if it is possible to keep an autoshape to a fixed position. I have a worksheet that has two panes, one is frozen i.e. a fixed header and scrollable rows. On the bottom of the page I want to put a button bar made of shape buttons. The problem is that when I scroll, the buttons scroll too! It would be nices if I can keep them in position. I know that is is possible to add a second window, but that does not look so well... Any ideas? Multiple fixed panes would be a great option! Change the alignment option to be absolute. Picture properties > Layout tab > Advan...

Money "lock-out" solution-thanks Kurt
Kurt Thanks for the idea. It worked. I unistalled money 2002 and the reinstalled using custom mode...up and running again! Thank -you What happens to all your data when you do that? Do you still have your data? I tried reinstalling, but no luck. I hate to uninstall, since I would lose all the data, right? Would appreciate info. Thanks, Alice >-----Original Message----- >Kurt > >Thanks for the idea. It worked. I unistalled money 2002 >and the reinstalled using custom mode...up and running >again! Thank -you >. > Your data is in a file *.mny. This is ind...

Help finding data using Tools -> Resource Descriptions
We have just recently implemented the Horizon's Manufacturing System at work, and noticed a problem with some of our costs. I would like to set a specific field for ALL BOM items to have "Add standard cost to parent" checked. I have had a look at the Horizon's Manufacturing System SDK, and I cannot see this particular field anywhere. The Finished Good Defaults window shows about a page worth of fields, but there is no data for this particular field. I also had a look at the raw data in table BOM50106 (where the rest of the F.G. Defaults are located), but none of these fields ...

XP Media Center- Windows Resource Kit Location?
Hello, I am trying to locate the QoS Tools and Settings, but it seems I am only able to access this by using the Windows Resource Kit? I did find Internet Information Services, however it states: "MMC could not create the snap-in" The Microsoft Management Console 3.0 version 5.1 (build 2600.xpsp_sp3_gdr.090804-1435: Service Pack 3) ...

Form locking up
I've completed my own Linked Table Manager wizard, so that the connections I create to SQL Server and Oracle database on my network don't require a DSN (DSN-less connections). This wizard exists in its own mda file, which I have added as a reference in several of my databases. In my test database, this wizard runs great. I've got a command button that calls a function (fnRefreshLinks) that exists in the wizard. It opens a form, refreshes a list of the linked tables, tests the link, and provides me with some options. When I run this same function from another database, it...

Where am I being locked out?!
This is an unusual problem and I'm hoping someone will have come across something similar before. Before I start I should state that I've hardly used Windows Server or Active Directory, so go easy on me if this is something obvious! I am having my account locked out on a daily basis. I know that this isn't to do with my machine as it happens even if I get my account unlocked and wait a day without turning my machine on I still can't log on the day after. I'm pretty sure it's a piece of software that I've installed somewhere that keeps trying to con...

Resource availability across servers?
An interesting discussion came up at work yesterday. There MAY be a need for us to physically separate our military projects from our commercial ones. If we end up having to do something like this, would it still be possible to see resource availability across all projects? We have resources who work on both commercial and military projects. If these projects reside on completely separate physical servers, could we still take full advantage of the enterprise capabilities? Kevin: This can't be done OOB, but it certainly could be handled programmatically. -- Gary...

Why not use this as a lock object in lock
Hello! According to the docs it says that lock (this) is a problem if the instance can be accessed publicly. Can somebody explain what can happen if I have a lock section and use this as the lock object like this. lock(this) { } //Tony On 2010-06-20 17:09, Tony Johansson wrote: > According to the docs it says that lock (this) is a problem if the instance > can be accessed publicly. > Can somebody explain what can happen if I have a lock section and use this > as the lock object like this. > lock(this) > { > > } > http://haacked.com/ar...

how do i resolve the 'system resource exceeded' error?
I keep getting the "System Resource Exceeded" error message when running a very complex query. How do I get around this, or resolve it? Thanks, Marlene http://support.microsoft.com/default.aspx/kb/918814 The above link in for Access 2007 but some of the advice applies to all versions. Complex queries are often a sign of a badly normalized database. -- Jerry Whittle, Microsoft Access MVP Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder. "Marlene" wrote: > I keep getting the "System Resource Exceeded" error message when running a &g...

Resource Scheduling...
Hi folks, I am running exchange 2003 on SBS2003. I have had this problem on BOTH installs (we recently got a new box and I rebuilt everything from scratch) so I know its something I am doing. I am attempting to get our conference rooms to auto-accept appointments. I have logged in as those users, set them to auto accept appointments from outlook, and then given folks permissions. But they appointments don't auto accept. Do I need to do something additional - perhaps open the mailboxes with my account?? Please help, its an issue thats been driving me crazy forever. Hi, Did yo...

locks and more locks
Hello. This is about a SQL Server 2005 back end and Access 2007 front end. There is a front end on each user's machine. This has to do with the locking (or blocking as some call it) issue. Connections from the front end remain open once the user opens the front end until the front end closes, if the user closes the front end at all and not everyone closes the front end when done. There can be approximately 100 (+/-) people using the front end during the day. Some may actually be transmitting data (transactions) and some may not. But my thought is if the connection is a...

"Computer Locked" dialog does not show up
I have windows XP Pro (version 2002) SP3. When I lock my computer, I do not see the "Computer Locked . . . " dialog. Instead, I only see the "Unlock Computer" dialog immediately upon locking. I am concerned about not pressing Ctrl+Atl+Del before entering my password to unlock. Is there a setting that I can change (I have extensive knowledge of windows registry and system tools, but I have not been able to find an option to "Show only unlock screen". ...

Keyboard locked
Hello all My keyboard seems to be locked and I can't type anthing. I have a Dell Inspiron 1501(three yrs old). I pressed the fn key with the number lock key and that didn't work. Can someone please help. Thx Dolores Try replacing the keyboard and/or rebooting your computer. "dolores01" <u59578@uwe> wrote in message news:a6fd3466ee816@uwe... > Hello all > My keyboard seems to be locked and I can't type anthing. I have a Dell > Inspiron 1501(three yrs old). I pressed the fn key with the number lock > key > and that didn't work. Can...

Determine which mail is locking queue
Came in to work yesterday and exchange was not delivering mail... bummer. I determined the queue was all backed up. Message tracking showed (*from memory) that the messages were being submitted to the advanced queue but would continue no further. I the reboot the server, stop the smtp server when it's back up move and then move all messages in the queue folder to a new folder and start the smtp server. All is well. I stop smtp server, place some of the messages I moved back into the queue folder and start the server again. So on and so forth until messages stop being delivered aga...