Need help understanding passing pointers between threads.

Need help understanding passing pointers between threads.
The code below works but if I uncomment the ASSERT_VALID() line,
the assertion fails with a warning about passing pointers between
threads.
I only vaguely understand this.
How do I do it right?
(The code is simplified for this post)

TIA,

Harvey


class CXThread
{
public:
	CXThread();

// Attributes
protected:
	class CMyDoc *pDoc;
	void FillXInfo();
	static UINT ThreadFuncX(LPVOID);
// Etc.
};


void CXThread::StartXThread(CMyDoc *pdoc)
{
	pDoc = pdoc; // @@ is this supposed to be dangerous?
	pThread = AfxBeginThread(ThreadFuncX,this,THREAD_PRIORITY_IDLE);
}

UINT CXThread::ThreadFuncX(LPVOID pT)  // static thread function
{
	((CXThread*) pT)->FillXInfo();
	return 0;
}

void CXThread::FillXInfo()
{
//	ASSERT_VALID(pDoc); // @@ Fails because we are in a thread
	ASSERT(&pDoc->m_tX == this); // basic safety check since
ASSERT_VALID() fails
	// The pDoc should be correct if it is pointing to this thread class
instance correctly

	// More code (removed for simplicity)
}

0
harveyab (125)
4/9/2006 8:22:12 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
465 Views

Similar Articles

[PageSpeed] 30

Harvey wrote:

> Need help understanding passing pointers between threads.
> The code below works but if I uncomment the ASSERT_VALID() line,
> the assertion fails with a warning about passing pointers between
> threads.
> I only vaguely understand this.
> How do I do it right?
> (The code is simplified for this post)
> 
> TIA,
> 
> Harvey
> 
> 
> class CXThread
> {
> public:
> 	CXThread();
> 
> // Attributes
> protected:
> 	class CMyDoc *pDoc;
> 	void FillXInfo();
> 	static UINT ThreadFuncX(LPVOID);
> // Etc.
> };
> 
> 
> void CXThread::StartXThread(CMyDoc *pdoc)
> {
> 	pDoc = pdoc; // @@ is this supposed to be dangerous?
> 	pThread = AfxBeginThread(ThreadFuncX,this,THREAD_PRIORITY_IDLE);
> }
> 
> UINT CXThread::ThreadFuncX(LPVOID pT)  // static thread function
> {
> 	((CXThread*) pT)->FillXInfo();
> 	return 0;
> }
> 
> void CXThread::FillXInfo()
> {
> //	ASSERT_VALID(pDoc); // @@ Fails because we are in a thread
> 	ASSERT(&pDoc->m_tX == this); // basic safety check since
> ASSERT_VALID() fails
> 	// The pDoc should be correct if it is pointing to this thread class
> instance correctly
> 
> 	// More code (removed for simplicity)
> }
> 

Harvey:

It is not the passing of pointers that is dangerous, but rather the 
using of MFC objects in a different thread from the one they were 
created in. You can use your pDoc pointer to access public member 
variables of your CMyDoc class, or to call any CMyDoc method you have 
written that does not use MFC in any way.

There is no problem with the pointer itself; all the threads in a given 
process use the same address space (and the same heap).

HTH,

David Wilkinson

================
0
no-reply8010 (1791)
4/9/2006 10:46:38 AM
David,
Thank you for your response.
So below is the beginning of the FillXInfo() function (simplified).
Are these things dangerous?
I guess I'm not sure how to know if I am "using MFC".
Is there a better approach to implementing a worker thread?
TIA, Harvey

void CXThread::FillXInfo()
{
//	ASSERT_VALID(pDoc); //@@ Fails because we are in a thread
	ASSERT(&pDoc->m_tX == this); // basic safety check
	CListCtrl &rLC0 = pDoc->m_pLV->GetListCtrl();
	int nI0 = rLC0.GetItemCount();
....

0
harveyab (125)
4/10/2006 4:35:57 AM
Harvey wrote:
> David,
> Thank you for your response.
> So below is the beginning of the FillXInfo() function (simplified).
> Are these things dangerous?
> I guess I'm not sure how to know if I am "using MFC".
> Is there a better approach to implementing a worker thread?
> TIA, Harvey
> 
> void CXThread::FillXInfo()
> {
> //	ASSERT_VALID(pDoc); //@@ Fails because we are in a thread
> 	ASSERT(&pDoc->m_tX == this); // basic safety check
> 	CListCtrl &rLC0 = pDoc->m_pLV->GetListCtrl();
> 	int nI0 = rLC0.GetItemCount();
> ...

That's exactly what you should not do from a secondary thread.  The 
problem is not pointers, the problem is accessing a window created in 
another thread (m_pLV is a CWnd).  You can access your own data members, 
but not MFC CWnd functions.  When you attempt this the calling thread is 
   blocked, the operation is attempted in the main thread, and if the 
main thread is also blocked then you have a deadlock.

Pass data to your thread.  PostMessage from your thread.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
4/10/2006 12:48:13 PM
Harvey wrote:

> David,
> Thank you for your response.
> So below is the beginning of the FillXInfo() function (simplified).
> Are these things dangerous?
> I guess I'm not sure how to know if I am "using MFC".
> Is there a better approach to implementing a worker thread?
> TIA, Harvey
> 
> void CXThread::FillXInfo()
> {
> //	ASSERT_VALID(pDoc); //@@ Fails because we are in a thread
> 	ASSERT(&pDoc->m_tX == this); // basic safety check
> 	CListCtrl &rLC0 = pDoc->m_pLV->GetListCtrl();
> 	int nI0 = rLC0.GetItemCount();
> ...
> 

Harvey:

ASSERT_VALID is an MFC macro.
CListView::GetListCtrl() is an MFC method
CListCtrl::GetItemCount() is an MFC method

pDoc->m_tX is just C++, so OK
pDoc->m_pLV is just C++, so OK

David Wilkinson



0
no-reply8010 (1791)
4/10/2006 1:11:39 PM
David and Scott,

Thank you much for your response. I think I am starting to understand
what I have done that needs to be changed. It will be a while before I
get my code sorted out.

Harvey

0
harveyab (125)
4/12/2006 5:15:04 AM
Reply:

Similar Artilces:

Help getting SheetName into a cell
I need to refer to the sheet name in a formula and can't figure out how to do it. I can't find a Function which will do this. I did discover ActiveSheet.Name but I have been unsuccessful in getting it work in a function. Specifically I have sheets named "1.a" , "1.b" , "1.c" . . . "99.a" , "99.b" , "99.c" and need to get these names into cell C3 of each sheet. The sheets may not be in order and there may be missing sheets. Thanks in advance for any help. Omar Hi You could put somthing like this in a macro or attatch ...

Help with SQL Query 06-30-10
We have a distinct list of email addresses in alpha order and we need to transform it from a single column into a grid of three columns maintaining the alpha order. The list is contained in a temp table inside of our query. We then use the temp table list and perform case statement with a mod on the row_number in a select statement to columnze the data. However, the columnar data contains a null value in two of the three columns and we are needing to remove the nulls and have the actual values on each row in the output. Here is our current sql: CREATE TABLE #tmpTable ( Email_Add...

Messages not delivered to another Routing Group HELP NEEDED
I have three routing groups ( exchange in the 3 of them are Exch 2k sp3 ). Mail from one server ( in my headquarter routing group ) to another ( in one branch office routing group ) is getting stucked in MESSAGES WITH UNREACHABLE DESTINATION and not routed to that server. Connectors seems to be working just fine. After add my other exchange server from my headqurter group to the connector to be albe to send mail it shows the queue now in the connector but does not sends anything. Otherwise, mail from my branch office to my headquarter correctly sends mail. Any help would be gre...

Need remote Clients to have open SMTP relay with windows authentication in Exchange 2003
Hi all ... first time posting here, but i have been able to find many answers to other questions in the past ... Unfortunatley I may be in a bit over my head with this one. I have about 100 remote users and until now they have not been required to send email over SMTP to domains out side of ours. Now we have a directive from above to allow relaying from dialup and remote users but need to setup SMTP authentication. Current config ... Server: Windows 2003 server Exchange 2003 server SP1 Firewall permitting any traffic on ports 110 and 25 Security options for SMTP in the System Manager is se...

Need help with Combo Box?
I would appreciate any help with this. I currently have a form with two combo boxes and a subform. The first combo box lists counties and the second box lists doctors in selected county. After selecting county, doc the subform lists pts for this doc. All this works fine. However, I need to add a couple of more filters. I am stuck and would like to know how to do this. I don't want to mess up what I already have. How can I incorporate a couple more filters? I thought maybe adding an option box to the form????? Can someone please help me to accomplish this? Thank you. Sure...

I have Korean Office...I need English (US) Office
LIPs don't seem to provide me with a solution...apparently there is no English LIP!?!?!?!?!? Seriously? That's crazy! LIP = Language Instruction Pack?? Why NO English? Where is Windows / Office developed? America, right? I'm in Korea. I bought a Korean computer. It came with Korean Windows. I need English menus and whatnot on my MS Office applications. In news:7430603D-8534-4235-A16D-D073B471EF85@microsoft.com, abundantmind <abundantmind@discussions.microsoft.com> typed: > LIPs don't seem to provide me with a solution...apparently > there is...

Do I need DSClient to run Exchange 5.5 on an NT4.0 box in Windows 2003 Native Mode ADS?
Good Evening, I am in the process of migrating my WinNT4.0 domain and Exchange 5.5 Org to Windows 2003 ADS/Exchange 2003. I know best practice is to change the domain to Native mode, but how does this affect my NT4.0 server running 5.5? Do I need to simply load the DSClient onto the server? I should also note that we did an inplace upgrade from NT 4.0. This is a single domain environment. As long as you no longer have NT 4.0 BDCs you should be able to move to native mode. Your NT 4.0 server running E55 will not be affected (unless of course it is also a BDC). It is a good idea to...

Pass CString to printf?
Hi, Is it safe to pass a CString to printf without explicit (LPCTSTR) cast? The following MSDN article suggests it is NOT safe: http://msdn.microsoft.com/en-us/library/awkwbzyc(VS.71).aspx "...it is essential that you use an explicit type cast when passing a CString object to a function that takes a variable number of arguments. .... CString kindOfFruit = "bananas"; int howmany = 25; printf( "You have %d %s\n", howmany, (LPCTSTR)kindOfFruit );" On the other hand, various newsgroup posts claim that CString was designed specifically in such a way that the...

Need RULE to search for Text in Body with varying # of spaces
Hi there, I have Outlook 2002 and have been using the outlook rule condition: "With specific words in the body" However the "Specific words" I'm looking for might contain varying number of spaces: EX: "some text: some more text" "some text: some more text" Is there a wildcard or something to match multiple spaces? --Thanks very much, Kenneth Kenneth Parker <kenneth.parker@sas.com> wrote: > I have Outlook 2002 and have been using the outlook rule condition: > "With specific words in the body" > > However...

pass parameters back from C/C++ to JScript
Hi, all I import Microsoft Webbrowser Control to my VC6 MFC project and customize the web browser with my own doc handler. The following is the implemenation: ICustomDoc * pCustdoc; m_WebBrowser.GetDocument()->QueryInterface(&pCustdoc); // m_WebBrowser is an instance of Microsoft Webbrowser Control if (pCustdoc) { pCustdoc->SetUIHandler(m_pDocHandler); // m_pDocHandler is my doc handler for the web browser. pCustdoc->Release(); } Then I can call C/C++ functions from web page JScript, such as: external.MyFunc("abcdef"). In this way, I can pass parameters fr...

Help making BINGO cards
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel I am quite the novice at using Excel. I'm trying to create a set of BINGO cards for a church group and I'm having trouble figuring it out. I saw a post about something called RAND function but I don't know what that means. <br><br>What I need is 56 unique cards with 5 columns and 5 rows each. The cards will be using the numbers 1-24 leaving the center square blank or &quot;0&quot;. <br><br>Can anyone please assist? This is a multi-part message in MIME format. ----...

Calculating Averages
Hi, I am using the newest version of Excel on Windows 7, and I need some help with a complex calculation. In cells A1-A100, I have dollar amounts ranging from $1-$1000. In cells B1-B100, I have percentages ranging from 0%-100%. I want to break out the cells in A1-A100 in several groups, like follows: $1-$99, $100-$199, $200-$299, and so on up to $1000. Then, I need to calculate the average percentage for jobs in those categories. So, for the category of $1-$99, lets say there are two cells with amounts in that range, A1 and A2. Their percentages in B1 and B2 are 40% and 6...

REALLY NEED HELP
Hi guys, i'm hoping someone could lend me a hand. I'm setting up an excel file to input our fees received from our customers sent to collections. I have a main page with all the customers names, and each name is a hyper link to that customers separate worksheet - where a running tab is kept. I have a button called "update" and i've assigned the following macro (also called UPDATE) move the info over to the respective customers worksheet. I'm trying to get a loop going. A# is the customers name (first name will be starting at A4. B# to F# (first transaction...

Workbook there but not visible! Help!
I was working on some VBA code for an excel application I am writing. I switched between the VBE and the spreadsheet and all of a sudden my workbook is no longer visible! In the VBE project explorer window it shows my project and my code but in the excel window...no workbook! Nothing! Ran a test procedure calling IsAddin to see if somehow I accidentally clicked a button/box telling Excel to make this file an addin but it returned false. I did close the file and re-open it. Any ideas? Maybe it's just off the visible screen: Window|Arrange|tiled (and resize manually) or maybe y...

Do I need Publisher installed to read/edit articles in an e-mail?
I will be receiving e-mails with attachments containing Publisher2002 created articles. Do I need to have Publisher 2002 or later editions in order to read and edit these e-mail articles? On Mon, 7 Nov 2005 22:20:03 +0000, quilter512 wrote (in article <E5DAA762-0ABA-49C2-8DDB-9A0D05162496@microsoft.com>): > I will be receiving e-mails with attachments containing Publisher2002 created > articles. Do I need to have Publisher 2002 or later editions in order to > read and edit these e-mail articles? You can answer your own question by STFW. You not heard of Google Grou...

Please help Password Trouble
I am using outlook 2000 on a DSL line on XP Home. I have chosen to not save my password, and the problem that I am having is that everytime I go to check my mail it makes me type in my password. There must be a way (like in express) that allows you to just type in your password the first time you log into outlook and keeps you logged in until you decide to log out?? Thanks for your help!!!! -- Virtualliance, Inc. Mark Needham 7 Kimball Lane Bldg A Lynnfield, Ma 01940 T 001-781-224-4700 F 001-781-224-2414 C 001-617-799-4597 www.virtuallianceinc.com mneedham@virtuallianceinc.com im: vaincmar...

Please help....
I have a question regarding bank reconciliations that I am hoping someone can help me with. It concerns a USD bank account that I use. At the end of the month I prepare the bank reconciliation in GP. After completing the reconciliation I get a print-out called the "Reconciliation Posting Journal". This print-out provides me with the folowing: Bank Statement Balance Oustanding Cheques(-) Depoits in Transit(+) =Adjusted Bank Balance All of these amounts are in USD. Then I go to "Financial - Inquiry - Summary" and pull the summary balance for this GL ...

Help! Lost all email from inbox
Hi, hoping someone can advise me. Somehow my fingers fumbled when reaching for mouse to open an email and every email in my inbox disappeared. I have outlook 2000 and do have it set to automatically delete when i highlight and press delete key. But in this case, i didnt highlight anything and must have hit some combination of keys and poof - over 100 emails from inbox disappeared. They are not in deleted items folder and many were not even opened yet. tia kate <kate@discussions.microsoft.com> wrote: > Hi, hoping someone can advise me. Somehow my fingers fumbled when >...

Help! New to Publisher
What is the A...at the bottom of a page inside a small box? I am typing a newsletter. Shouldn't one page flow to the next like in Microsoft Office? If it is text overflow, I cannot retrieve. What am I doing wrong? Please advise. That indicates that there is more text inside that area that cannot be seen. If you stretch that text box down, you'll see the rest of your text. To make it flow from one box to another, you need to set it up for that. (I will admit that I break mine manually because I've never taken the time to learn how to do it correctly.) -- The problem with ...

Outlook 2002 backup question...please help!
Hi all, I am having a problem with windows which may require me to reformat. I cannot get into outlook (2002) via windows, but I can access files from dos. Can someone please tell me where the data files for outlook 2002 are stored, and which ones I'd need to copy? Thanks in advance for any help. Jim See if this info helps: http://www.howto-outlook.com/howto/backupandrestore.htm "Jim" <lakerfan426@yahoo.com> wrote in message news:OUhIH0oTGHA.4132@TK2MSFTNGP11.phx.gbl... > Hi all, > > I am having a problem with windows which may require me to reformat. I >...

Need Help Using A Custom Session Manager
Hi. I have some questions about session management. I have decided to use a custom session manager class to have more control over session state in my web site. I have started out by using Stephan Prodan's Session Class here: http://stefanprodan.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3DC%2523 You'll need to take a look (which I appreciate immensely) to get a sense of what I am talking about. 1) My first question pertains to how I persist and access my session information after a user authenticates (or doesn't). In his exam...

Help: MAPI can't find PSTPRX.DLL
Hi there Can anyone suggest how I can make outlook 2002 work properly. Everytime I press send/receive it says MAPI can not locate PSTPRX.DLL. I have done search and it is not on my computer. Please can anyone assist? just trying out 1st time >-----Original Message----- >Hi there > >Can anyone suggest how I can make outlook 2002 work >properly. Everytime I press send/receive it says MAPI can >not locate PSTPRX.DLL. I have done search and it is not >on my computer. Please can anyone assist? >. > See if this info helps: http://support.microsoft.com/default.asp...

Need Codec for Movies
I cannot get the Divx codec to work. Can anyone suggest another? Try this: 'Shark007.net - Windows Vista Codec Package' (http://shark007.net/vistacodecpackage.html) -- whs whs;1247892 Wrote: > Try this: 'Shark007.net - Windows Vista Codec Package' > (http://shark007.net/vistacodecpackage.html) +1 on that. -- MilesAhead "I don't want to belong to any club that would have me as a member." - Groucho Marx :: :: Thank you! "whs" <guest@unknown-email.com> wrote in message news:04397129b9710eebaeb1e11762768...

Need to find the table
I do get some odd requests for great plains. My latest requires me to track down a table. The one i need is for one of the forms that shows in the smart list. I need the one under account transactions and is called Output VAT Nominal. I need to know what table/s supply this form. Even better would be if it was possible to access the hard coding of this form. I looked for it and found nothing. We run great plains version 8 should it help. well, thankyou all kindly in advance. Tools>Resource Descriptions is always a good place to start. "Saltious" wrote: > I do get som...

I need to do a slide show on information technology careers
where is a good place where I can find pictures as well as information for a slide show You can find lots of photographs in the Clip Art section of http://office.microsoft.com -- Echo [MS PPT MVP] http://www.echosvoice.com What's new in PPT 2007? http://www.echosvoice.com/2007.htm Fixing PowerPoint Annoyances http://tinyurl.com/36grcd PowerPoint 2007 Complete Makeover Kit http://tinyurl.com/32a7nx "c hill" <c hill@discussions.microsoft.com> wrote in message news:358D5DB0-5B91-447A-B0F3-F2C81203D091@microsoft.com... > where is a good place where I ca...