Skip to content

Commit b1b8a5e

Browse files
committed
udpate thread tutorial.md
1 parent d121755 commit b1b8a5e

File tree

1 file changed

+165
-3
lines changed

1 file changed

+165
-3
lines changed

zh/chapter3-Thread/Introduction-to-Thread.md

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,173 @@ thread& operator=(const thread&) = delete;
262262
t.join();
263263
}
264264

265-
- `join`: Join 线程。
265+
- `join`: Join 线程,调用该函数会阻塞当前线程,直到由 `*this` 所标示的线程执行完毕,join 才返回。
266+
267+
#include <iostream>
268+
#include <thread>
269+
#include <chrono>
270+
271+
void foo()
272+
{
273+
// simulate expensive operation
274+
std::this_thread::sleep_for(std::chrono::seconds(1));
275+
}
276+
277+
void bar()
278+
{
279+
// simulate expensive operation
280+
std::this_thread::sleep_for(std::chrono::seconds(1));
281+
}
282+
283+
int main()
284+
{
285+
std::cout << "starting first helper...\n";
286+
std::thread helper1(foo);
287+
288+
std::cout << "starting second helper...\n";
289+
std::thread helper2(bar);
290+
291+
std::cout << "waiting for helpers to finish..." << std::endl;
292+
helper1.join();
293+
helper2.join();
294+
295+
std::cout << "done!\n";
296+
}
297+
266298
- `detach`: Detach 线程。
299+
将当前线程对象所代表的执行实例与该线程对象分离,使得线程的执行可以单独进行。一旦线程执行完毕,它所分配的资源将会被释放。
300+
301+
调用 detach 函数之后:
302+
303+
- `*this` 不再代表任何的线程执行实例。
304+
- joinable() == false
305+
- get_id() == std::thread::id()
306+
307+
另外,如果出错或者 `joinable() == false`,则会抛出 `std::system_error`.
308+
309+
#include <iostream>
310+
#include <chrono>
311+
#include <thread>
312+
313+
void independentThread()
314+
{
315+
std::cout << "Starting concurrent thread.\n";
316+
std::this_thread::sleep_for(std::chrono::seconds(2));
317+
std::cout << "Exiting concurrent thread.\n";
318+
}
319+
320+
void threadCaller()
321+
{
322+
std::cout << "Starting thread caller.\n";
323+
std::thread t(independentThread);
324+
t.detach();
325+
std::this_thread::sleep_for(std::chrono::seconds(1));
326+
std::cout << "Exiting thread caller.\n";
327+
}
328+
329+
int main()
330+
{
331+
threadCaller();
332+
std::this_thread::sleep_for(std::chrono::seconds(5));
333+
}
334+
267335
- `swap`: Swap 线程 。
268-
- `native_handle`: 返回 native handle。
269-
- `hardware_concurrency` [static]: 检测硬件并发特性。
336+
337+
#include <iostream>
338+
#include <thread>
339+
#include <chrono>
340+
341+
void foo()
342+
{
343+
std::this_thread::sleep_for(std::chrono::seconds(1));
344+
}
345+
346+
void bar()
347+
{
348+
std::this_thread::sleep_for(std::chrono::seconds(1));
349+
}
350+
351+
int main()
352+
{
353+
std::thread t1(foo);
354+
std::thread t2(bar);
355+
356+
std::cout << "thread 1 id: " << t1.get_id() << std::endl;
357+
std::cout << "thread 2 id: " << t2.get_id() << std::endl;
358+
359+
std::swap(t1, t2);
360+
361+
std::cout << "after std::swap(t1, t2):" << std::endl;
362+
std::cout << "thread 1 id: " << t1.get_id() << std::endl;
363+
std::cout << "thread 2 id: " << t2.get_id() << std::endl;
364+
365+
t1.swap(t2);
366+
367+
std::cout << "after t1.swap(t2):" << std::endl;
368+
std::cout << "thread 1 id: " << t1.get_id() << std::endl;
369+
std::cout << "thread 2 id: " << t2.get_id() << std::endl;
370+
371+
t1.join();
372+
t2.join();
373+
}
374+
375+
执行结果如下:
376+
377+
thread 1 id: 1892
378+
thread 2 id: 2584
379+
after std::swap(t1, t2):
380+
thread 1 id: 2584
381+
thread 2 id: 1892
382+
after t1.swap(t2):
383+
thread 1 id: 1892
384+
thread 2 id: 2584
385+
386+
- `native_handle`: 返回 native handle(与操作系统相关)。
387+
388+
#include <thread>
389+
#include <iostream>
390+
#include <chrono>
391+
#include <cstring>
392+
#include <pthread.h>
393+
394+
std::mutex iomutex;
395+
void f(int num)
396+
{
397+
std::this_thread::sleep_for(std::chrono::seconds(1));
398+
399+
sched_param sch;
400+
int policy;
401+
pthread_getschedparam(pthread_self(), &policy, &sch);
402+
std::lock_guard<std::mutex> lk(iomutex);
403+
std::cout << "Thread " << num << " is executing at priority "
404+
<< sch.sched_priority << '\n';
405+
}
406+
407+
int main()
408+
{
409+
std::thread t1(f, 1), t2(f, 2);
410+
411+
sched_param sch;
412+
int policy;
413+
pthread_getschedparam(t1.native_handle(), &policy, &sch);
414+
sch.sched_priority = 20;
415+
if(pthread_setschedparam(t1.native_handle(), SCHED_FIFO, &sch)) {
416+
std::cout << "Failed to setschedparam: " << std::strerror(errno) << '\n';
417+
}
418+
419+
t1.join();
420+
t2.join();
421+
}
422+
423+
- `hardware_concurrency` [static]: 检测硬件并发特性,返回当前实现所支持的线程并发数目,然后返回值仅仅只作为系统提示。
424+
425+
#include <iostream>
426+
#include <thread>
427+
428+
int main() {
429+
unsigned int n = std::thread::hardware_concurrency();
430+
std::cout << n << " concurrent threads are supported.\n";
431+
}
270432

271433
## `std::this_thread` 命名空间中相关辅助函数介绍 ##
272434

0 commit comments

Comments
 (0)