c++之thread,先起个头,以后补充

下面是网上的解释:

Thread
Class to represent individual threads of execution.

A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.

An initialized thread object represents an active thread of execution; Such a thread object is joinable, and has a unique thread id.

A default-constructed (non-initialized) thread object is not joinable, and its thread id is common for all non-joinable threads.

A joinable thread becomes not joinable if moved from, or if either join or detach are called on them.

  • 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 )
  • Non-member overloads
    • swap (thread):Swap threads (function )

举例说明:

// thread example
#include <iostream>       // std::cout
#include <stdio.h>
#include <thread>         // std::thread

void foo()
{
    std::thread::id firstID = std::this_thread::get_id();
    printf("first thread id is %ld\n", firstID);
    std::cout<<"first thread id is "<<firstID << std::endl;
}

void bar(int x)
{
    std::thread::id secondID = std::this_thread::get_id();
    printf("second thread id is %ld\n", secondID);
    std::cout<<"second thread id is "<<secondID << std::endl;
}

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;
}

g++ main.cpp -std=c++11  -lpthread

main, foo and bar now execute concurrently...
second thread id is 140619849778944
second thread id is 140619849778944
first thread id is 140619858171648
first thread id is 140619858171648
foo and bar completed.

可以看出,对于x64而言,thread ID是long int 型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值