vendredi 1 juillet 2016

Threading Trouble

I'm trying to learn some threading in C++11 and am having some issues. Below is my source:

#include <iostream>
#include <thread>
#include <ctime>

using std::thread;
using std::cout;

typedef unsigned int uint;

double diffClocks(const clock_t start, const clock_t end);
void worker(const uint start, const uint end);

void foo() { return; }

int main(int argc, char* argv[]) {

    // Control
    clock_t start = clock();

    uint x = 0;
    for(uint i = 0; i < 2000000; i++) {
        x += i;
    } // END for(i)

    clock_t end = clock();
    cout << "Control: " << diffClocks(start, end) << '\n';
    // END Control

    // Test
    start = clock();

    //thread one(foo);

    //one.join();
    thread t1(worker, 0, 1000000);
    thread t2(worker, 1000001, 2000000);

    t1.join();
    t2.join();

    end = clock();
    cout << "Test: " << diffClocks(start, end) << '\n';
    // END Test

return 0;
} // END int main()

double diffClocks(const clock_t start, const clock_t end) {
return ( (double)(end - start) / (double)CLOCKS_PER_SEC );
} // END double diffClocks()

void worker(const uint start, const uint end) {
    uint x = 0;
    for(uint i = start; i < end; i++) {
        x += i;
    } // END for(i)

return;
} // END worker

Compiled w/ g++ -std=c++11

Error message: /tmp/cc2bWP5A.o: In function std::thread::thread<void (&)()>(void (&)())': test.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x7d): undefined reference topthread_create' collect2: error: ld returned 1 exit status

What is going on?

Aucun commentaire:

Enregistrer un commentaire