Wednesday, August 15, 2012

Boost Threading XCode + Boost Library Example

To do threading in boost you need to have compiled the Boost Libraries ./b2
The libraries are stored in /[boost install path]/stage/lib folder

Make sure you set the "Header Search Path" in XCode to /usr/local/include then add all the "dylib" files to your project.

.dylib = dynamic library files
.a = static library files

Here's a really good reference for using dynamic libraries with xcode Dynamic Library Reference

Navigate to the project Settings click on target and where it says "Link Binaries with Libraries" add your library files there, be sure to add all of them.

  

Now comes time for the coding.

In main.cpp add the following code, you should be able to run it.
 #include <iostream>  
 #include <boost/thread.hpp>  
 using namespace boost;  
 using namespace boost::this_thread;  
 using namespace std;  
 // Global function called by thread  
 void GlobalFunction()  
 {  
   for (int i=0;i<10;++i)  
   {  
     cout << i << "Do something in parallel with main method." << endl;  
     boost::this_thread::yield();  
   }  
 }  
 void GlobalThreadTest()  
 {  
   boost::thread t(&GlobalFunction);  
   for (int i = 0; i<10; ++i) {  
     cout << i << "Do something in main method. " << endl;  
   }  
 }  
 int main(int argc, const char * argv[])  
 {  
   GlobalThreadTest();  
   return 0;  
 }  







No comments:

Post a Comment