| |

VerySource

 Forgot password?
 Register
Search
View: 1022|Reply: 5

Weakly ask a main thread and auxiliary thread synchronization problem

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-12-17 12:30:02
| Show all posts |Read mode
Send a message to the main thread in the auxiliary thread. After the main thread has processed the message, set a global time to a signal state. Use WaitForSingleObject (the event, INFINITE) in the Run() of the auxiliary thread to block and wait for the event to be set to a signal state. But I can't wait, why? Thank you Assert for appearing in CMapPtrToPrt()
code show as below:
The code is:
    CDrawThread::Run()
    {
        ::PostMessage(m_pOwner->GetSafeHwnd(),WM_GETDATA,0,0L);
        ::WaitForSingleObject(g_evDone,INFINITE);
        TRACE() //The TRACE has not been executed.
    }
    CWaveView::OnGetData() // WM_GETDATA
    {
        CDoc *pDoc = (CDoc*)GetDocument();
        pDoc->... // do sth
        g_evDone->SetEvent();
        TRACE() //The TRACE has been executed
    }

Debug output:
    ARRSRT will appear after executing the second TRACE
Reply

Use magic Report

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-12-18 17:15:01
| Show all posts
MFC objects are thread specifical, you need to do Detach/Attach, Attaching and Detaching Objects
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-12-19 16:00:01
| Show all posts
already have
CView* m_pOwner = NULL;
m_pOwner->FromHandle(hOwner);
No, do you mean this, senior?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-19 20:30:01
| Show all posts
This code seems to be fine! ! !
You processed the message successfully, but the event couldn't wait, and then you said that an Assert error occurred. . . . .
This shows that your message must have been unsuccessful. Problem with OnGetData
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-12-20 00:45:01
| Show all posts
Let me guess, it's purely a guess, ha ha, top popularity!

Is it g_evDone->SetEvent(); After that, the worker thread continues to run, requiring TRACE(), but, in OnGetData(), SetEvent(); After the execution is completed, the same requires TRACE(), will it be these two TRACE() conflicted?
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-12-20 07:45:01
| Show all posts
It's my fault. I didn't make it clear. The TRACE in OnGetData() has been executed. After this TRACE is executed, in theory, the TRACE after WaitForSingleObject() should be executed again. But no, the program debug jumps out of an assert. The call stack is as follows:
CMapPtrToPtr::GetValueAt(void * 0x002602de) line 186 + 9 bytes
CHandleMap::LookupPermanent(void * 0x002602de) line 90 + 19 bytes
CWnd::AssertValid() line 894 + 15 bytes
CView::AssertValid() line 495
CFormView::AssertValid() line 278
CEcmRecordView::AssertValid() line 62
AfxAssertValidObject(const CObject * 0x003d63a0 {CEcmRecordView}, const char
* 0x5f4d1700 THIS_FILE, int 939) line 108
CDocument::AssertValid() line 940
CEcmUIDoc::AssertValid() line 109
AfxAssertValidObject(const CObject * 0x003d5d88 {CEcmUIDoc}, const char * 0x
5f4d1744 THIS_FILE, int 425) line 108
CDocTemplate::AssertValid() line 426
CMultiDocTemplate::AssertValid() line 220
AfxAssertValidObject(const CObject * 0x003d4a38 {CMultiDocTemplate}, const c
har * 0x5f4d17e0 THIS_FILE, int 852) line 108
CDocManager::AssertValid() line 853
AfxAssertValidObject(const CObject * 0x003d4b98 {CDocManager}, const char *
0x5f4d1a38 THIS_FILE, int 551) line 108
CWinApp::AssertValid() line 552
AfxAssertValidObject(const CObject * 0x00424978 class CEcmUIApp theApp, cons
t char * 0x5f4d19e8 THIS_FILE, int 537) line 108
CWinThread::OnIdle(long 0) line 541
CWinApp::OnIdle(long 0) line 479
CWinThread::Run() line 479 + 30 bytes
CWinApp::Run() line 400
AfxWinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x0014
1efa, int 1) line 49 + 11 bytes
WinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00141ef
a, int 1) line 30
WinMainCRTStartup() line 330 + 54 bytes
KERNEL32! 7c816fd7()
The above is the prompt in the call stack, the following is the code of CMapPtrToPtr() and the place of error:
void* CMapPtrToPtr::GetValueAt(void* key) const
// find value (or return NULL - NULL values ​​not different as a result)
{
    if (m_pHashTable == NULL)
        return NULL;
    UINT nHash = HashKey(key)% m_nHashTableSize;
    // see if it exists
    CAssoc* pAssoc;
    for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
    // assert here
    {
        if (pAssoc->key == key)
            return pAssoc->value;
     }
    return NULL;
}
Thank you all for your attention, help me think about it and brainstorm
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list