Create two C++ programms using the Boost library for a shared memory example

November 19, 2012

This tutorial shows how you can use the Boost library for C++ programms under Ubuntu
You will learn how you can write a shared memory programm that uses semaphores for synchronization

First you have to install the Boost library for Ubuntu
sudo apt-get install libboost-all-dev

Then you can check if Boost works correctly by creating a file “main.cpp” with the following code:

For compiling and linking you have to navigate into the folder with your “main.cpp” file and execute:

You should see the Boost Version Number in your terminal

Now that we know that boost works just right, we can write the first
file containing a struct of a Semaphore

//put the first programm in another file called “process1.cpp”

the second programm called “process2.cpp” reads the value programme2
wrote and overwrites this value with the square root of the original value

Now compile – link and execute

The two programms should print some text and numbers.

For some more suffisticated Examples click the link below:
Synchronization mechanisms

Tags: ,

Leave a Comment

You must be logged in to post a comment.