Implementing a C++ threading Library in c++ - c++

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?

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.

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.

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.

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

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

Another easy to use, platform independent concurrency HEADER ONLY library:
https://github.com/zlateski/zi_lib/tree/master/zi/concurrency/
examples are under test directory

Following URL states "A platform-independent, multi-threading and synchronization library for C++"
http://zthread.sourceforge.net/
Best of luck to implement the same.

Related

Is there a common design pattern or object oriented approach used to encapsulate thread behaviour?

I have recently been using pthreads on Linux and want to start looking into using boost threads in the near future. I have never used MS visual studio so I don't know the approach there but I (have to) use Embarcadero C++ Builder (formerly Borland) one of the few good things I find with it is that is has a built in class TThread. This is a class that you can derive from to give nicely encapsulated data and start and terminate functions. I prefer this approach than the pthread way of passing functions and void* being passed into the thread create function. I was wondering if there is some kind of design pattern or structure that is commonly used to provide a more object oriented that encapsulates the functionality of threads in this way? I can try and create it myself, but I cannot be the first person to desire this kind of approach and wondered if there was a "standard" way of achieving it.
Edit: Alternatively, if this is a very bad idea perhaps an illustration of why?
I would consider that the most standard approach would be using the standard thread library (closely related to boost::thread, not 100% the same). I would avoid redesigning what has already been designed, reviewed and verified by a committee of experts.
Also note that for the kind of operations that you mention in the comment you might want to take a look at the future part of the standard library (again similar to the boost counterpart), and in particular to the std::asynch function.
I like the way boost::thread is designed (I saw a similar design in the threading library from Rouge Wave). Basically a thread is started by passing a function to be executed and latter that thread object can be used to join that thread. pthread is very similar in design, but boost::thread is much easier to use.

Lightweight portable C++ threading

Does anyone know about a lightweight portable C++ threading library, that can work on Windows, Linux and Mac OS X?
Specifically in my case, I do a simulator that after each time passes exports simulated data. I would like to run only one thread (simulate) that would once in a while start another thread (export). The only condition would be: if export thread started wait until it finishes, before starting a new one.
Thanks
What about TinyThread++?
Need portable threads for your C++
app? Is C++0x unavailable for your
target compiler(s)? Is Boost too
large?
Then you need TinyThread++!
TinyThread++ implements a fairly
compatible subset of the C++0x thread
management classes.
I use Boost.Thread and would recommend it to others.
It is portable to almost everything and easy to use. The "lightweight" thing is the only question, since I am not really sure what that means. Boost is lightweight in that there is almost no overhead to using it, since all the threading functionality is loose static wrapping for the underlying threading library (pthreads, Win32 API, Cell BE, etc). A "mutex" is really anything that implements that "Lockable" concept (see the documentation), which can be anything - even your own special whatever. In that sense, it is very lightweight and extensible.
However, Boost is a huge library and pulling just the parts of it you need can be extremely painful (this is a common complaint about Boost in general). Off the top of my head, using Boost.Thread means you have to have Boost.DateTime, Boost.System, Boost.ConceptCheck and Boost.Compiler (and probably more and whatever those rely on, etc). To their credit, it is very easy to build what you need if you have the whole library due to their automatic linking magic, but the need to have it all is definitely something to consider, especially if Windows is on the list of targets.
As an alternative to Boost, I would recommend OpenMP, assuming your compiler has support for it. The fact that it requires compiler support for some of the more advanced features might disqualify it on the "lightweight" thing, but it is pretty lightweight on usage (the first time you #pragma omp parallel for is pretty neat). It is not as feature-packed as Boost (I think only Qt can compete here), but using OpenMP gives you some really cool features that no other threading library can do. You'll have to use a somewhat modern compiler, but both GCC and MSVC have good support here. One caveat is that it is really a C library, which I see as a disadvantage if you're doing C++, but that could be a good thing for your requirements.
If you're looking for something significantly more lightweight (in both senses of the word), then I would recommend OpenThreads. It is nowhere near as extensible as Boost and find it less performant (not significantly, though), it is pretty well-designed and worth mentioning. It will hit all of your specified targets (Windows, OSX and Linux), so if it has the features you want, go for it.
Also, Wikipedia.
Boot.Thread is what you are looking for. To quote its description in Boost's doc pages
Portable C++ multi-threading.
Boost.Thread would work here.
You can use join to wait for an existing thread to finish.
There are other code samples in the docs but starting a thread looks like this:
struct callable
{
void operator()();
};
boost::thread copies_are_safe()
{
callable x;
return boost::thread(x);
} // x is destroyed, but the newly-created thread has a copy, so this is OK
boost::thread oops()
{
callable x;
return boost::thread(boost::ref(x));
} // x is destroyed, but the newly-created thread still has a reference
// this leads to undefined behaviour
I've mostly heard about Boost.Thread which can be pretty heavy, Poco.Thread which is supposed to be lightweight and Intel's TBB which I don't know how it works.
I did a little experimentation with C++0x but I've been told it's not mature enough yet for complex implementations.
I use the c++11 Standar for threads to create a class multithreading and it works for Linux, Windows and OS X, may it be could be use full for you:
https://github.com/jorgemedra/C-11-Threads/blob/master/README.md

Multithreading in C++ ... where to start?

I'd like to start learning multithreading in C++. I'm learning it in Java as well. In Java, if I write a program which uses multithreading, it will work anywhere. However in C++, doesn't multithreading rely on platform-specific API's? If so, that would seem to get in the way of portability.
How can I do multithreading in C++ without causing portability issues? Is boost's thread library a good solution?
As a sidenote - how is it even possible to implement multithreading as a library? Isn't that something that has to be done by the compiler?
If you do not have a compiler that supports C++0x yet (available with visual studio c++ 2010 for example), use boost threads. (Unless you use a framework that already supports threading, which is not the case - you wouldn't ask the question otherwise -). These boost threads became actually the standard in the brand new C++. Before that time C++ itself was thread unaware.
TBB Threading Building Blocks might also be interesting to you if you want to learn other aspects in parallel programming.
Regarding Qt: if you only want threading support it is complete overkill. It has horribly slow round trip times from compiling to result. It is really well designed thought. But not an official standard like the C++0x threads from boost. Therefore I would not take it as a first choice.
In C++, yes threading is platform specific. However, many threading libraries encapsulate the nuances of threading on various platforms, providing a uniform API with which to write your threaded app, so you don't have to worry about platform specific details.
Boost threading library is a very good solution.
I also recommending checking out ACE as well.
Let's start backward:
How is it possible to implement threading in a library ?
It isn't, at least not in (pure) C++. This requires language support (the compiler is only an implementation).
At the moment 2 things are used:
assembly code for some parts (like in the pthread library)
specific compiler instructions for others (dependent on the compiler and platform)
Both are brittle and require a tremendous amount of work for portability. Basically it means lot of #ifdef portions in the code to test for the compiler and targetted architecture, test for the support of some directives etc...
That is why it was deemed necessary to add threading support in C++0x.
How do I do multithreading ?
Even before you choose a library, you should choose a method. There are 2 ways of programming multithreaded applications (and you can combine them):
Communicate by sharing: this means using mutexes, atomic operations, etc... you can use pthread on Linux platforms, but I would recommend Boost.Thread (among others) for its portability.
Share by communicating: more recent, and adapted to distributed computations, this stems from the functional languages. This means passing messages from one thread to another and not sharing any resources. You can use FastFlow or Intel's Thread Building Blocks aka TBB.
You can conceivably merge the two, but it would be better not to. Personally I have found the description of FastFlow totally awesome: it encourages lock-free programming. Also, the main advantage of the second method is that it's better adapted to multi-processes programming and scales to distributed environments.
To begin with, I would recommend focusing on either one and build some applications with it. When you're comfortable you may try out the other, but be ready to start anew, they are that different.
//This program explains how pthread works, here 5 thread are trying to update a global variable simultaneously but with locking synchronization in maintained
#include<iostream>
#include<pthread.h>
using namespace std ;
#define MAX_NO_THREAD 5
int global_sum = 0 ;
pthread_mutex_t lock ; //Declared global lock mutex object
void *print_fun(void *arg)
{
cout<<"\nThread id : "<<(int)arg;
pthread_mutex_lock(&lock) ; //aquiring lock on piece of code
for ( int j=0; j<100000000; j++)
{
global_sum++ ;
}
pthread_mutex_unlock(&lock) ; //reomving lock on peice of code
cout<<"\nGlobal Sum : "<<global_sum ;
}
int main()
{
int i = 0 ;
pthread_t threads_obj[MAX_NO_THREAD] ; //Initializing object array for thread
pthread_mutex_init(&lock, NULL) ; //Initalinzing lock object for thread
int st ;
for ( i=0; i<5; i++)
{
pthread_create(&threads_obj[i], NULL, *print_fun, (void *)i) ;//Initializing threads calling function print_fun
pthread_join(threads_obj[i], 0) ; //Forcing main thread to main until these thread complete
}
pthread_mutex_destroy(&lock) ; //Destroying lock object
}
//compile this program using -lpthread option with g++
//g++ thread.cc -lpthread
also check out
Qt
To offer a suggestion different from Boost, I use Pthreads (or Pthreads-Win32 on Windows). It's a very do-it-yourself barebones library, but provides you with all you need and nothing else. It's very lightweight compared to Boost and you can easily find C++ wrappers around it to give you higher level abstractions.
you might also consider openmp http://openmp.org. Many compilers support it, including MS, GCC/G++ and Intel. Although you don't get explicit control of threads, its higher level abstraction of parallelism is sometimes more efficient (at coding time as well as runtime), and the code is much easier to understand. It's not going to help you much if you're doing GUI work, but for scalable computation it's worth a look.
The Boost Threading Library is probably the best place to start for C++. Gives you threading contructs, as well as all the mutexes and control objects you need to write a real working multithreaded application.
With C++11, "Thread support library" is introduced under <thread> header.
More info could be found from below links
https://en.cppreference.com/w/cpp/thread
http://www.cplusplus.com/reference/thread/
If you're doing this out of interest to improve your knowledge of different programming models and language skills then the Boost library would be a fine way to go. However I would think long and hard about actually building any production applications using multi-threaded C++.
C++ is challenging enough at times to attain correctness without adding the considerable complexity of shared memory multi-threading. Even the most seasoned programmers would agree that multi-threaded programs are extremely hard to reason about and get right. Even the simplest programs can quickly become hard to test and debug when multi-threaded.
Imperative languages such as C++ or Java or C# (with their mutable variables, shared memory and locking/signalling primitives) are very often the least approachable way to try to build multi-threaded applications. There are usually perfectly good single threaded implementation options to accomplish most user-space (as opposed to kernel or embedded) application problems, including on multi-core machines.
If you really want to build reliable "multi-threaded" applications I would suggest you check out functional languages like Erlang, Haskell, F# or Clojure.

What is easiest way to create multithreaded applications with C/C++?

What is the easiest way to create multithreaded applications with C/C++?
unfortunately there is no easy way. Couple of options: pthread on linux, win32 api threads on windows or boost::thread library
There is no easy way to create a multithreaded application in any language.
Just to mention it because it hasn't been mentioned: a compiler with OpenMP support (http://openmp.org/wp/)
Qt has a pretty
threading support and documentation
but as others have cautioned this is not for beginners. The doc link I gave itself points to a short reading list
pthreads!
https://computing.llnl.gov/tutorials/pthreads/
There is no simple answer to this. It depends very heavily on what you hope to gain from multithreading, the platform/compiler, and which threading model you want to use. Every threading API has its pitfalls.
And just because no one has mentioned it so far, OpenMP is another option that is supported in many modern mainstream compilers and is intended to simplify the use of concurrency.
http://openmp.org/wp/
It's been a while since I worked in C++ and I haven't seen the Boost threading support, but I found it very helpful to encapsulate semaphore services provided by the OS, usually either POSIX or Win32, in simple classes that would acquire locks, and release them in the destructors, making their use fairly simple.
void operateOnSharedResource(SharableResource & foo) {
MutexLock lock(foo.getMutex());
// do stuff to foo
// implicit call to MutexLock dtor performs release
}
Ultimately there are lots of simple tricks like this to ease thread programming and I'd be surprised if Boost didn't have something like this by now (EDIT: It does and it's documented in Lock Types).
Regardless, the main problem with writing multi-threaded code isn't going to be solved by any third party library, and that's understanding where your code can be parallelized usefully, and where shared resources are going to be touched and must be accounted for. Here's a few rules of thumb I use when writing multi-threaded code.
Try to minimize the number of shared resources
Try to encapsulate shared resources in class wrappers that make all operations atomic.
Make worker threads as simple as possible
Proper encapsulation really does wonders for writing safer multi-threaded code, because the fewer things you can see, the fewer things can have a race condition.
I'm not sure about the easiest, but IMO the most user-friendly threading library is the one included in the Poco C++ project. For a preview have a look at the Thread.h header file.
The C++0x specification includes threading facilities (one of my favorite new features). Soon it won't matter what OS you're compiling for! Just look how easy it is to create a new thread and join back to the creator thread:
#include <thread>
#include <iostream>
class SayHello
{
public:
void operator()() const
{
std::cout<<"hello"<<std::endl;
}
};
int main()
{
std::thread t((SayHello()));
t.join();
}
Visual Studio 2010 is implementing parts of C++0x but we're still waiting on the threading facilities.
Boost.Thread is relatively easier because it's portable, well-documented and has high-level API such as scoped_try_lock.
Besides the ones already mentioned, ACE is another popular and widely deployed C++ framework that provides thread encapsulations across multiple platforms. It's style of C++ isn't as modern as Boost.Thread, for example, but it is quite mature.
I would say with Qt. Qt Threads and Qt Concurrency are probably worth googling.
Posix Thread is quite good, they also come with great documentation and tutorials.
Not as easy as java threads, but still quite good.
The easiest way is by avoiding/minimizing mutable shared state.
Once you have mutable shared state, you need to deal with locking which is where the bulk of the difficulty in writing multi-threaded programs exists.
This depends entirely on what you're doing. If you can fit what you're doing into OpenMP then that is the way to go. Otherwise you may want to look at Intel's TBB. TBB offers several workflows which you should be able to fit into, but the library is dual licensed and you may not be able to accept either license. If both OpenMP and TBB are out, then you should consider your operating system's thread pools abilities.
At some point you may need to bite the bullet and use Boost.Thread. If so, you'll want to look at what makes multithreading in C++ hard (good to read even if you're not using C++0x: "It's not the threads themselves, it's the communication that causes problems. Mutable shared state introduces implicit communication," page 3).

Standard practice for implementing threads in C++?

I'm looking forward to an interview in C++ in the coming weeks. (yay) So I have been relearning C++ and studying up. Unfortunately I have realized that I've never implemented threads in C++, and am somewhat concerned about a quiz on concurrency.
As far as I can tell, C++ uses pthreads in Linux and some other device in Windows. Is this correct? Is there another industry standard, more OO way to handle threads in C++ that I should be expected to know? And are there any good web resources that you can point me to for practicing and learning threads in C++?
Thanks!
There is a boost threads library which is probably the closest to a standard.
Generally threads are supplied by the OS so you get whatever the OS supplied. Also peoples first exposure to threading is often in a GUI, to allow a background calculation to not block the GUI, and so people tend to use the thread functions supplied by the particular GUI framework (MFC/Qt etc)
Currently C++ is entirely unaware that threads exist. Different OSes provide threading libraries to make them available. The next version of C++, so called C++0x, is going to make a thread library standard. If I were to start a multithreaded app today I would go with either boost threads or the threads that were a part of any package I might be using i.e. QT or WxWidgets.
Well, until C++0x gets here, there is no standard way to do threading in C++. You can use whatever facilities your operating system provides. So yes, if you are on a UNIX-like operating system, you can use pthreads. On Windows, you can use the Windows API.
There are 3rd party toolkits out there that attempt to provide a uniform and portable threading API, e.g. boost threads and QT.
It is also not difficult to write your own portable abstraction layer either. We did this because the boost API didn't have everything we needed several years ago (e.g. no way to set priority).
In windows the only way to create threads is using the win32 API. Every library you may have that creates threads on windows eventually uses win32 CreateThread()
QT Contains a nice C++ wrapper around a thread that is cross platform. Usually a good practice is to have a MyThread class that contains all the nitty-gritty details of setting up a thread, checking for error codes, getting it's exit code and the like. The MyThread class would have a pure virtual function called run() which is intended to actually do whatever you'd want the thread to do. The user of the MyThread class is expected to inherit from it and implement run() this way you can isolate the user of the class from the details for actually creating the thread.
MyThead also needs to have a method start() which initiates the thread. The thread would start at some entry point inside the class (this usually needs to be a static method) and then this eventually leads to the user's run() method to be invoked.
Beyond Qt, wxWidgets, Boost and native, OS-provided threading facilities, you can just Google around for thread libraries for C++. They are probably more portable and lightweight as well (if all you are looking for is threading). However, if you have a need for more facilities and the aforementioned libraries provide them, then go ahead and use them. Boost is especially good, it has other facilities as well, but admittedly it's threading library, as Brian Neal said, is limited in some regards.
For your interview, neither boost nor qt is helpful at all. you could just use them as the high level libs and interfaces, and no one would ask you how to use boost or qt in such an interview.
For understanding threading and mutex etc, see a document from http://code.google.com/p/effoaddon/downloads/list, named EffoAddons.pdf.
Raw source
http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/thrd/src/thrd/thrd.cpp,
high level abstract interface
http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/thrd/include/thrd_i.h,
and code support wait and signal
http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/queue/include/iqb_ops_i.h.
you'd better study something underlying, not just high level interfaces, though write C++.
I've seen several 'real-world' implementations of threading using C++, and they have all been implemented by someone's writing a Thread class to wrap the underlying O/S API
For example, on Windows there's a CreateThread API: your Thread class would pass its this value to the void* lpParameter parameter of the CreateThread API; and your LPTHREAD_START_ROUTINE, which you implement as a private static method of the Thread class, then needs to cast void* back to Thread*' in order to get theThread` instance.