|
||
|
|
#1 (permalink) |
|
Forum Expert
|
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. |
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
|
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. |
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
|
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. |
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
|
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? |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
|
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).
|
|
|
|
|
|
#6 (permalink) |
|
Administrator
Join Date: Aug 2002
Location: Baltimore, MD
Age: 25
Posts: 4,817
|
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;
};
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 |
|
|
|
|
|
#7 (permalink) |
|
Administrator
Join Date: Aug 2002
Location: Baltimore, MD
Age: 25
Posts: 4,817
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|