Slow thread creation on Windows

I updated the crunching app in a multi-threaded program using C ++ 11 tools. It works fine on Mac OS X but does not use multithreading on Windows (Visual Studio 2013). Using the following toy program

#include <iostream>
#include <thread>

void t1(int& k) {
    k += 1;
};

void t2(int& k) {
    k += 1;
};

int main(int argc, const char *argv[])
{
    int a{ 0 };
    int b{ 0 };

    auto start_time = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 10000; ++i) {
        std::thread thread1{ t1, std::ref(a) };
        std::thread thread2{ t2, std::ref(b) };
        thread1.join();
        thread2.join();
    }
    auto end_time = std::chrono::high_resolution_clock::now();
    auto time_stack = std::chrono::duration_cast<std::chrono::microseconds>(
        end_time - start_time).count();
    std::cout << "Time: " << time_stack / 10000.0 << " micro seconds" <<
        std::endl;

    std::cout << a << " " << b << std::endl;

    return 0;
}

I found that it takes 34 microseconds to start a thread on Mac OS X and 340 microseconds for Windows. Am I doing something wrong on the Windows side? Is this a compiler problem?

+4
source share
2 answers

Not a problem with the compiler (and, strictly speaking, a problem with the operating system).

, - . Windows ( Linux clone).
, . , , . , , . , , ( 20 !).

, , .

20 000 , , . ( ) , "" , , . .
, , (, -), . ( ) , , 1 64 , (- ).

, , , 10 , , 3 . ( ), .

+7

Visual ++ Concurrency Runtime (MS specific) std.thread. Concurrency Runtime/function ( ). , std.thread, , ConcRT.

(, , ) - , , , , -. std::thread, ; (, ).

EDIT:

+1

All Articles