Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do multi-threaded programming

Status
Not open for further replies.

reneb

Programmer
Aug 8, 2000
43
US
I have a function that sets up a progress dialog and does time consuming conversion. The problem is that when the function is in the middle of a conversion, if the user clicks to a different application and returns, the progress dialog will not redraw itself untill the conversion has completed. I am assuming that if I throw the conversion into a separate thread then the progress window will be able to redraw itself. Is my assumption correct? If yes, how do I setup a separate thread? I have done it in java but not c++. Please help. Thanks in advance.
[sig][/sig]
 
Dear reneb,

>> If yes, how do I setup a separate thread?

If you mean 'how to start a thread' here is an excerpt from an MSDN article. A full discussion on how to control the threads behavior and how to perform synchronization is to lengthy to undertake in a single message. There are many articles and samples on MSDN regarding thread creation and use.

// Excerpt from MSDN Visual C++ KB: ID: Q132078

This article shows by example how to use the thread handle returned by _beginthreadex() with the synchronization API WaitForSingleObject() in multithreaded applications.

When developing a multithreaded Win32-based application with Visual C++, you need to use the CRT thread functions to create any threads that call CRT functions. To create and terminate threads, use either _beginthread() and _endthread() or _beginthreadex() and _endthreadex(). If you use the Win32 APIs CreateThread() and EndThread() instead, some of the CRT functions used in the thread will not work. You need to use the CRT functions to both create and end the threads or you lose the memory that the CRT allocates for the thread.

Hope this helps
-pete [sig][/sig]
 
Hello,

_beginthread(), etc. are older C thread functions. If you want the real plus in C++ use CWinThread class. Easier to use and lots of help in MSDN.

Bother C
 
Does CWinThread still impose the static vs virtual function calls? the reason I ask is that when using the old 'c' style in VC++, I had a very hardtime gettng callbacks, and thread monitoring, just due to a problem that I wasnt allowed to call a virtual function from a static one. [sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
Dear Karl,

> just due to a problem that I wasnt allowed to call a
> virtual function from a static one.

That has nothing to do with CWinThread specifically. That is part of the C++ specification. A static function by definition has no instance data, therefore no 'this' pointer. Of course having a 'this' pointer is critical to virtual functions.

The common practice for most call back functions is to provide a user data parameter in both the callback and the setup fuction, as in _beginthread. It takes a (void*) parameter which in turn is passed on to the thread function which is defined as:

void( __cdecl *start_address )( void * ),

That parameter is how you pass your 'this' pointer in the case of an object oriented thread class.

class mythread
{
unsigned long _tHandle;
public:
static void __cdecl threadfunc(void* ptr);
virtual int getIntValue(){ return 10; }
void start(){
_tHandle = _beginthread( threadfunc, 0, (void*)this);
}
};

// static callback function
void __cdecl mythread::threadfunc(void* ptr){
// no this pointer so us the 'ptr' parameter to gain access
// to the object instance that is using this callback
mythread* pThis = (mythread*)ptr;
// call virtual function
cout << pThis->getIntValue();
}

Hope this helps
-pete [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top