Boost thread not working

This is a discussion on Boost thread not working within the C++ Programming forums, part of the General Programming Boards category; Hi I am having trouble with the thread library in the boost package. After I installed it and set up ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    14

    Boost thread not working

    Hi

    I am having trouble with the thread library in the boost package. After I installed it and set up the directories (I am using Visual c++ express 2005 btw) I tried this test program:
    Code:
    #include <boost/thread.hpp>
    
    int main()
    {
    	return 0;
    }
    However when I compiled this, I got a link error "LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc80-mt-gd-1_33_1.lib'"

    I checked inside the lib folder and there wasn't a file called libboost_thread-vc80-mt-gd-1_33_1.lib' but there was a file called boost_thread-vc71-mt-gd-1_33_1.lib. So I made a copy of the existing file and renamed it to libboost_thread-vc80-mt-gd-1_33_1.lib. After doing this the above program compiled.

    Next I tried this program:
    Code:
    #include <boost/thread.hpp>
    #include <iostream>
    
    int main()
    {
        boost::thread_group group;
        std::cout<< "hello world";
        return 0;
    }
    This resulted in the error:
    Code:
    msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "class std::basic_ostream<char,struct 
    std::char_traits<char> > & __cdecl std::operator<<<struct std::char_traits<char> >(class 
    std::basic_ostream<char,struct std::char_traits<char> > &,char const *)" 
    (??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D
    @std@@@0@AAV10@PBD@Z) already defined in Boost Threads.obj

    So I (trying to get anything to work) deleted the "std::cout<<"hello world" line and the program compiled.

    Finally I tried this program:

    Code:
    #include <boost/thread.hpp>
    #include <iostream>
    
    
    void testFunction() 
    {
    }
    
    int main()
    {
        boost::thread_group group;
        group.create_thread(&testFunction);
        group.join_all();
        for(volatile long i = 0; i < 200000000; i++); //delay
        return 0;
    }
    This compiled correctly, but when run, it resulted in an assert error:
    Code:
    Assertion failed: res == WAIT_OBJECT_0, file D:\Program Files\Boost cpp 
    Libraries\libs\thread\build\..\src\once.cpp, line 168
    I removed the group.join_all() line and the program ran correctly, but the program outputted:
    Code:
    Assertion failed: res == WAIT_OBJECT_0, file D:\Pro
    It appears that the program still failed, but it quits before the assert has finished. Removing the delay line results in no message, while setting the delay to 800000000 results in the same assertion failure as before

    I am completely stuck with this one and would appreciate any ideas!
    (oh and sorry for my huge first post )

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Sounds like you might need the boost vc-8_0 toolset
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    14
    Quote Originally Posted by JaWiB
    Sounds like you might need the boost vc-8_0 toolset
    I tried reinstalling the boost libraries using that tool (i didn't use that last time). To build it, i placed BJam inside the c:/windows directory and ran in the command prompt:

    bjam "-sTOOLS = E:\install files\programming\c++\boost_1_33_1\vc-8_0-tools.jam" --prefix="D:\cpp libraries\Boost"

    It all installed but it didn't fix anything. Did I do something wrong?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I installed boost last night and am using the same compiler. Took me a little while to figure out just how to set up the project to use the libraries. After creating a console program, I had to add to boost directories to project settings:

    include directory is D:\Boost\include\boost-1_33_1
    lib directory is D:\Boost\lib

    I compiled and ran your last program and did not get any compile or runtime errors.

    I believe sTOOLS should name the compiler you want to use: Here is a batch file I created so that I don't have to type all that every time
    Code:
    call d:\vcvars32.bat
    set VC80_ROOT="D:\Program Files\Microsoft Visual Studio 8\VC"
    set _SCL_SECURE_NO_DEPRECATE=1
    bjam "--prefix=d:\boost" "-sTOOLS=vc-8_0" install >msgs.txt

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    14
    Yay its working!!!
    Thanks Anchient Dragon as although your bat file didn't fix the problem, it lead me on the right direction to find the problem. The actual problem was that the Microsoft Platform SDK was not set up correctly, so although it worked perfectly inside the Visual c++ express IDE, it didn't work from the command line.

    Anyway, I'll list the steps it took for me to get Boost installed correctly for Visual C++ express 2005 so that if anyone else has similar problems, it might help them.

    1. Download the boost package from http://sourceforge.net/project/showf...ackage_id=8041 . Any of the packages would work, but the .exe one is the easiest to use for windows.

    2. Download the boost.jam package from http://sourceforge.net/project/showf...ckage_id=72941 . Any of the packages with an Architecture of i386 would work, but the zip file would be the easiest.

    3. Unzip the boost.jam download. It should be named something like boost-jam-3.1.13-1-ntx86.zip if you chose the .zip package. Copy the bjam.exe file in the zip file to the c:\windows folder.

    4. Install the Microsoft Platform SDK package. You have two choices:
    a) With a web install from http://www.microsoft.com/downloads/d...displaylang=en
    Choose the PSDK-x86.exe download which is near the bottom of page unless you have a 64 bit processor.

    b) With an iso image from http://www.microsoft.com/downloads/d...displaylang=en
    Then burn the iso onto a cd using your favourite cd burning program (like http://www.deepburner.com/?r=download).

    The web install is easier, but you don't get to keep the install files so if you need to reinstall it you have to download it again.

    5. Set up Visual C++ express for SDK
    I'm assuming it was installed at its default location (for me it was C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2) If not just adjust all the paths to your settings.

    a) Open the environmental variables dialog (control panel/system/advanced/environmental variables). Then find the INCLUDE system variable, edit it and add to the end of it ";C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include" without quotes (the ';' is used to separate different entries). If there is no INCLUDE system variable, create one and set its value to "C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include".

    Do the same for the LIB system variable but use the path "C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib".

    b) Do steps 3 to 5 on the website http://msdn.microsoft.com/vstudio/ex...alc/usingpsdk/

    6) Extract the boost libraries (or if you chose the .exe file in step 1, just launch it and click extract).

    7) Open the boost folder. It should be called something like boost_1_33_1

    8) Create a new text file and paste this in
    Code:
    bjam "--prefix=c:\cpp libraries\boost" "-sTOOLS=vc-8_0" install > msgs.txt
    The directory after prefix is where the libraries are installed. Change this to where you want it. Note that the complete libraries take about 1GB of disk space.
    The rename the file boost.bat

    9) Open a command prompt at the folder your currently at.
    eg start - run - cmd - cd "c:/installs/boost_1_33_1" or whatever.

    10) type boost.bat and hit enter!
    This will take about 10 minutes and during that you will see hundreds of warning like
    Code:
    cl : Command line warning D9035 : option 'Og' has been deprecated and will be re
    moved in a future release
    This is normal. If you get any failures but, something has gone wrong!

    11) Once finished, open visual c++ express, go to tools-options-projects and solutions-VC++ directories. Go to include files and add a new entry for "C:\cpp libraries\boost\include\boost-1_33_1" (change to suit your install directory). Then create a new entry in the library file section for "C:\cpp libraries\boost\lib" (change to suit your install directory).

    12) Test if it is working by using this code. It uses most of the major libraries (threads, pointers, spirit, lambda, functions, date, static_assert, tokenizer)

    Code:
    #include <stdio.h>
    #include <tchar.h>
    #include <iostream>
    #include <vector>
    
    /* Boost libraries generate a lot of warnings.
       #pramga warning is used to disable them
    */
    #pragma warning(push, 0)
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/algorithm.hpp>
    #include <boost/lambda/if.hpp>
    
    #include <boost/Function.hpp>
    #include <boost/tokenizer.hpp>
    
    #include <boost/scoped_ptr.hpp>
    
    #include <boost/thread.hpp>
    
    #include <boost/spirit/core.hpp>
    #include <boost/spirit/actor/push_back_actor.hpp>
    
    #include <boost/assign/std/vector.hpp>
    
    #include <boost/static_assert.hpp>
    #include "boost/date_time/gregorian/gregorian.hpp"
    #pragma warning(pop)
    using namespace boost::lambda;
    using namespace boost::assign;
    using namespace boost::spirit;
    
    void calculateDaysAlive(void);
    void testFunction(void);
    int adder(int x, int y);
    void printNumber(double number);
    
    void printNumber(double number) {
        std::cout << number << std::endl;
    }
    
    
    
    int adder(int x, int y) {
        return x + y;
    }
    
    int currentNumber;
    void testFunction() 
    {
        int x = currentNumber++;
        for (int i = 0; i < 30; i++) {
            std::cout << x;
        }
    }
    
    int main()
    {
        // testing <boost/static_assert.hpp> ####################################
        // note that for this macro, it is good to use double brackets
        BOOST_STATIC_ASSERT((4 > 3)); // will do nothing
        //BOOST_STATIC_ASSERT((4 < 3)); this line would cause a compile error
    
    
        // testing <boost/thread.hpp> ########################################
        std::cout << "-----thread stuff-----\n";
        boost::thread_group group;
        for (int i = 0; i < 10; i++) {
            group.create_thread(&testFunction);
        }
        group.join_all();
        std::cout << "\n\n";
    
        // testing boost date/time libraries ##############################
        std::cout << "-----date/time stuff-----\n";
        calculateDaysAlive();
    
        // testing boost/function.hpp #####################################
        std::cout << "-----function.hpp stuff-----\n";
        boost::function<int (int a, int b)> f = adder;
        std::cout << f(12,23) << std::endl; // 35
    
        // testing lambda.hpp etc #############################################
        std::cout << "-----lambda stuff-----\n";
        int a = 3; int b = 4; int c = 5;
        std::cout << (_1 + _2) (a, b) << std::endl; // 7
        //std::cout << (_1 + _2) (3, 4); compile error, you cannot use constants
        // when using lambda
    
        std::cout << bind(&adder, a, _1) (c) << std::endl; // prints 8
        boost::function<int (int a)> g = bind(f, a, _1); // basically g(x) = f(3,x)
        std::cout << g(10) << std::endl; // prints 13
    
    
        // testing <boost/tokenizer.hpp> #################################
        std::cout << "-----tokenizer stuff-----\n";
        std::string s = "This is,  a test";
        boost::tokenizer<> tok(s);
        for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
            std::cout << *beg << "\n";
        }
    
        // testing <boost/scoped_ptr.hpp> ###################################
        std::cout << "-----pointer stuff-----\n";
        boost::scoped_ptr<int> intPointer(new int);
        *intPointer = 3;
        std::cout << *intPointer << std::endl;  // prints 3
        // no need to call delete intPointer
    
    
        // test boost/assign... ####################################
        std::cout << "-----assign stuff-----\n";
        std::vector<int> values;
        values += 1,2,3,4,5,6,7,8,9;
        values += 8,7,6,5,4,3,2,1;
        std::vector<int>::iterator start = values.begin();
        std::vector<int>::iterator end = values.end();
        std::for_each(start, end, std::cout << _1 << ", "); // more lambda
        std::cout << std::endl;
    
        
        // test boost/spirit ##############################################
        std::cout << "-----spirit stuff-----\n";
        std::string str = "34.2, 1433, 5.443|  12.00";
        std::vector<double> v;
        /* 
        Defines a parser that starts with a real number (real_p) which is
        used in a function call to printNumber ([&printNumber]) and is 
        followed (>>) by any number (*) of sub statements ((substatement))
        that start with a comma (',') and is followed (>>) by a real number (real_p)
        which is pushed onto vector v ([push_back_a(v)]). It is then followed by
        a '|' and another real number that the square of it is printed. It ignores
        spaces between elements (space_p in second argument)
        */
        bool boolean =  parse(str.c_str(),
    
            //  Begin grammar
            (
                real_p[&printNumber] >> *(',' >> real_p[push_back_a(v)])
                >> '|' >> real_p[std::cout << (_1 * _1) << "\n"]
            )
            ,
            //  End grammar
    
            space_p).full;
    
        std::for_each(v.begin(), v.end(), std::cout << _1 << ", ");
        std::cout << std::endl;
    
        std::cin.get();
        std::cin.get();
    	return 0;
    }
    
    //tests <boost/date_time>
    void calculateDaysAlive() {
      
      using namespace boost::gregorian;
      std::string s;
      std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): ";
      std::cin >> s;
      try {
        date birthday(from_simple_string(s));
        date today = day_clock::local_day();
        days days_alive = today - birthday;
        days one_day(1);
        if (days_alive == one_day) {
          std::cout << "Born yesterday, very funny" << std::endl;
        }
        else if (days_alive < days(0)) {
          std::cout << "Not born yet, hmm: " << days_alive.days() 
                    << " days" <<std::endl;
        }
        else {
          std::cout << "Days alive: " << days_alive.days() << std::endl;
        }
    
      }
      catch(...) {
        std::cout << "Bad date entered: " << s << std::endl;
      }
    }
    It should output something similar to:

    Code:
    -----thread stuff-----
    00000001111111111111111022222222222222220111111111111110000002333333333333333320
    44444444444444440235555555555555555320466666666666666664023555555555555553333204
    67777777777777777640238888888888888888320467999999999999999976402388888888888888
    333320467999999999999997777640222204677777776400004666666444
    
    -----date/time stuff-----
    Enter birth day YYYY-MM-DD (eg: 2002-02-01): 2000-01-01
    Days alive: 2423
    -----function.hpp stuff-----
    35
    -----lambda stuff-----
    7
    8
    13
    -----tokenizer stuff-----
    This
    is
    a
    test
    -----pointer stuff-----
    3
    -----assign stuff-----
    1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1,
    -----spirit stuff-----
    34.2
    144
    1433, 5.443,
    The thread test's output depends on the timing of your computer and will change every time. The date test will also change obviously.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost Read/Write Mutexes
    By Zafrir Patt in forum C++ Programming
    Replies: 1
    Last Post: 06-11-2009, 10:32 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 02:17 PM
  4. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 02:04 PM
  5. Exiting a thread from outside the thread ?
    By gopi_tony in forum Windows Programming
    Replies: 1
    Last Post: 11-10-2006, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21