Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 05-24-2006, 05:25 PM   #1 (permalink)
Forum Expert
 
milt's Avatar
 
Join Date: Nov 2003
Location: Hershey, PA
Age: 19
Posts: 1,601
Send a message via AIM to milt Send a message via Yahoo to milt Send a message via Skype™ to milt
Default Threading with C++

Hello,

Lately I've been coding a lot in C++, and I'm really starting to like it. Right now I'm working on a project that is basically a network chat. I use Borland C++ Builder 6, which isn't too bad. Anyways, I've figured out how to make connections with sockets, etc. Now that I've figured this out, I need to know about threading.

I want to thread each connection, and from my understanding threading with C++ is quite complicated. I heard that it does not have direct features for it, so you have to code it yourself. Is this true? Is there a Thread class? I've tried Googling.... not much info there.

Thanks in advance for any help.
__________________
--Milt, AKA Pokey
milt is offline   Reply With Quote
Old 05-24-2006, 05:47 PM   #2 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hiding in your room.
Age: 21
Posts: 1,269
Send a message via MSN to arul
Default

CodeProject has everything.
http://www.codeproject.com/threads/

(edit)
To be more specific, here's nice article about threading
http://www.codeproject.com/threads/cthread.asp
(/edit)
__________________

Last edited by arul; 05-24-2006 at 06:00 PM.
arul is offline   Reply With Quote
Old 05-24-2006, 07:39 PM   #3 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

You should also check out the portable threading library in Boost's libraries.
http://www.boost.org/doc/html/threads.html

Last edited by Sep102; 05-24-2006 at 07:41 PM.
Sep102 is offline   Reply With Quote
Old 05-25-2006, 07:26 AM   #4 (permalink)
Forum Expert
 
milt's Avatar
 
Join Date: Nov 2003
Location: Hershey, PA
Age: 19
Posts: 1,601
Send a message via AIM to milt Send a message via Yahoo to milt Send a message via Skype™ to milt
Default

Thanks for the info guys, I'll be looking more into it.

After reading through all of it, I'm guessing that there definitley is no built in threading in C++. As for the threading libraries, are those just classes that you add to your project and then learn how to use them?
__________________
--Milt, AKA Pokey
milt is offline   Reply With Quote
Old 05-25-2006, 12:56 PM   #5 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

I don't know about CThread from CodeProject, but it would seem you could just drop it in and use it from the couple of seconds I glanced at it. As for boost.Thread, you would need to first install the Boost libraries, add the path to them into whatever lists paths for the development environment you are using (for example, in VS 2005, add the directory to the VC++ Directories for Include files in the Projects and Solutions tree in the Options dialog). Then you can just include the files you need for the Thread library (which should be <boost/thread.hpp>) and use the classes as per the Boost.Thread documentation (which I posted a link to earlier).
Sep102 is offline   Reply With Quote
Old 05-27-2006, 08:30 PM   #6 (permalink)
Administrator
 
Zippy's Avatar
 
Join Date: Aug 2002
Location: Baltimore, MD
Age: 25
Posts: 4,817
Default

No, there aren't any built in threading classes for C++... but at it's heart windows threading isn't that complicated.

Here's a simple example which I am coding directly from my brain to this post
(sorry for the lack of indent, I'm too lazy to open up VS right now)

Code:
#include <windows.h>
class Thread
{
public:
Thread() : _Handle( NULL ), _Id( 0 )
{
}

~Thread()
{
if ( _Handle )
TerminateThread( _Handle );
}

HANDLE GetHandle() const
{
return _Handle;
}

DWORD GetID() const
{
return _Id;
}

bool Start()
{
if ( !_Handle )
{
_Handle = CreateThread( NULL, 0, _ThreadStartProc, (void*)this, 0, &_Id );
return (bool)(_Handle != NULL);
}
else
{
return false;
}
}

protected:
virtual DWORD _ThreadMain() = 0;

private:
static DWORD WINAPI _ThreadStartProc( void *ptr )
{
if ( ptr )
return ((Thread*)ptr)->_ThreadMain();
else
return 0xFFFFFFFF;
}

HANDLE _Handle;
DWORD _Id;
};
Pretty simple, no?

All your function reference needs: http://msdn.microsoft.com/library/de..._functions.asp
__________________
Zippy, Razor Creator and RunUO Core Developer
The RunUO Software Team

"Intuition, like a flash of lightning, lasts only for a second. It generally comes when one is tormented by a difficult decipherment and when one reviews in his mind the fruitless experiments already tried. Suddenly the light breaks through and one finds after a few minutes what previous days of labor were unable to reveal."
~The Cryptonomicon

Zippy is offline   Reply With Quote
Old 05-27-2006, 08:32 PM   #7 (permalink)
Administrator
 
Zippy's Avatar
 
Join Date: Aug 2002
Location: Baltimore, MD
Age: 25
Posts: 4,817
Default

BTW if you are ever considering cross platform, there is a windows .dll available which implements the posix (pthreads) library. Using this makes it extremely simple to port threaded applications (assuming of course all the other code was already portable).
__________________
Zippy, Razor Creator and RunUO Core Developer
The RunUO Software Team

"Intuition, like a flash of lightning, lasts only for a second. It generally comes when one is tormented by a difficult decipherment and when one reviews in his mind the fruitless experiments already tried. Suddenly the light breaks through and one finds after a few minutes what previous days of labor were unable to reveal."
~The Cryptonomicon

Zippy is offline   Reply With Quote
Old 05-27-2006, 09:12 PM   #8 (permalink)
Forum Expert
 
milt's Avatar
 
Join Date: Nov 2003
Location: Hershey, PA
Age: 19
Posts: 1,601
Send a message via AIM to milt Send a message via Yahoo to milt Send a message via Skype™ to milt
Default

Sweet, thank you very much Zippy for the help and information.

I'll check it out and post back my results
__________________
--Milt, AKA Pokey
milt is offline   Reply With Quote
Old 05-30-2006, 04:21 PM   #9 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

stay away from circular tables
noobie is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5