Click to See Complete Forum and Search --> : A win32 multithreading using vc++ example required


Naumaan
November 6th, 2003, 12:23 PM
Hi

I m new in win32 programming i need a simple example of win32 multithreading in visual c++ with a dialog if possible .

thanks in advance

gstercken
November 6th, 2003, 12:28 PM
What do you mean by "multithreading with a dialog"? IMHO, if you are really new to Win32 programming, you should probably start by something easier than multithreading. Even seasoned Win32 developers often avoid multithreading due to its inherent complexity.

Naumaan
November 6th, 2003, 12:38 PM
thanks for reply

multithreading using dialog means that when i press a button on a dialog in win32 application then one or two threads starts so i can easily understant the initilization and working of threads also if i press another button on dialog then threads stops or ends.

Sorry for confusion

Dave McLelland
November 6th, 2003, 01:22 PM
This does what you want I think.
(from a thing that I posted to anther thread that I cant find now)

Andreas Masur
November 6th, 2003, 01:49 PM
Although I agree with the things pointed out by gstercken...take a look at the attached example...

Sam Hobbs
November 6th, 2003, 08:55 PM
You seem to enjoy a challenge. You will be much more successful if you are able to search the MSDN and find answers there. I was able to quckly find a lot. In particular, look at Multithreading with C and Win32 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_multithreading_with_c_and_win32.asp) and the relevant articles following it. Also I found Multithreading for Rookies (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/msdn_threads.asp). I don't know if Using Multithreading and C++ to Generate Live Objects (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/msdn_frogfly1.asp) will help you; it might be too advanced.

The following is a sample use of _beginthreadex. In this sample, an event is created and passed as an argument. The thread checks the event in a loop and when the event is signaled (by the main thread) the thread returns.#if !defined(_MT)
#error _beginthreadex requires a multithreaded C run-time library.
#endif

#define STRICT 1
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;

#define Second (1000)

unsigned __stdcall Thread(void *ArgList) {
HANDLE hEvent = *((HANDLE*)ArgList);
while (WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0) {
Sleep(Second>>2); // sleep for a quarter of a second
cout << "Waiting for signal\n";
}
cout << "Thread ending\n";
return 0;
}

int main(int argc, char *argv[], char *envp[]) {
int ErrorNumber, DOSErrorNumber;
unsigned ThreadId;
HANDLE hThread, hEvent;

// Create a manual-reset nonsignaled unnamed event
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

hThread = (HANDLE)_beginthreadex(NULL, 0, Thread, &hEvent, 0, &ThreadId);
if (hThread == 0) {
ErrorNumber = errno;
DOSErrorNumber = _doserrno;
cerr << "Begin thread error: " << strerror(ErrorNumber) << '\n';
cerr << "Begin thread DOS error code: " << DOSErrorNumber << '\n';
return 16;
}
cout << "Thread begun\n";

Sleep(Second<<1); // wait 2 seconds
SetEvent(hEvent); // signal thread to end
int rv = WaitForSingleObject(hThread, Second<<3); // wait up to 8 seconds
cout << "Thread wait rv " << rv << '\n';
CloseHandle(hEvent);
CloseHandle(hThread);

return 0;
}

Naumaan
November 6th, 2003, 10:41 PM
Thanks all for reply

but the above examples posted by Andreas Masur and Dave McLelland are not a win32 examples .

and the example posted by Sam Hobbs is usefull but it did not use visual v++ win32 architecture with Standard LPRESULT CALLBACK FUNCTIONS . I already mension that i want to learn win32 programming and multithreading in win32.

thanks

Dave McLelland
November 6th, 2003, 10:45 PM
Originally posted by ABDULNAUMAN
Thanks all for reply

but the above examples posted by Andreas Masur and Dave McLelland are not a win32 examples .

and the example posted by Sam Hobbs is usefull but it did not use visual v++ win32 architecture with Standard LPRESULT CALLBACK FUNCTIONS . I already mension that i want to learn win32 programming and multithreading in win32.

thanks The forum for non MFC is "C++and WinAPI"

Andreas Masur
November 6th, 2003, 10:50 PM
As Dave already has pointed out this question would then be better placed in the WinAPI forum. I will go ahead and move it there.

Nevertheless, the although the examples are basically based on MFC, there are no big differences in regard to the threads itself. Thus, based on the examples, one can easily integrate the threads in a WinAPI application as well.

Fur further reference you might also want to take a look at the following FAQs:

How to create a worker thread? (http://www.codeguru.com/forum/showthread.php?s=&threadid=231241)
How to end a thread? (http://www.codeguru.com/forum/showthread.php?s=&threadid=231242)
How to use member functions as thread functions? (http://www.codeguru.com/forum/showthread.php?s=&threadid=231246)

Andreas Masur
November 6th, 2003, 10:51 PM
[Moved thread]

Sam Hobbs
November 6th, 2003, 11:21 PM
First learn a bit more on the subject before being particular. I am nearly certain for multithreading, the C runtime functions are the preferred solution. I think there are minor problems if we use the Windows multithreading functions directly. Note that the C runtime functions (CRT) also use the Windows multithreading functions, it is just that the CRT versions are more compatible with C and C++.

filthy_mcnasty
November 8th, 2003, 01:44 PM
to the original poster, i strongly recommend you read all of the thread related chapters from

Programming Applications for Windows - Jeffrey Richter

It covers in great detail, with examples and source and in win32, what you are talking about. I will not, however, post any of these because they are copyrighted but if that's what you're looking for then you should buy the book and read it. However, this book is definately not aimed at beginners and as others have said, you should tackle other things first.


As Sam said, the c runtime stuff is prefered (_beginthreadex as opposed to it's API counterpart) but the need for this isn't THAT significant if you ask me. they both ultimately have the same results.

Sam Hobbs
November 8th, 2003, 03:03 PM
Originally posted by filthy_mcnasty
As Sam said, the c runtime stuff is prefered (_beginthreadex as opposed to it's API counterpart) but the need for this isn't THAT significant if you ask me. they both ultimately have the same results. Yes.

Note that my comments were a response to the statement that my sample "did not use visual v++ win32 architecture"; in other words, "the need for this isn't THAT significant" when "this" is the Windows API functions.

It is my understanding that the disadvantage of the Windows API functions is a possibility of a slight memory leak.

filthy_mcnasty
November 8th, 2003, 07:17 PM
right, it deals with the tiddata structure allocated internally by all that stuff. linking the project w/ the multithreaded c/c++ RTL will allow this whole process to work properly (w/ the CreateThread API) and cause no difference from _beginthreadex() but some (me too) consider it just better practice. then someone else looking at your code can minimize what they are concerned with. ie, they can focus on just the code instead of project settings.