Boost logo

Boost Users :

From: Kevin Martin (kev82_at_[hidden])
Date: 2008-05-08 09:30:48


On 8 May 2008, at 04:09, Peisheng Wang wrote:

You are importing the namespace boost, so mutex is both a variable
name and a type name, the compiler gets confused. You have a similar
problem with std::count. Changing the variable names to amutex and
acount fixes the problem. The below code compiles and runs for me.

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

using namespace std;
using namespace boost;

int acount = 0;
boost::mutex amutex;

void increment_count()
{
    boost::mutex::scoped_lock lock(amutex);
    std::cout << "count = " << ++acount << std::endl;
}

int main(int argc, char* argv[])
{
     boost::thread_group threads;
     for (int i = 0; i < 10; ++i)
threads.create_thread(&increment_count);
     threads.join_all();
     return 0;
}

I'm not quite sure your compiler line is correct either. I don't think
you are supposed to be linking with libpthread. You should instead be
linking with libboost_thread. My compiler line was:

g++ test.cc -I/opt/local/include -L/opt/local/lib -lboost_thread-mt

Thanks,
Kevin Martin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net