|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <iostream>
- #include <thread>
- using namespace std;
- void thread_1()
- {
- while(1)
- {
- cout<<"子线程1111"<<endl;
- }
- }
- void thread_2(int x)
- {
- while(1)
- {
- cout<<"子线程2222"<<endl;
- }
- }
- int main()
- {
- thread first ( thread_1); // 开启线程,调用:thread_1()
- thread second (thread_2,100); // 开启线程,调用:thread_2(100)
- first.join();
- second.join();
- while(1)
- {
- std::cout << "主线程\n";
- }
- return 0;
- }
复制代码
程序报错(linux):
- /usr/bin/ld: /tmp/cc3NaPCp.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
- g.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x44): undefined reference to `pthread_create'
- /usr/bin/ld: /tmp/cc3NaPCp.o: in function `main':
- g.cpp:(.text.startup+0x57): undefined reference to `pthread_create'
- collect2: error: ld returned 1 exit status
复制代码
程序报错(windows):
- In function 'int main()':
- [Error] 'thread' was not declared in this scope
- [Note] 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
- ...
复制代码
Windows 不熟
Linux 编译的时候您可能需要添加一个 -lpthread 来链接到需要用到的库
|
|