Is there a latest example of using mutex under boost 1.48.0?

10,262

Check this example (boost::mutex usage is presented in Resource::use()):

#include <boost/thread.hpp>
#include <boost/bind.hpp>

class Resource
{
public:
    Resource(): i(0) {}

    void use()
    {
        boost::mutex::scoped_lock lock(guard);
        ++i;
    }

private:
    int i;
    boost::mutex guard;
};

void thread_func(Resource& resource)
{
    resource.use();
}

int main()
{
    Resource resource;
    boost::thread_group thread_group;
    thread_group.create_thread(boost::bind(thread_func, boost::ref(resource)));
    thread_group.create_thread(boost::bind(thread_func, boost::ref(resource)));
    thread_group.join_all();
    return 0;
}
Share:
10,262
derekhh
Author by

derekhh

Currently I am a software engineer at Snap Inc. Previously I've been working at Microsoft Bing for almost four years. During my years at Microsoft I was the primary back-end algorithm developer or tech lead for many features related to natural language processing and machine learning. I've designed and implemented many core algorithms that powered Bing's conversational experience, question answering and entity carousel. Prior to joining Microsoft, I've obtained my Ph.D. from the Hong Kong University of Science and Technology. My research was primarily on the theme of sensor-based human activity recognition. Throughout my PhD years, I've published around 20 papers in top conferences and journals. I was also a winner of the Microsoft Research Fellowship in the year 2009. I also enjoy competitive programming a lot. I was a regular contestant in programming contests like acm/icpc, Google Code Jam and TopCoder Open. I've also won awards and top prizes from these competitions. Google Scholar Page: https://scholar.google.com/citations?user=Ks81aO0AAAAJ&amp;hl=en Specialties: machine learning, data mining, algorithms, programming

Updated on August 15, 2022

Comments

  • derekhh
    derekhh over 1 year

    Most of the examples I've found on the web are outdated, using boost::mutex which I've failed to declare either including or . Is there any clear example of how to use boost::mutex in ver 1.48.0? The tutorials in Chapter 27 (Threads) are extremely unclear and do not provide any code samples.