Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,587 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 831 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Threads in Win32?

 
Reply to this topicStart new topic

Threads in Win32?

milotictear
post 8 Aug, 2008 - 12:22 PM
Post #1


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 3



Thanked 1 times
My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page


Tom9729
post 8 Aug, 2008 - 12:41 PM
Post #2


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,429



Thanked 10 times

Dream Kudos: 325
My Contributions


Multithreading is probably implemented as part of the Windows API.

Edit:

Two links f.ex.
http://msdn.microsoft.com/en-us/library/y6h8hye8(VS.80).aspx
http://www.mycplus.com/tutorial.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: 8 Aug, 2008 - 12:43 PM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 8 Aug, 2008 - 12:44 PM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 37 times

Dream Kudos: 525
My Contributions


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.

This post has been edited by NickDMax: 8 Aug, 2008 - 12:46 PM
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 8 Aug, 2008 - 01:46 PM
Post #4


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 550



Thanked 43 times

Dream Kudos: 25
My Contributions


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:

cpp


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:

cpp


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: 8 Aug, 2008 - 01:47 PM
User is offlineProfile CardPM

Go to the top of the page

milotictear
post 8 Aug, 2008 - 02:43 PM
Post #5


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 3



Thanked 1 times
My Contributions


Thanks all. CreateThread worked perfectly for me after a bit of work.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/11/08 07:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month