Can any body help me figure it out? Thanks.

g++ boo_t.cpp -L../resource/boost_1_35_0/bin.v2/libs/thread/build/gcc-4.1.2/release/threading-multi/libboost_thread-gcc41-mt-1_35.so.1.35.0 -Lpthread boo_t.cpp: In function ‘void increment_count()’: boo_t.cpp:14: error: reference to ‘mutex’ is ambiguous boo_t.cpp:10: error: candidates are: boost::mutex mutex /usr/include/boost/thread/mutex.hpp:35: error: class boost::mutex code: #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <iostream> using namespace std; using namespace boost; int count = 0; boost::mutex mutex; void increment_count() { boost::mutex::scoped_lock lock(mutex); std::cout << "count = " << ++count << 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(); "boo_t.cpp" 24L, 480C 1,1 Top c -- Peisheng Wang Software Engineer iZENEsoft (Shanghai) Co., Ltd Room 601 Marine Tower, No. 1 Pudong Ave. Tel:86-21-68860698-114 Fax:86-21-68860699 Company Website:<a target="_blank" href="http://www.izenesoft.com/">www.izenesoft.com</a> <a href="../../../../index.php?menuaction=felamimail.uicompose.compose&send_to=Wmhlbi5ZdUBNb3JnYW5TdGFubGV5LmNvbQ==">Zhen.Yu@MorganStanley.com</a>

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

Kevin Martin wrote:
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.
...
boost::mutex mutex;
void increment_count() { boost::mutex::scoped_lock lock(mutex);
Yes. Specifically, the line above could be interpreted as the declaration of a function lock() accepting an unnamed mutex by value and returning a boost::mutex::scoped_lock (the "most vexing parse"). Kevin's suggestion is probably best to keep things straight for the reader as well as for the compiler. It might also work to use assignment-style initialization? I haven't tried it for this case. boost::mutex::scoped_lock lock = mutex; I mention that more as a tactic for future function-vs.-object declaration ambiguities than for this particular one.
participants (3)
-
Kevin Martin
-
Nat Goodspeed
-
Peisheng Wang