This is a mirror of official site: http://jasper-net.blogspot.com/

How to get started using Boost threads

| Tuesday, November 8, 2011
Introduction

This post aims to be an accessible step-by-step introduction to helping beginners get set up with the Boost threads in Visual Studio environments for the first time. Like with many technical subjects, there seems to be a great deal of information out there that tells you a lot but does not actually show you much! This article contains no in-depth discussions on how to use Boost threads in all their different guises. That's for another time or place. What this article (hopefully) does is help you get up and running with Boost threads minus any compiler whinges, which for me at least is often the hardest part.
A simple boost::thread example

A nice introduction to boost thread programming exists over at Gavin Baker’s “antonymn” page which I will take the liberty of reproducing here:

#include <iostream> 
#include <boost/thread.hpp>  
#include <boost/date_time.hpp>      
     
void workerFunc() 

    boost::posix_time::seconds workTime(3);         
    std::cout << "Worker: running" << std::endl;   
     
    // Pretend to do something useful... 
    boost::this_thread::sleep(workTime);         
    std::cout << "Worker: finished" << std::endl; 
}   
     
int main(int argc, char* argv[]) 

    std::cout << "main: startup" << std::endl;         
    boost::thread workerThread(workerFunc);   
     
    std::cout << "main: waiting for thread" << std::endl;         
    workerThread.join();   
     
    std::cout << "main: done" << std::endl;         
    return 0; 

This example code creates a boost thread object, passes it an example worker function and exits the thread when complete.  This simple example I use as a means of validating the successful setup of the Boost thread library.  The following sections describe the preliminary steps that will be necessary to run this simple example. 


Read more: Codeproject
QR: StartingBoostThreads.aspx

Posted via email from Jasper-net

0 comments: