Thread: A few questions on thread/s ...

  1. #1
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877

    A few questions on thread/s ...

    Hello all,

    I have a few questions on using threads.

    1st Question

    Whenever I write some code using threads and compile it with gcc (v 4.9), I have to include a linker switch like this:

    Code:
    -lpthread
    Or else I get the error "undefined reference to 'pthread_create'":

    Code:
    // Probably not great code, just want to demonstrate the problem
    
    #include <iostream>
    #include <thread>
    #include <mutex>
    
    std::mutex cout_lock;
    
    int main () {
        int num = 0;
        auto countToTen = [] (int& n)->void {
            for (int i = 0; i < 10; i++) {
                n += 1;
                std::lock_guard<std::mutex> lock(cout_lock);
                std::cout << i << "\n";
            }
        };
    
        std::thread t1(countToTen, std::ref(num));
    
        cout_lock.lock();
        std::cout << "Waiting to finish.\n";
        cout_lock.unlock();
    
        t1.join();
        std::cout << "Finished counting, final value of iterator: " << num << "\n";
    
        return 0;
    }
    
    // undefined reference to 'pthread_create'
    I was wondering if this error (without the linking command) was expected behavior? Are threads just a special case in this regard (I can't remember needing to link for vanilla std namespace classes before), or is there something I'm potentially missing?

    2nd Question

    Since I'll also be using some boost libraries (mostly asio, maybe bind), I was wondering what the right thing to do in regards to which version of similar classes to use, in those cases where both libraries provide classes for the same thing?

    Various tutorials seem to indicate you should always use boost classes for maximal compatibility with boost, but they could be biased, so I thought I would ask. (I know it's general though).

    Thanks!!!
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think this answers your first question : c++11 - In g++ is C++ 11 thread model using pthreads in the background? - Stack Overflow

    As for your second, I would prefer STL stuff over Boost stuff. My logic is, STL stuff is more "official" C++.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    @ MJ - Thanks, that makes sense. I'm glad I don't have to rebuild gcc or anything. I'm curious about the implications on portability between operating systems, I'll have to do some research (so far I've only learned how to use threads, synchronization, etc). I don't think the code itself would need to change though, which is cool, I guess that's the whole idea lol.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C programming. Questions involved in thread.
    By Chris Robbins in forum C Programming
    Replies: 25
    Last Post: 12-29-2013, 08:09 PM
  2. Replies: 3
    Last Post: 11-20-2011, 12:01 AM
  3. Mutex, Cond and Thread questions (pthread, linux)
    By thebearot in forum C Programming
    Replies: 14
    Last Post: 04-23-2010, 12:10 PM
  4. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  5. 3 questions, one thread....
    By Blizzarddog in forum C++ Programming
    Replies: 15
    Last Post: 10-16-2002, 11:23 AM