I'm a Java programmer who started a trek into c++ a few weeks ago, and in the project I'm working on I need a Thread, and I can't seem to find any class that c++ already has to help on this.
If I go to use sleep(int) it'll pause execution, which I don't want at all. I've heard of SetTimer for win32 but can't seem to find any resources on its uses or if it'd be what I'm looking for.
What I'm working on is a program like the Microsoft office assistants, and I'm trying to make it animated by managing the times between frame changes in a separate thread. Does anyone have any idea of what I would use, since c++ doesn't seem to have a built in thread class?
Threads in Win32?
Page 1 of 14 Replies - 626 Views - Last Post: 08 August 2008 - 02:43 PM
Replies To: Threads in Win32?
#2
Re: Threads in Win32?
Posted 08 August 2008 - 12:41 PM
Multithreading is probably implemented as part of the Windows API.
Edit:
Two links f.ex.
http://msdn.microsof...ye8(VS.80).aspx
http://www.mycplus.c...ial.asp?TID=288
More are available on Google.
If you're looking for crossplatform multithreading, there's an implementation of POSIX threads for Windows.
http://sourceware.org/pthreads-win32/
Edit:
Two links f.ex.
http://msdn.microsof...ye8(VS.80).aspx
http://www.mycplus.c...ial.asp?TID=288
More are available on Google.
If you're looking for crossplatform multithreading, there's an implementation of POSIX threads for Windows.
http://sourceware.org/pthreads-win32/
This post has been edited by Tom9729: 08 August 2008 - 12:43 PM
#3
Re: Threads in Win32?
Posted 08 August 2008 - 12:44 PM
Well you could look into Boost's thread library. Boost is sort of an extension to C++/STL (it also makes up the bulk of the changes being added to the new C++ specification).
Other than that, you can use the windows API to create and manage threads. CreateThread() and ExitThread().
Personally I think it is much better to suffer though the initial headache of setting up your IDE to work with boost than to suffer the headache of working with the Windows API threading model.
Google will turn up a lot of tutorials on using the window API for multi threading, but be careful to ensure that you use constructors and deconstructors properly to ensure you don't end up with memory leaks.
Other than that, you can use the windows API to create and manage threads. CreateThread() and ExitThread().
Personally I think it is much better to suffer though the initial headache of setting up your IDE to work with boost than to suffer the headache of working with the Windows API threading model.
Google will turn up a lot of tutorials on using the window API for multi threading, but be careful to ensure that you use constructors and deconstructors properly to ensure you don't end up with memory leaks.
This post has been edited by NickDMax: 08 August 2008 - 12:46 PM
#4
Re: Threads in Win32?
Posted 08 August 2008 - 01:46 PM
SetTimer is easy. There are two ways you can use it. You can use it in conjunction with a WindowProc, or you can use it with a standalone callback function.
With a callback:
As a window proc event:
By the way, Posix Threads is available for Windows also.
With a callback:
void APIENTRY OnTimerElapsed(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
// Do something
}
// To set the timed callback:
// 5000 means it is called in 5000ms, or 5 seconds
// 1000 is just some arbitrary identifier that you can assign to identify the timer. It cannot be zero, however.
SetTimer(NULL, 1000, 5000, OnTimerElapsed);
As a window proc event:
LRESULT APIENTRY WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_TIMER:
// wParam is the timer id
// lParam is a function specified as the last param of SetTimer
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
// To set this:
// hWnd is the handle of the to-be associated window.
SetTimer(hWnd, 1000, 5000, NULL);
By the way, Posix Threads is available for Windows also.
This post has been edited by perfectly.insane: 08 August 2008 - 01:47 PM
#5
Re: Threads in Win32?
Posted 08 August 2008 - 02:43 PM
Thanks all. CreateThread worked perfectly for me after a bit of work.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|