0

Kindly see the code: What am I missing. I am trying to test the boost threads. The hpp files I have tried it works fine in a lot of cases but here I guess I am missing something.

#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

The terminal output is below:

$ 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
Hiesenberg
  • 345
  • 1
  • 4
  • 11
  • 2
    Put your `main` function in a cpp file (vs a header file). Your `main` function also isn't returning anything... – Buddy Jun 06 '15 at 05:34
  • 1
    @Buddy: for better or worse, the C++98 standard blessed the concept of 'if processing reaches the end of `main()` without an explicit `return` statement, it is equivalent to ending the function with `return 0;`'. C99 followed suit. I'm not convinced it was a good decision, and don't exploit it in my code, but that's the way it is. See also [What should `main()` return in a C or C++ program?](http://stackoverflow.com/questions/204476/) – Jonathan Leffler Jun 06 '15 at 05:52
  • Cool, I learned something new today. Thanks! – Buddy Jun 06 '15 at 14:31

0 Answers0