Solved

Call to afxBeginThread blocks till completion of thread Why?

Posted on 1997-06-17
4
459 Views
Last Modified: 2008-03-03

 I am having a problem with afxBeginThread()
 
 Here is relevent code:
 
 struct CThreadInfo
 {
       HANDLE m_hEventStartOperation;
       HANDLE m_hEventKillOperation;
       HANDLE m_hEventOperationKilled;
 };
 
 CmainFrame::CMainFrame()
 {...............
 .
 .
 
 // TODO: Add member initialization code here
 
 // Create Events for image_presenter_thread before Starting it
 
 m_himage_presenter_thread_Start = CreateEvent(NULL, FALSE, FALSE, NULL);
 m_himage_presenter_thread_Kill = CreateEvent(NULL, FALSE, FALSE, NULL);
 m_himage_presenter_thread_Killed = CreateEvent(NULL, FALSE, FALSE, NULL);
 
 // Initialize processing thread parameter structures
 
 m_image_presenter_thread_info.m_hEventStartOperation =
 m_himage_presenter_thread_Start;
 m_image_presenter_thread_info.m_hEventKillOperation =
 m_himage_presenter_thread_Kill;
 m_image_presenter_thread_info.m_hEventOperationKilled =
 m_himage_presenter_thread_Killed;
 
 image_presenter_thread = AfxBeginThread(
                   (AFX_THREADPROC) image_presenter((LPVOID*)
 &m_image_presenter_thread_info),
                   (LPVOID*) &m_image_presenter_thread_info,
                   THREAD_PRIORITY_NORMAL,
                   0,0,NULL);
 .
 .
 .
 SetEvent(m_image_presenter_thread_Start);
 }
 
 UINT CMainFrame::image_presenter(void* pParam)
 {
       CThreadInfo* p_image_presenter_thread_info = (CThreadInfo*) pParam;
 
WaitForSingleObject(p_image_presenter_thread_info->m_hEventStartOperation,
 INFINITE);
 .
 .
 .
 }
 
 WHEN the code executes, AfxBeginThread executes and starts
image_presenter(
 )
 But then, AfxBeginThread DOES NOT RETURN.   Why?
 Of course, image_presenter( ) hangs because
 SetEvent(m_image_presenter_thread_Start)  is never executed
 because of the blocking AfxBeginThread call.
 
 Any help will be greatly appreciated
 
 Thanks
 
0
Comment
Question by:sherwingw
  • 2
  • 2
4 Comments
 

Author Comment

by:sherwingw
ID: 1163964
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 140 total points
ID: 1163965
image_presenter should be a static member function of CMainFrame.

image_presenter_thread = ::AfxBeginThread(image_presenter, &m_image_presenter_thread_info);

AfxBeginThread should return after it creates a new CWinThread object, calls its CreateThread function to start executing the thread.
0
 

Author Comment

by:sherwingw
ID: 1163966
chensu:

Declaring image_presenter to be a static member function, rather than just a member function of CMainFrame is not a viable soulution, as it SEVERLY restricts fhe functionality of image presenter.  Static functions only have access to static variables and static functions.  Consequently, if image_presenter is declared static, it can not make a call to any of the non static windows routines!

I tried the static solution, and went through the pain of "static-ing" all called functions .......but ran into a windows call, which was not a static function. So this is not viable.

Any other suggestions?
0
 
LVL 23

Expert Comment

by:chensu
ID: 1163967
You have to use a static function or a global function. The reason is that a member function contains an implicit parameter (the this pointer). For example,

UINT CMainFrame::image_presenter(void* pParam);

is actually

UINT CMainFrame::image_presenter(CMainFrame *pThis, void* pParam);

if it is not a static function. And this prototype does not conform to what AfxBeginThread() requires. It is the implicit this pointer that lets you access the non-static members.

So, you can pass some data that the thread needs into the structure pointed by void* pParam. For example, you can pass the window handle. But, do not pass the this pointer. As Microsoft documentation says, it is dangerous to pass C++ objects from one thread to another, unless the objects are designed to be used in such a manner.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question