views:

890

answers:

7

I am a java programmer but currently working on the c++ language. Unlike java, the c++ does not define any threading utility. It is a little hard for me to implement a multi-threaded application in c++. Is there anyway someone can implement his own threading library using c++? Must you be able to grasp some concept of assembly language? I have in many occassions tried to create a thread from the OS specific features but i have always failed. Can someone help out?

+6  A: 

Given your level of expertise, perhaps Boost Threads would be helpful?

http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html

In answer to your other questions, of course you can implement your own threading library. No you do not need to know any assembly language to do so, but there are no C standard library functions to help you, you will need to use the OS specific libraries. For example on windows you use the beginthreadex CRT function, which ends up calling the CreateThread Win32 system call, but on many unixes you would use pthread_create.

Other than starting a thread, you probably want some synchronization primitives, the Boost library delivers on that front as well. If you want to roll your own, on Windows look into CreateMutex, WaitForSingleObject and ReleaseMutex or the CriticalSection functions, on unix look into pthread_mutex_init.

Don Neufeld
+1  A: 

if you are using POSIX systems such as Linux or Unix or even windows you can use pthread

if you are stuck on a windows machine without .NET i think you don't have much choice but to learn win32 threads. Of course there are plenty of "win32 thread libraries" on the tubes.

Boost

CodeProject

SourceForge

I suspect that most large code repositories have some kind of custom implementation...

The easiest choice win windows could be going for .NET threads which are much easier to deal with. You can have Visual C++ .NET for free

Eric
A: 

Following URL states "A platform-independent, multi-threading and synchronization library for C++"

http://zthread.sourceforge.net/

Best of luck to implement the same.

lakshmanaraj
+1  A: 

Pthreads can be used in C++. I usually just write some simple wrappers around them.

Also, if you're just trying to splut up a bunch of low level loops, please check out OpenMP. It's insanely easy to use.

I have a little C++ project that has a threading library here. The purpose is pretty specific, but it can serve as an example perhaps.

http://github.com/mikelikespie/reccage

mikelikespie
+4  A: 

I would suggest that you use an existing multiplatform library as Boost, ACE, POCO, QT...

ACE has quite a bit of thread classes, the simplest to use is probably ACE_Task_Base. You inherit from it and provide an implementation for int svc() method. That is the method that gets called when you activate the thread.

class WorkerThread : public ACE_Task_Base
{
public:
   int svc() { ... }
};
int main()
{
   WorkerThread worker;
   worker.activate();
   worker.wait();
}

With POCO you have to use the Runnable interface and the usage is close to what you would get with Java threads (if you implement Runnable, not extend Thread):

class Worker : public Poco::Runnable { ... };
int main() 
{ 
   Worker worker;
   Poco::Thread thr;
   thr.start( worker );
   thr.join();
}

Boost threads (recommended) are a little different. Instead of implementing/extending an interface it depends on the operator() being defined. You create a thread passing it an object that implements operator()(). The thread starts by calling that method. The good part is that it is really simple to combine with boost::bind to create a fake functor (object implementing operator()):

class X { public: void method( int argument ); }
int main()
{
   X x;
   boost::thread thr( boost::bind( &X::method, &x, 100 ) );
   thr.join();
}

The snippet will create an object of type X. With bind you create a functor that wraps a call to the method X::method applied on the object x, passing it 100 as the method argument.

While this differs most from the Java perspective it is also the most flexible approach, as you can decouple the threading model from the real working code. No need to fit a particular interface. This is also the upcoming standard threading interface.

David Rodríguez - dribeas
+3  A: 

Besides the libraries other have mentioned, there is Intel's Threading Building Blocks. It is a open source (GPL2 with linking exception), cross platform library from Intel.

cuda
It's nice to have at least a short at the documentation, as many of the ideas are quote useful.
Anonymous