Post

std::for_each、std::mem_fn 备忘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
## include <iostream>
## include <thread>
## include <vector>
## include <algorithm>
## include <functional>

int main() {
    std::vector<std::thread> threads;
    for (int i = 0; i < 10; i++) {
        threads.emplace_back([i] {
            for (int j = 0; j < 3; j++) printf("%d\n", i);
        });
    }
    std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
    return 0;
}
This post is licensed under CC BY 4.0 by the author.