复制 #include <pthread.h>
pthread_create (thread, attr, start_routine, arg)
复制 #include <thread>
std::thread nth(&ctr::Class::func, this); // 创建运行对象成员函数的线程
std::thread nth(func, "HELLO");
复制 #include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
void f1(int n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread " << n << " executing\n";
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void f2(int& n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread 2 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int main()
{
int n = 0;
std::thread t1; // t1 is not a thread
std::thread t2(f1, n + 1); // pass by value
std::thread t3(f2, std::ref(n)); // pass by reference
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join();
t4.join();
std::cout << "Final value of n is " << n << '\n';
}
复制 #include <iostream>
#include <unistd.h>
#include <thread>
#include <unistd.h>
using namespace std;
class A {
public:
void f(string str) {
cout << "A::f() " << str << endl;
sleep(3);
}
void fn() {
cout << "A::fn()" << endl;
sleep(3);
}
};
void f(string str) {
cout << "f(string str)" << str << endl;
sleep(3);
}
void fn() {
cout << "fn()" << endl;
sleep(3);
}
int main() {
A a;
std::thread nth(f, "Hello"); // 有参数的方法
sleep(1);
std::thread nfth(fn); // 无参方法
sleep(1);
std::thread nfthc(&A::f, &a, "hello"); // 有参类方法
while (true) {}
return 0;
}
/** Output
f(string str)Hello
fn()
A::f() hello
*/
复制 // constructing threads
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector
std::atomic<int> global_counter (0);
void increase_global (int n) { for (int i=0; i<n; ++i) ++global_counter; }
void increase_reference (std::atomic<int>& variable, int n) { for (int i=0; i<n; ++i) ++variable; }
struct C : std::atomic<int> {
C() : std::atomic<int>(0) {}
void increase_member (int n) { for (int i=0; i<n; ++i) fetch_add(1); }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "increase global counter with 10 threads...\n";
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(increase_global,1000));
std::cout << "increase counter (foo) with 10 threads using reference...\n";
std::atomic<int> foo(0);
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(increase_reference,std::ref(foo),1000));
std::cout << "increase counter (bar) with 10 threads using member...\n";
C bar;
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(&C::increase_member,std::ref(bar),1000));
std::cout << "synchronizing all threads...\n";
for (auto& th : threads) th.join();
std::cout << "global_counter: " << global_counter << '\n';
std::cout << "foo: " << foo << '\n';
std::cout << "bar: " << bar << '\n';
return 0;
}
复制 #include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
void thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
int main(int argc, const char *argv[])
{
std::thread threads[5];
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++) {
threads[i] = std::thread(thread_task, i + 1);
}
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t: threads) {
t.join();
}
std::cout << "All threads joined.\n";
return EXIT_SUCCESS;
}
复制 #include <iostream>
#include <thread>
#include <pthread.h>
using namespace std;
void f() {
std::this_thread::sleep_for(std::chrono::seconds(2));
cout << "f(): mid check" << endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
cout << "f(): done" << endl;
}
void fl() {
while (true) {}
cout << "fl() done" << endl;
}
int main() {
std::thread th(f);
thread::native_handle_type handle = th.native_handle();
th.detach();
std::this_thread::sleep_for(std::chrono::seconds(3));
pthread_cancel(handle);
std::thread thfl(fl);
thfl.detach();
handle = thfl.native_handle();
cout << "Handle: " << handle << endl;
pthread_cancel(handle);
std::this_thread::sleep_for(std::chrono::seconds(3));
return 0;
}
/** Output
f(): mid check
Handle: 0
*/
复制 #include <iostream>
#include <thread>
#include <unistd.h>
#include <fstream>
using namespace std;
void f() {
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "f() finish" << endl;
ofstream out("out", ofstream::out | ofstream::trunc);
if (out.is_open()) {
out << "HELLO";
}
out.close();
return;
}
int main() {
std::thread ths[5];
ths[0] = std::thread();
cout << ths[0].joinable() << endl; // 0
// ths[0].join(); // terminate called after throwing an instance of 'std::system_error'; Invalid argument
ths[1] = std::thread(f);
ths[1].detach();
cout << "main() finish" << endl;
return 0;
}
/** Output
0
main() finish
*/
// 没有打印日志和生成文件
/** 将ths[1].detach()改为ths[1].join()后,Output:
0
f() finish
main() finish
*/
// 有生成文件
复制 #include <iostream>
#include <thread>
using namespace std;
void fb() {
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "fb() done" << endl;
}
void f(thread &thd) {
cout << thd.get_id() << endl;
thd.join();
}
void fc(thread thd) {
cout << thd.get_id() << endl;
thd.join();
}
int main() {
thread th1(fb);
thread th2(fb);
cout << th1.joinable() << endl;
cout << th2.joinable() << endl;
f(th1);
// fc(th1); // ERROR: 由于thread禁用了赋值操作符,这里编译时会报错
// 这里的th1是不能拷贝复制给形参的
fc(std::move(th2));
cout << th1.joinable() << endl;
cout << th2.joinable() << endl;
return 0;
}
/** Output
1
1
140432286369536
fb() done
fb() done
140432277976832
0
0
*/
// 一个值得注意的地方,如果main函数改为:
/**
int main() {
thread th1(fb);
f(th1); // 更改了这句的顺序
thread th2(fb);
cout << th1.joinable() << endl;
cout << th2.joinable() << endl;
fc(std::move(th2));
cout << th1.joinable() << endl;
cout << th2.joinable() << endl;
return 0;
}
*/
// 那么输出为
/**
140007787820800
fb() done
0
1
140007787820800
fb() done
0
0
*/
// 有意思的是, 这里的线程ID相同, 有时间再细究, 编译器的优化?
复制 #### example 1
$ cat 07.cpp
#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
int main() {
thread th([]() {
int i = 0;
while (true) {
cout << "- " << i++ << " -" << endl;
sleep(2);
}
});
th.join();
return 0;
}
$ g++ 07.cpp -lpthread && ./a.out
- 0 -
- 1 -
#### example 2
$ cat 13.cpp
#include <iostream>
#include <future>
#include <thread>
#include <chrono>
using namespace std;
int main() {
std::promise<bool> p;
thread th([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
cout << "set value true" << endl;
p.set_value(true);
});
th.detach();
cout << "th detach" << endl;
p.get_future().wait();
cout << "exit" << endl;
return 0;
}
$ g++ 13.cpp -lpthread -std=c++11 && ./a.out
th detach
set value true
exit
复制 g++ -std=c++11 -pthread 002.cpp # g++-4.8