Thread Synchronization
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Thread Synchronization

  1. #1
    Join Date
    Jan 2004
    Location
    Noida, India
    Posts
    47

    Thread Synchronization

    I have a Dialog based MFC application. In the event handler of one of my button, I want to create a thread with apartment set as a single threaded apartment. In the launching thread i want to perfoem some operation. I want that application first execute launching thread function and after that it perform other operations written in the handler. I have written a code which is as below:

    Code:
    void CTestthreadDlg::OnButton1() 
    {
    	DWORD dwThrdParam = 1; 
    	hThread = ::CreateThread(NULL,0,ThreadFunc,&dwThrdParam,0,&dwThreadId);
    	AfxMessageBox("2");
    }
    
    DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
    { 
    	CoInitialize (NULL);
    	AfxMessageBox("1");
    	CoUninitialize();
                    return 0; 
    }
    Now when I execute this then I first get messagebox written as 1 and after that 2. This means that thread function is executing as an asynchronous operation. So my problem is that what should I do so that it first display 2 and then 1.

    Please help.

    Thanks,
    Amit

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Thread Synchronization

    Create thread as suspended using CREATE_SUSPENDED flag and call ResumeThread after AfxMessageBox("2").

    BTW. Once using MFC, better add a CWinThread member and use AfxBeginThread.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Thread Synchronization

    Quote Originally Posted by amit_kr_gupta
    I have a Dialog based MFC application. In the event handler of one of my button, I want to create a thread with apartment set as a single threaded apartment. In the launching thread i want to perfoem some operation. I want that application first execute launching thread function and after that it perform other operations written in the handler. I have written a code which is as below:
    ........
    Thanks,
    Amit
    In a MFC application you MUST use AfxBeginThread instead of ::CreateThread function.
    See also Using Worker Threads

  4. #4
    Join Date
    Jan 2004
    Location
    Noida, India
    Posts
    47

    Re: Thread Synchronization

    I am sorry, I had written wrong statement as :

    "Now when I execute this then I first get messagebox written as 1 and after that 2. This means that thread function is executing as an asynchronous operation. So my problem is that what should I do so that it first display 2 and then 1"

    Actually I am first getting messagebox 2 and then 1. My requirement is that i should first get messagebox 1 and then 2.

    Please help.

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Thread Synchronization

    You can call WaitForSingleObject(hThread).
    Moreover search for Thread Synchronization in MSDN.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    Join Date
    Jan 2004
    Location
    Noida, India
    Posts
    47

    Re: Thread Synchronization

    If I call WaitForSingleObject(hThread, INFINITE) after the CreateThread then it is not displaying any messages i.e neither 1 nor 2 and it seems hanged.

  7. #7
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Thread Synchronization

    Then MsgWaitForMultipleObjects can save you.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  8. #8
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: Thread Synchronization

    WaitForSingleObject works fine for me.

  9. #9
    Join Date
    Jan 2004
    Location
    Noida, India
    Posts
    47

    Re: Thread Synchronization

    Bob,
    I think you have tested it in Console Application. If you test it in MFC Application (let's assume on the event handler of a button ) then you may face the same problem.

    Now after inserting Wait for Single Object, my code looks like below, but I am not getting any output

    Code:
    void CTestthreadDlg::OnButton1() 
    {
    	DWORD dwThrdParam = 1; 
    	hThread = ::CreateThread(NULL,0,ThreadFunc,&dwThrdParam,0,&dwThreadId);
                    WaitForSingleObject(hThread,INFINITE);
    	AfxMessageBox("2");
    }
    
    DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
    { 
    	CoInitialize (NULL);
    	AfxMessageBox("1");
    	CoUninitialize();
                    return 0; 
    }
    Please help, I am still facing the problem. If someone has written any successful test for this scenario then I will appreciate if you attach the sample code also.


    Thanks,
    Amit

  10. #10
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: Thread Synchronization

    I tested it in a MFC application.

    Code:
    BOOL CNik1Doc::OnNewDocument()
    {
    	if (!CDocument::OnNewDocument())
    		return FALSE;
    
    	DWORD dwThrdParam = 1; 
    	DWORD dwThreadId;
    	HANDLE hThread = ::CreateThread(NULL,0,ThreadFunc,&dwThrdParam,0,&dwThreadId);
    	::WaitForSingleObject(hThread, INFINITE);
    
    	AfxMessageBox("2");
    	
    	return TRUE;
    }
    
    DWORD WINAPI CNik1Doc::ThreadFunc( LPVOID lpParam ) 
    { 
    	CoInitialize (NULL);
    	AfxMessageBox("1");
    	CoUninitialize();
      return 0; 
    }
    Also you don't need ConInitialize/Uninitialize here. That's for COM not multithreading.

    Are you running in debug/breakpoint or without any breakpoints?
    Last edited by Andreas Masur; March 9th, 2005 at 07:34 AM.

  11. #11
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Thread Synchronization

    Did you try
    Quote Originally Posted by ovidiucucu
    Then MsgWaitForMultipleObjects can save you.
    ?
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Thread Synchronization

    [ Moved thread ]
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  13. #13
    Join Date
    Jan 2004
    Location
    Noida, India
    Posts
    47

    Re: Thread Synchronization

    Bob,
    Thanks. Can you please attach your sample for me. I will really appreciate your help.

    ovidiucucu,
    I tried using MsgWaitForMultipleObjects(1,&hThread,TRUE,INFINITE,QS_ALLEVENTS); but still it is hanging.

    Thanks,
    Amit

  14. #14
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: Thread Synchronization

    There you go.
    Attached Files Attached Files

  15. #15
    Join Date
    Jan 2004
    Location
    Noida, India
    Posts
    47

    Re: Thread Synchronization

    Bob,
    How you decalred your ThreadProc function in your header file. I am declaring like below in my header file:

    Code:
    DWORD WINAPI ThreadFunc(LPVOID lpvParameter);
    and in my implementation file, it is like

    Code:
    DWORD WINAPI CTestthreadDlg::ThreadFunc(LPVOID lpvParameter)
    {
    CoInitialize(NULL);
    	AfxMessageBox("1");
    	
    	CoUninitialize();
        return 0; 
    }
    Now I am getting compiler error as
    "error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center