请看代码:我错过了什么。 我正在尝试测试提升线程。我尝试过的hpp文件在很多情况下工作正常,但在这里我想我错过了一些东西。
#ifndef MULTI
#define MULTI
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h>
class Multi
{
private:
boost::mutex mx_;
int value_;
public:
Multi(int value) : value_(value) { }
int getValue() { return value_; }
void increase()
{
for(int i = 0; i < 3; ++i)
{
{
boost::lock_guard<boost::mutex> lock(mx_);
std::cout << "Thread " << boost::this_thread::get_id()
<< ": " << ++value_ << std::endl;
}
boost::this_thread::sleep(boost::posix_time::millisec(50));
}
}
void divide(int div)
{
for(int i = 0; i < 3; ++i)
{
{
boost::lock_guard<boost::mutex> lock(mx_);
std::cout << "Thread " << boost::this_thread::get_id()
<< ": " << (value_ /= div) << std::endl;
}
boost::this_thread::sleep(boost::posix_time::millisec(50));
}
}
};
int main()
{
Multi m(42);
std::cout << "Main thread " << m.getValue() << std::endl;
boost::thread t1(&Multi::increase, &m); // 1.
boost::thread t2(&Multi::divide, &m, 2); // 2.
t1.join();
t2.join();
std::cout << "Main thread: " << m.getValue() << std::endl;
}
#endif
终端输出如下:
$ g++ -std=c++11 -o Multi Multi.hpp -lboost_system -lboost_thread
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In function `_start':
/build/buildd/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status