Jump to content
  • Advertisement
Sign in to follow this  
yahn

learning threads?

This topic is 4666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm trying to learn winsock, and you need to use threads in winsock. i don't know how to use threads so i searched google for c++ thread tutorial, and most the results wern't even relevant. can anyone lead me to a good article/website to learn c++ threads? thank you for your time.

Share this post


Link to post
Share on other sites
Advertisement
Well, first you don't need to use threads for winsock, unless you really want to.
You can use select() instead to figure out which sockets have new information. With this approach you can stay single-threaded.

Other information about sockets can be found at Beej's Guide. It focuses on Berkeley's Sockets, but can be used for Winsock as long as you call WSAStartup() before anything else. Also you call closesocket() rather than just close() when closing a socket.

A sample threaded program can be found at MSDN here from (CreateThread).

Share this post


Link to post
Share on other sites
first of, you don't need threads to do network programing.
It might be good to use threads in the future but for the very beginning
you can stick with a single-threaded application.
Winsock provides a way to make certain calls nonblocking.
(like connecting to a server, waiting for connections, ...)

Learning about threads, however is a "very good thing"(tm).

I foud a site clicky.

they use the thread_create() function there. I don't know if it is standard c++,
however. I guess it is unix-specific, in windows you have another method whose name i just forgot... ;)

you can also look into the boost library for a platform independent thread class.

EDIT: you should definetely have a look at boost::threads ;)
http://www.boost.org/doc/html/threads.html

Share this post


Link to post
Share on other sites
I've just been working on threads and winsock myself. If you'd like, I have a object-orientated thread library that is easy to use (it's for windows, of course). I've also written a Winsock2 wrapper that is OK to use as well. I've tested both of them out (together and separately) and they work alright, so if you'd like to have a look at them they might help you to understand how threads (and winsock) works.

Share this post


Link to post
Share on other sites
thank you for the offer, but i think the best way to learn is by figuring it out myself. if you could tell me what you did to learn how they worked, that would be great.

Share this post


Link to post
Share on other sites
The way I started learning about threads was by googling for tutorials (there are many) and looking through the Platform SDK. The basics aren't that difficult. The only *essential* functions are your thread function (the function that your thread will execute, just like your main threads function is WinMain()), CreateThread() and one of the wait functions (e.g. WaitForSingleObject()).

Example:


DWORD WINAPI ThreadProc(void *pParam)
{
for (char i = 0; i < 10; i++)
cout << i << endl;

return 0;
}

int WINAPI WinMain(...)
{
// Handle to the thread.
HANDLE hThread;

// Create the thread.
hThread = CreateThread(
NULL, // Default security attributes.
NULL, // Default stack size.
&ThreadProc, // The address of the function we wish to execute.
// Note: Must have the correct signature.
NULL, // An optional parameter to pass in. Useful if you
// want to encapsulate threading by passing
// 'this'.
0, // Zero means start thread execution immediately.
NULL // We don't need a thread identifier, as the
// hThread HANDLE already does this.
);

// Wait for the thread to finish execution.
WaitForSingleObject(hThread, INFINITE);

// Exit the main-thread/process (the application).
return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!