> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/c-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/c-1/he-xin-mo-zu/thread.md).

# Thread

> c語言沒有內建thread 所以要用PThread(POSIX thread)
>
> c++ 可以使用#include \<thread>

## 範例：

```c
#include <iostream>
#include <thread>

using namespace std;

void test_func()
{
  // do something
  double dSum = 0;
  for( int i = 0; i < 10000; ++ i )
    for( int j = 0; j < 10000; ++ j )
      dSum += i*j;

  cout << "Thread: " << dSum << endl;
}

int main( int argc, char** argv )
{
  // execute thread
  thread mThread( test_func );

  // do somthing
  cout << "main thread" << endl;

  // wait the thread stop
  mThread.join();

  return 0;
}
```

一篇不錯的文章

<https://kheresy.wordpress.com/2012/07/06/multi-thread-programming-in-c-thread-p1/>

<https://kheresy.wordpress.com/2012/07/11/multi-thread-programming-in-c-thread-p2/>
