我是靠谱客的博主 灵巧大象,最近开发中收集的这篇文章主要介绍C++ 多线程编程thread,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

c++ reference

编译命令:

g++ -std=c++11 thread.cpp -o thread -lpthread

头文件:

#include <thread>

Thread class:

Member types:
id
//Thread id (public member type )
native_handle_type
//Native handle type (public member type )
Member functions:
(constructor)
//Construct thread (public member function )
(destructor)
//Thread destructor (public member function )
operator=
//Move-assign thread (public member function )
get_id
//Get thread id (public member function )
joinable
//Check if joinable (public member function )
join
//Join thread (public member function )
detach //Detach thread (public member function )
swap //Swap threads (public member function )
native_handle //Get native handle (public member function )
hardware_concurrency [static]
/*Detect hardware concurrency (public static member function )*/

thread:

// thread example
#include <iostream>
// std::cout
#include <thread>
// std::thread
void foo()
{
// do stuff...
}
void bar(int x)
{
// do stuff...
}
int main()
{
std::thread first (foo);
// spawn new thread that calls foo()
std::thread second (bar,0);
// spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...n";
// synchronize threads:
first.join();
// pauses until first finishes
second.join();
// pauses until second finishes
std::cout << "foo and bar completed.n";
return 0;
}

get_id():

// thread::get_id / this_thread::get_id
#include <iostream>
// std::cout
#include <thread>
// std::thread, std::thread::id, std::this_thread::get_id
#include <chrono>
// std::chrono::seconds
std::thread::id main_thread_id = std::this_thread::get_id();
void is_main_thread() {
if ( main_thread_id == std::this_thread::get_id() )
std::cout << "This is the main thread.n";
else
std::cout << "This is not the main thread.n";
}
int main()
{
is_main_thread();
std::thread th (is_main_thread);
th.join();
}

joinable()

// example for thread::joinable
#include <iostream>
// std::cout
#include <thread>
// std::thread
void mythread()
{
// do stuff...
}
int main()
{
std::thread foo;
std::thread bar(mythread);
std::cout << "Joinable after construction:n" << std::boolalpha;
std::cout << "foo: " << foo.joinable() << 'n';
std::cout << "bar: " << bar.joinable() << 'n';
if (foo.joinable()) foo.join();
if (bar.joinable()) bar.join(); // join()之后joinable() 返回:false
std::cout << "Joinable after joining:n" << std::boolalpha;
std::cout << "foo: " << foo.joinable() << 'n';
std::cout << "bar: " << bar.joinable() << 'n';
return 0;
}
//运行结果:
Joinable after construction:
foo: false
bar: true
Joinable after joining:
foo: false
bar: false

detach():

#include <iostream>
// std::cout
#include <thread>
// std::thread, std::this_thread::sleep_for
#include <chrono>
// std::chrono::seconds
void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds endedn";
}
int main()
{
std::cout << "Spawning and detaching 3 threads...n";
std::thread (pause_thread,1).detach();
std::thread (pause_thread,2).detach();
std::thread (pause_thread,3).detach();
std::cout << "Done spawning threads.n";
std::cout << "(the main thread will now pause for 5 seconds)n";
// give the detached threads time to finish (but not guaranteed!):
pause_thread(5);
return 0;
}
//运行结果:
Spawning and detaching 3 threads...
Done spawning threads.
(the main thread will now pause for 5 seconds)
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
pause of 5 seconds ended

sleep_for:

// this_thread::sleep_for example
#include <iostream>
// std::cout, std::endl
#include <thread>
// std::this_thread::sleep_for
#include <chrono>
// std::chrono::seconds
int main()
{
std::cout << "countdown:n";
for (int i=10; i>0; --i) {
std::cout << i << std::endl;
std::this_thread::sleep_for (std::chrono::seconds(1));
}
std::cout << "Lift off!n";
return 0;
}

sleep_until:

// this_thread::sleep_for example
#include <iostream>
// std::cout
#include <iomanip>
// std::put_time
#include <thread>
// std::this_thread::sleep_until
#include <chrono>
// std::chrono::system_clock
#include <ctime>
// std::time_t, std::tm, std::localtime, std::mktime
int main()
{
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
std::cout << "Current time: " << std::put_time(ptm,"%X") << 'n';
std::cout << "Waiting for the next minute to begin...n";
++ptm->tm_min; ptm->tm_sec=0;
std::this_thread::sleep_until (system_clock::from_time_t (mktime(ptm)));
std::cout << std::put_time(ptm,"%X") << " reached!n";
return 0;
}

yield:

// this_thread::yield example
#include <iostream>
// std::cout
#include <thread>
// std::thread, std::this_thread::yield
#include <atomic>
// std::atomic
std::atomic<bool> ready (false);
void count1m(int id) {
while (!ready) {
// wait until main() sets ready...
std::this_thread::yield();
}
for (volatile int i=0; i<1000000; ++i) {}
std::cout << id;
}
int main ()
{
std::thread threads[10];
std::cout << "race of 10 threads that count to 1 million:n";
for (int i=0; i<10; ++i) threads[i]=std::thread(count1m,i);
ready = true;
// go!
for (auto& th : threads) th.join();
std::cout << 'n';
return 0;
}

例子:

#include <iostream>
#include <thread>
#include <chrono>
std::thread::id main_thread_id = std::this_thread::get_id();
void hello()
{
std::cout << "Hello Concurrent Worldn";
if (main_thread_id == std::this_thread::get_id())
std::cout << "This is the main thread.n";
else
std::cout << "This is not the main thread.n";
}
void pause_thread(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds endedn";
}
int main() {
std::thread t(hello);
std::cout << t.hardware_concurrency() << std::endl;//可以并发执行多少个(不准确)
std::cout << "native_handle " << t.native_handle() << std::endl;//可以并发执行多少个(不准确)
t.join();
std::thread a(hello);
a.detach();
std::thread threads[5];
// 默认构造线程
std::cout << "Spawning 5 threads...n";
for (int i = 0; i < 5; ++i)
threads[i] = std::thread(pause_thread, i + 1);
// move-assign threads
std::cout << "Done spawning threads. Now waiting for them to join:n";
for (auto &thread : threads)
thread.join();
std::cout << "All threads joined!n";
}

最后

以上就是灵巧大象为你收集整理的C++ 多线程编程thread的全部内容,希望文章能够帮你解决C++ 多线程编程thread所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部