【建站服务】专业网站建设-网站设计-网站制作与网站开发-高端品牌企业宣传策划-哈尔滨上往建站-域名申请
作者: 风兰 . 阅读量: 4 . 发表时间:2022-09-21 02:20:25
上往建站提供服务器空间服务商,百度快照排名,网站托管,百度推广运营,致力于设计外包服务与源代码定制开发,360推广,搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。
专业网站建设-网站设计-网站制作与网站开发-高端品牌企业宣传策划-哈尔滨上往建站

当上面的代码被编译和执行时,它会产生下列结果:
$ g++ -Wno-write-strings test.cpp -lpthread -o test.o $ ./test.o main() : creating thread, 0main() : creating thread, 1Thread ID : 0 Message : This is message main() : creating thread, Thread ID : 21 Message : This is message main() : creating thread, 3Thread ID : 2 Message : This is message main() : creating thread, 4Thread ID : 3 Message : This is messageThread ID : 4 Message : This is message
我们可以使用以下两个函数来连接或分离线程:
pthread_join (threadid, status) pthread_detach (threadid)
pthread_join() 子程序阻碍调用程序,直到指定的 threadid 线程终止为止。当创建一个线程时,它的某个属性会定义它是否是可连接的(joinable)或可分离的(detached)。只有创建时定义为可连接的线程才可以被连接。如果线程创建时被定义为可分离的,则它永远也不能被连接。
这个实例演示了如何使用 pthread_join() 函数来等待线程的完成。
当上面的代码被编译和执行时,它会产生下列结果:
main() : creating thread, 0main() : creating thread, 1main() : creating thread, 2main() : creating thread, 3main() : creating thread, 4Sleeping in thread Thread with id : 4 ...exiting Sleeping in thread Thread with id : 3 ...exiting Sleeping in thread Thread with id : 2 ...exiting Sleeping in thread Thread with id : 1 ...exiting Sleeping in thread Thread with id : 0 ...exiting Main: completed thread id :0 exiting with status :0Main: completed thread id :1 exiting with status :0Main: completed thread id :2 exiting with status :0Main: completed thread id :3 exiting with status :0Main: completed thread id :4 exiting with status :0Main: program exiting.
C++ 11 之后添加了新的标准线程库 std::thread,std::thread 在 <thread> 头文件中声明,因此使用 std::thread 时需要包含 在 <thread> 头文件。
之前一些编译器使用 C++ 11 的编译参数是 -std=c++11:
g++ -std=c++11 test.cpp
std::thread 默认构造函数,创建一个空的 std::thread 执行对象。
#include<thread>std::thread thread_object(callable)
一个可调用对象可以是以下三个中的任何一个:
函数指针
函数对象
lambda 表达式
定义 callable 后,将其传递给 std::thread 构造函数 thread_object。
专业网站建设-网站设计-网站制作与网站开发-高端品牌企业宣传策划-哈尔滨上往建站
上往建站提供搭建网站,域名注册,官网备案服务,网店详情页设计,企业网店,专业网络店铺管理运营全托管公司咨询电话,服务器空间,微信公众号托管,网页美工排版,致力于域名申请,竞价托管,软文推广,全网营销,提供标准级专业技术保障,了却后顾之忧,主营:虚拟主机,网站推广,百度竞价托管,网站建设,上网建站推广服务,网络公司有哪些等业务,专业团队服务,效果好。
服务热线:400-111-6878 手机微信同号:18118153152(各城市商务人员可上门服务)
关键词:网站建设,企业网站,网站制作,网页设计,高端网站建设,企业网站制作,网页制作,制作网站,网站设计,高端网页设计,高端网站设计,做网站,自适应网站



c++ 11 之后有了标准的线程库:
#include <iostream>#include <thread>std::thread::id main_thread_id = std::this_thread::get_id();void hello() { std::cout << "Hello Concurrent World "; if (main_thread_id == std::this_thread::get_id()) std::cout << "This is the main thread. "; else std::cout << "This is not the main thread. ";}void pause_thread(int n) { std::this_thread::sleep_for(std::chrono::seconds(n)); std::cout << "pause of " << n << " seconds ended ";}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... "; 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: "; for (auto &thread : threads) thread.join(); std::cout << "All threads joined! ";}之前一些编译器使用 C++11 的编译参数是 -std=c++11
ztftrue
ztf***e@gmail.com
参考地址
bigben
396***8@qq.com
要注意内存泄露问题。
如果设置为 PTHREAD_CREATE_JOINABLE,就继续用 pthread_join() 来等待和释放资源,否则会内存泄露。