Showing posts with label boost. Show all posts
Showing posts with label boost. Show all posts

Sunday, April 10, 2011

Begining multithreaded programming in boost

Hi,

With boost dev environment successfully installed, I now am embarking on the path to multi threaded nirvana.

For starters am going to paste the most basic multi threaded code here and the instructions how to build it.

 #include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>


using namespace std;

void workerFunc()
{

cout<<"workerFunc Started"<<endl;
boost::posix_time::seconds sec(3);
boost::this_thread::sleep(sec);
cout<<"workerFunc Finished"<<endl;
}

int main ( int argc, char * argv[])
{

cout<<"main started"<<endl;
boost::thread workerThread(workerFunc);
cout<<"main : waiting for workerThread"<<endl;
workerThread.join();
cout<<"main finished"<<endl;
return 0;

}
The code posted above is the most basic multi threaded code in boost, I will first explain the code and then post the instructions on how to compile it.

The code comprises of two functions.

1. workerFunc
2. main

We will be discussing only those lines which are relevant to threading.

In the workerFunc function two lines are of interest.

1. boost::posix_time::seconds sec(3)
2. boost::this_thread::sleep(sec)

In the first line a time construct named sec is defines with 3 as its value, it is defined in boost/date_time.hpp

The second line makes use of sleep function from boost::this_thread, it pause the execution of the current thread for the time provided as an argument to the sleep function. The handle/reference to the current thread is provided by boost::this_thread.

In the main function , again there are two lines which are of interest.

1. boost::thread wokerThread(workerFunc)
2. workerThread.join()

Line 1 creates a thread named workerThread and passes the name of the function to be executed in the newly created thread.

Line 2 uses the join function to instruct the main thread to wait for the new thread to finish execution.

Now , to compile the program the command is

g++ -o appName main.cpp -L /path/to/boost/lib -lboost_thread

g++ is the c++ compiler bundled with gcc, -0 flag tells the compiler to generate the executable with the supplied name, in our case appName, -L instructs the compiler to add the provided path to its search directories for libraries and finally -l flag is immediately followed by the library name.

The method we have used to supply the function to be executed as thread is not the only one, we can have thread functions which take arguments, have a class with thread function inside it etc etc.

Will explain other methods of creating and using threads using boost libraries as i delve deeper into the library.

Regards,
Ishan

Installing boost on ubuntu - 2

Hi,

In the previous post i described the method to install boost library on ubuntu.
It seems that it installed the header only version of the library.
To get the complete thing installed the command is :

apt-get install libboost-all-dev

And you will be good to go.

Regards,
Ishan

Saturday, April 9, 2011

installing boost on ubuntu 10.10 netbook remix

Hi,

After setting up boost library on my winxp machine, i wanted to install boost on my new netbook running ubuntu netbook remix.

I tried building the library from source as i did on windows but kept running on to some build errors.

Then i went to the traditional way to install from ubuntu packages, but there were some unmet dependency problem which i was unable to solve, then after a bit of searching around i got the exact package name to download.

apt-get install libboost-dev libboost-doc

This did the trick, but it installed the 1.4.2 version of the boost library ,current version being 1.4.6.

Will trying building some cpp code and get back with the results.

Regards,
Ishan

Tuesday, April 5, 2011

Using boost static libraries with CodeBlocks

Hi,

Since the past two days , i was trying to run the most trivial multi threading code snippet using boost thread library.

And was unsuccessful, but failure is not always negative , it provided me with the matter for a new blog post to share :)

The issue i was facing was , when i built the project using dynamic boost libraries it worked fine, but as i didn't wanted to have to provide the libraries separately, i tried building the project with statically linked libraries, but no matter what i tried, build always failed with unreferenced symbol error.

Tried re downloading the boost code and recompiling it after a detailed study of bjam build option, but everything failed, then i found , hidden under the pile of junk on the internet a single line of code , and everything was working.

I'll stop being poetic and post the code below.

#define BOOST_THREAD_USE_LIB

Just add this line at the top of the source file using the library and u'll be fine.

As it turned out, the problem was with codeblocks build system, the headers being used were expecting the library to get linked dynamically , atleast this is what i was able to under stand.

So, now i can move along on an adventurous voyage of exploring the boost underworld.
Will keep you all posted.

Regards,
Ishan.

Thursday, March 31, 2011

Building Boost for codeblocks

Hi,

Boost Libraries are the best example of collaborative opensource development. But this post is not about Boost, its about installing Boost libraries for "CodeBlocks" , a free and opensource C++ IDE.

I was trying to Get the library work from the g++ toolset for boost , but was getting an error regarding g++ location, Then after many wasted hours i reinstalled CodeBlocks on C drive itself. initially it was in "program files" folder.

It worked, The libraries are being built as i write this post, the problem was with the "bjam" utility, it was having problems processing the path with spaces.

Regards,
Ishan