线程池技术补充(轮询,等待完成结果,避免争夺,任务窃取)

简介

前文我们介绍了线程池,已经给大家提供了一个完整的线程池封装了,本节跟着《C++ 并发编程实战》一书中作者的思路,看看他的线程池的实现,以此作为补充

轮询方式的线程池

配合我们之前封装的线程安全队列threadsafe_queue

  1. #include <mutex>
  2. #include <queue>
  3. template<typename T>
  4. class threadsafe_queue
  5. {
  6. private:
  7. struct node
  8. {
  9. std::shared_ptr<T> data;
  10. std::unique_ptr<node> next;
  11. node* prev;
  12. };
  13. std::mutex head_mutex;
  14. std::unique_ptr<node> head;
  15. std::mutex tail_mutex;
  16. node* tail;
  17. std::condition_variable data_cond;
  18. std::atomic_bool bstop;
  19. node* get_tail()
  20. {
  21. std::lock_guard<std::mutex> tail_lock(tail_mutex);
  22. return tail;
  23. }
  24. std::unique_ptr<node> pop_head()
  25. {
  26. std::unique_ptr<node> old_head = std::move(head);
  27. head = std::move(old_head->next);
  28. return old_head;
  29. }
  30. std::unique_lock<std::mutex> wait_for_data()
  31. {
  32. std::unique_lock<std::mutex> head_lock(head_mutex);
  33. data_cond.wait(head_lock,[&] {return head.get() != get_tail() || bstop.load() == true; });
  34. return std::move(head_lock);
  35. }
  36. std::unique_ptr<node> wait_pop_head()
  37. {
  38. std::unique_lock<std::mutex> head_lock(wait_for_data());
  39. if (bstop.load()) {
  40. return nullptr;
  41. }
  42. return pop_head();
  43. }
  44. std::unique_ptr<node> wait_pop_head(T& value)
  45. {
  46. std::unique_lock<std::mutex> head_lock(wait_for_data());
  47. if (bstop.load()) {
  48. return nullptr;
  49. }
  50. value = std::move(*head->data);
  51. return pop_head();
  52. }
  53. std::unique_ptr<node> try_pop_head()
  54. {
  55. std::lock_guard<std::mutex> head_lock(head_mutex);
  56. if (head.get() == get_tail())
  57. {
  58. return std::unique_ptr<node>();
  59. }
  60. return pop_head();
  61. }
  62. std::unique_ptr<node> try_pop_head(T& value)
  63. {
  64. std::lock_guard<std::mutex> head_lock(head_mutex);
  65. if (head.get() == get_tail())
  66. {
  67. return std::unique_ptr<node>();
  68. }
  69. value = std::move(*head->data);
  70. return pop_head();
  71. }
  72. public:
  73. threadsafe_queue() : // ⇽-- - 1
  74. head(new node), tail(head.get())
  75. {}
  76. ~threadsafe_queue() {
  77. bstop.store(true);
  78. data_cond.notify_all();
  79. }
  80. threadsafe_queue(const threadsafe_queue& other) = delete;
  81. threadsafe_queue& operator=(const threadsafe_queue& other) = delete;
  82. void Exit() {
  83. bstop.store(true);
  84. data_cond.notify_all();
  85. }
  86. bool wait_and_pop_timeout(T& value) {
  87. std::unique_lock<std::mutex> head_lock(head_mutex);
  88. auto res = data_cond.wait_for(head_lock, std::chrono::milliseconds(100),
  89. [&] {return head.get() != get_tail() || bstop.load() == true; });
  90. if (res == false) {
  91. return false;
  92. }
  93. if (bstop.load()) {
  94. return false;
  95. }
  96. value = std::move(*head->data);
  97. head = std::move(head->next);
  98. return true;
  99. }
  100. std::shared_ptr<T> wait_and_pop() // <------3
  101. {
  102. std::unique_ptr<node> const old_head = wait_pop_head();
  103. if (old_head == nullptr) {
  104. return nullptr;
  105. }
  106. return old_head->data;
  107. }
  108. bool wait_and_pop(T& value) // <------4
  109. {
  110. std::unique_ptr<node> const old_head = wait_pop_head(value);
  111. if (old_head == nullptr) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. std::shared_ptr<T> try_pop()
  117. {
  118. std::unique_ptr<node> old_head = try_pop_head();
  119. return old_head ? old_head->data : std::shared_ptr<T>();
  120. }
  121. bool try_pop(T& value)
  122. {
  123. std::unique_ptr<node> const old_head = try_pop_head(value);
  124. if (old_head) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. bool empty()
  130. {
  131. std::lock_guard<std::mutex> head_lock(head_mutex);
  132. return (head.get() == get_tail());
  133. }
  134. void push(T new_value) //<------2
  135. {
  136. std::shared_ptr<T> new_data(
  137. std::make_shared<T>(std::move(new_value)));
  138. std::unique_ptr<node> p(new node);
  139. {
  140. std::lock_guard<std::mutex> tail_lock(tail_mutex);
  141. tail->data = new_data;
  142. node* const new_tail = p.get();
  143. new_tail->prev = tail;
  144. tail->next = std::move(p);
  145. tail = new_tail;
  146. }
  147. data_cond.notify_one();
  148. }
  149. bool try_steal(T& value) {
  150. std::unique_lock<std::mutex> tail_lock(tail_mutex,std::defer_lock);
  151. std::unique_lock<std::mutex> head_lock(head_mutex, std::defer_lock);
  152. std::lock(tail_lock, head_lock);
  153. if (head.get() == tail)
  154. {
  155. return false;
  156. }
  157. node* prev_node = tail->prev;
  158. value = std::move(*(prev_node->data));
  159. tail = prev_node;
  160. tail->next = nullptr;
  161. return true;
  162. }
  163. };

我们封装了一个简单轮询的线程池

  1. #include <atomic>
  2. #include "ThreadSafeQue.h"
  3. #include "join_thread.h"
  4. class simple_thread_pool
  5. {
  6. std::atomic_bool done;
  7. //⇽-- - 1
  8. threadsafe_queue<std::function<void()> > work_queue;
  9. //⇽-- - 2
  10. std::vector<std::thread> threads;
  11. //⇽-- - 3
  12. join_threads joiner;
  13. void worker_thread()
  14. {
  15. //⇽-- - 4
  16. while (!done)
  17. {
  18. std::function<void()> task;
  19. //⇽-- - 5
  20. if (work_queue.try_pop(task))
  21. {
  22. //⇽-- - 6
  23. task();
  24. }
  25. else
  26. {
  27. //⇽-- - 7
  28. std::this_thread::yield();
  29. }
  30. }
  31. }
  32. simple_thread_pool() :
  33. done(false), joiner(threads)
  34. {
  35. //⇽--- 8
  36. unsigned const thread_count = std::thread::hardware_concurrency();
  37. try
  38. {
  39. for (unsigned i = 0; i < thread_count; ++i)
  40. {
  41. //⇽-- - 9
  42. threads.push_back(std::thread(&simple_thread_pool::worker_thread, this));
  43. }
  44. }
  45. catch (...)
  46. {
  47. //⇽-- - 10
  48. done = true;
  49. throw;
  50. }
  51. }
  52. public:
  53. static simple_thread_pool& instance() {
  54. static simple_thread_pool pool;
  55. return pool;
  56. }
  57. ~simple_thread_pool()
  58. {
  59. //⇽-- - 11
  60. done = true;
  61. for (unsigned i = 0; i < threads.size(); ++i)
  62. {
  63. //⇽-- - 9
  64. threads[i].join();
  65. }
  66. }
  67. template<typename FunctionType>
  68. void submit(FunctionType f)
  69. {
  70. //⇽-- - 12
  71. work_queue.push(std::function<void()>(f));
  72. }
  73. };
  1. worker_thread 即为线程的回调函数,回调函数内从队列中取出任务并处理,如果没有任务则调用yield释放cpu资源。

  2. submit函数比较简单,投递了一个返回值为void,参数为void的任务。这和我们之前自己设计的线程池(可执行任意参数类型,返回值不限的函数)相比功能稍差了一些。

获取任务完成结果

因为外部投递任务给线程池后要获取线程池执行任务的结果,我们之前自己设计的线程池采用的是future和decltype推断函数返回值的方式构造一个返回类型的future。

这里作者先封装一个可调用对象的类

  1. class function_wrapper
  2. {
  3. struct impl_base {
  4. virtual void call() = 0;
  5. virtual ~impl_base() {}
  6. };
  7. std::unique_ptr<impl_base> impl;
  8. template<typename F>
  9. struct impl_type : impl_base
  10. {
  11. F f;
  12. impl_type(F&& f_) : f(std::move(f_)) {}
  13. void call() { f(); }
  14. };
  15. public:
  16. template<typename F>
  17. function_wrapper(F&& f) :
  18. impl(new impl_type<F>(std::move(f)))
  19. {}
  20. void operator()() { impl->call(); }
  21. function_wrapper() = default;
  22. function_wrapper(function_wrapper&& other) :
  23. impl(std::move(other.impl))
  24. {}
  25. function_wrapper& operator=(function_wrapper&& other)
  26. {
  27. impl = std::move(other.impl);
  28. return *this;
  29. }
  30. function_wrapper(const function_wrapper&) = delete;
  31. function_wrapper(function_wrapper&) = delete;
  32. function_wrapper& operator=(const function_wrapper&) = delete;
  33. };
  1. impl_base 是一个基类,内部有一个纯虚函数call,以及一个虚析构,这样可以通过delete 基类指针动态析构子类对象。

  2. impl_type 继承了impl_base类,内部包含了一个可调用对象f,并且实现了构造函数和call函数,call内部调用可调用对象f。

  3. function_wrapper 内部有智能指针impl_base类型的unique_ptr变量impl, function_wrapper构造函数根据可调用对象f构造impl

  4. function_wrapper支持移动构造不支持拷贝和赋值。function_wrapper本质上就是当作task给线程池执行的。

可获取任务执行状态的线程池如下

  1. class future_thread_pool
  2. {
  3. private:
  4. void worker_thread()
  5. {
  6. while (!done)
  7. {
  8. function_wrapper task;
  9. if (work_queue.try_pop(task))
  10. {
  11. task();
  12. }
  13. else
  14. {
  15. std::this_thread::yield();
  16. }
  17. }
  18. }
  19. public:
  20. static future_thread_pool& instance() {
  21. static future_thread_pool pool;
  22. return pool;
  23. }
  24. ~future_thread_pool()
  25. {
  26. //⇽-- - 11
  27. done = true;
  28. for (unsigned i = 0; i < threads.size(); ++i)
  29. {
  30. //⇽-- - 9
  31. threads[i].join();
  32. }
  33. }
  34. template<typename FunctionType>
  35. std::future<typename std::result_of<FunctionType()>::type>
  36. submit(FunctionType f)
  37. {
  38. typedef typename std::result_of<FunctionType()>::type result_type;
  39. std::packaged_task<result_type()> task(std::move(f));
  40. std::future<result_type> res(task.get_future());
  41. work_queue.push(std::move(task));
  42. return res;
  43. }
  44. private:
  45. future_thread_pool() :
  46. done(false), joiner(threads)
  47. {
  48. //⇽--- 8
  49. unsigned const thread_count = std::thread::hardware_concurrency();
  50. try
  51. {
  52. for (unsigned i = 0; i < thread_count; ++i)
  53. {
  54. //⇽-- - 9
  55. threads.push_back(std::thread(&future_thread_pool::worker_thread, this));
  56. }
  57. }
  58. catch (...)
  59. {
  60. //⇽-- - 10
  61. done = true;
  62. throw;
  63. }
  64. }
  65. std::atomic_bool done;
  66. //⇽-- - 1
  67. threadsafe_queue<function_wrapper> work_queue;
  68. //⇽-- - 2
  69. std::vector<std::thread> threads;
  70. //⇽-- - 3
  71. join_threads joiner;
  72. };
  1. worker_thread内部从队列中pop任务并执行,如果没有任务则交出cpu资源。

  2. submit函数返回值为std::future<typename std::result_of<FunctionType()>::type>类型,通过std::result_of<FunctionType()>推断出函数执行的结果,然后通过::type推断出结果的类型,并且根据这个类型构造future,这样调用者就可以在投递完任务获取任务的执行结果了。

  3. submit函数内部我们将函数执行的结果类型定义为result_type类型,并且利用f构造一个packaged_task任务。通过task返回一个future给外部调用者,然后我们调用队列的push将task放入队列,注意队列存储的是function_wrapper,这里是利用task隐式构造了function_wrapper类型的对象。

利用条件变量等待

当我们的任务队列中没有任务的时候,可以让线程挂起,然后等待有任务投递到队列后在激活线程处理

  1. class notify_thread_pool
  2. {
  3. private:
  4. void worker_thread()
  5. {
  6. while (!done)
  7. {
  8. auto task_ptr = work_queue.wait_and_pop();
  9. if (task_ptr == nullptr) {
  10. continue;
  11. }
  12. (*task_ptr)();
  13. }
  14. }
  15. public:
  16. static notify_thread_pool& instance() {
  17. static notify_thread_pool pool;
  18. return pool;
  19. }
  20. ~notify_thread_pool()
  21. {
  22. //⇽-- - 11
  23. done = true;
  24. work_queue.Exit();
  25. for (unsigned i = 0; i < threads.size(); ++i)
  26. {
  27. //⇽-- - 9
  28. threads[i].join();
  29. }
  30. }
  31. template<typename FunctionType>
  32. std::future<typename std::result_of<FunctionType()>::type>
  33. submit(FunctionType f)
  34. {
  35. typedef typename std::result_of<FunctionType()>::type result_type;
  36. std::packaged_task<result_type()> task(std::move(f));
  37. std::future<result_type> res(task.get_future());
  38. work_queue.push(std::move(task));
  39. return res;
  40. }
  41. private:
  42. notify_thread_pool() :
  43. done(false), joiner(threads)
  44. {
  45. //⇽--- 8
  46. unsigned const thread_count = std::thread::hardware_concurrency();
  47. try
  48. {
  49. for (unsigned i = 0; i < thread_count; ++i)
  50. {
  51. //⇽-- - 9
  52. threads.push_back(std::thread(&notify_thread_pool::worker_thread, this));
  53. }
  54. }
  55. catch (...)
  56. {
  57. //⇽-- - 10
  58. done = true;
  59. work_queue.Exit();
  60. throw;
  61. }
  62. }
  63. std::atomic_bool done;
  64. //⇽-- - 1
  65. threadsafe_queue<function_wrapper> work_queue;
  66. //⇽-- - 2
  67. std::vector<std::thread> threads;
  68. //⇽-- - 3
  69. join_threads joiner;
  70. };
  1. worker_thread内部调用了work_queue的wait_and_pop函数,如果队列中有任务直接返回,如果没任务则挂起。

  2. 另外我们在线程池的析构函数和异常处理时都增加了work_queue.Exit(); 这需要在我们的线程安全队列中增加Exit函数通知线程唤醒,因为线程发现队列为空会阻塞住。

  1. void Exit() {
  2. bstop.store(true);
  3. data_cond.notify_all();
  4. }

避免争夺

我们的任务队列只有一个,当向任务队列频繁投递任务,线程池中其他线程从队列中获取任务,队列就会频繁加锁和解锁,一般情况下性能不会有什么损耗,但是如果投递的任务较多,我们可以采取分流的方式,创建多个任务队列(可以和线程池中线程数相等),将任务投递给不同的任务队列,每个线程消费自己的队列即可,这样减少了线程间取任务的冲突。

  1. #include "ThreadSafeQue.h"
  2. #include <future>
  3. #include "ThreadSafeQue.h"
  4. #include "join_thread.h"
  5. #include "FutureThreadPool.h"
  6. class parrallen_thread_pool
  7. {
  8. private:
  9. void worker_thread(int index)
  10. {
  11. while (!done)
  12. {
  13. auto task_ptr = thread_work_ques[index].wait_and_pop();
  14. if (task_ptr == nullptr) {
  15. continue;
  16. }
  17. (*task_ptr)();
  18. }
  19. }
  20. public:
  21. static parrallen_thread_pool& instance() {
  22. static parrallen_thread_pool pool;
  23. return pool;
  24. }
  25. ~parrallen_thread_pool()
  26. {
  27. //⇽-- - 11
  28. done = true;
  29. for (unsigned i = 0; i < thread_work_ques.size(); i++) {
  30. thread_work_ques[i].Exit();
  31. }
  32. for (unsigned i = 0; i < threads.size(); ++i)
  33. {
  34. //⇽-- - 9
  35. threads[i].join();
  36. }
  37. }
  38. template<typename FunctionType>
  39. std::future<typename std::result_of<FunctionType()>::type>
  40. submit(FunctionType f)
  41. {
  42. int index = (atm_index.load() + 1) % thread_work_ques.size();
  43. atm_index.store(index);
  44. typedef typename std::result_of<FunctionType()>::type result_type;
  45. std::packaged_task<result_type()> task(std::move(f));
  46. std::future<result_type> res(task.get_future());
  47. thread_work_ques[index].push(std::move(task));
  48. return res;
  49. }
  50. private:
  51. parrallen_thread_pool() :
  52. done(false), joiner(threads), atm_index(0)
  53. {
  54. //⇽--- 8
  55. unsigned const thread_count = std::thread::hardware_concurrency();
  56. try
  57. {
  58. thread_work_ques = std::vector < threadsafe_queue<function_wrapper>>(thread_count);
  59. for (unsigned i = 0; i < thread_count; ++i)
  60. {
  61. //⇽-- - 9
  62. threads.push_back(std::thread(&parrallen_thread_pool::worker_thread, this, i));
  63. }
  64. }
  65. catch (...)
  66. {
  67. //⇽-- - 10
  68. done = true;
  69. for (int i = 0; i < thread_work_ques.size(); i++) {
  70. thread_work_ques[i].Exit();
  71. }
  72. throw;
  73. }
  74. }
  75. std::atomic_bool done;
  76. //全局队列
  77. std::vector<threadsafe_queue<function_wrapper>> thread_work_ques;
  78. //⇽-- - 2
  79. std::vector<std::thread> threads;
  80. //⇽-- - 3
  81. join_threads joiner;
  82. std::atomic<int> atm_index;
  83. };
  1. 我们将任务队列变为多个//全局队列 std::vector<threadsafe_queue<function_wrapper>> thread_work_ques;.

  2. commit的时候根据atm_index索引自增后对总大小取余将任务投递给不同的队列。

  3. worker_thread增加了索引参数,每个线程的在回调的时候会根据自己的索引取出对应队列中的任务进行执行。

任务窃取

当本线程队列中的任务处理完了,它可以去别的线程的任务队列中看看是否有没处理的任务,帮助其他线程处理任务,简称任务窃取。

  1. #include "ThreadSafeQue.h"
  2. #include <future>
  3. #include "ThreadSafeQue.h"
  4. #include "join_thread.h"
  5. #include "FutureThreadPool.h"
  6. class steal_thread_pool
  7. {
  8. private:
  9. void worker_thread(int index)
  10. {
  11. while (!done)
  12. {
  13. function_wrapper wrapper;
  14. bool pop_res = thread_work_ques[index].try_pop(wrapper);
  15. if (pop_res) {
  16. wrapper();
  17. continue;
  18. }
  19. bool steal_res = false;
  20. for (int i = 0; i < thread_work_ques.size(); i++) {
  21. if (i == index) {
  22. continue;
  23. }
  24. steal_res = thread_work_ques[i].try_pop(wrapper);
  25. if (steal_res) {
  26. wrapper();
  27. break;
  28. }
  29. }
  30. if (steal_res) {
  31. continue;
  32. }
  33. std::this_thread::yield();
  34. }
  35. }
  36. public:
  37. static steal_thread_pool& instance() {
  38. static steal_thread_pool pool;
  39. return pool;
  40. }
  41. ~steal_thread_pool()
  42. {
  43. //⇽-- - 11
  44. done = true;
  45. for (unsigned i = 0; i < thread_work_ques.size(); i++) {
  46. thread_work_ques[i].Exit();
  47. }
  48. for (unsigned i = 0; i < threads.size(); ++i)
  49. {
  50. //⇽-- - 9
  51. threads[i].join();
  52. }
  53. }
  54. template<typename FunctionType>
  55. std::future<typename std::result_of<FunctionType()>::type>
  56. submit(FunctionType f)
  57. {
  58. int index = (atm_index.load() + 1) % thread_work_ques.size();
  59. atm_index.store(index);
  60. typedef typename std::result_of<FunctionType()>::type result_type;
  61. std::packaged_task<result_type()> task(std::move(f));
  62. std::future<result_type> res(task.get_future());
  63. thread_work_ques[index].push(std::move(task));
  64. return res;
  65. }
  66. private:
  67. steal_thread_pool() :
  68. done(false), joiner(threads), atm_index(0)
  69. {
  70. //⇽--- 8
  71. unsigned const thread_count = std::thread::hardware_concurrency();
  72. try
  73. {
  74. thread_work_ques = std::vector < threadsafe_queue<function_wrapper>>(thread_count);
  75. for (unsigned i = 0; i < thread_count; ++i)
  76. {
  77. //⇽-- - 9
  78. threads.push_back(std::thread(&steal_thread_pool::worker_thread, this, i));
  79. }
  80. }
  81. catch (...)
  82. {
  83. //⇽-- - 10
  84. done = true;
  85. for (int i = 0; i < thread_work_ques.size(); i++) {
  86. thread_work_ques[i].Exit();
  87. }
  88. throw;
  89. }
  90. }
  91. std::atomic_bool done;
  92. //全局队列
  93. std::vector<threadsafe_queue<function_wrapper>> thread_work_ques;
  94. //⇽-- - 2
  95. std::vector<std::thread> threads;
  96. //⇽-- - 3
  97. join_threads joiner;
  98. std::atomic<int> atm_index;
  99. };
  1. worker_thread中本线程会先处理自己队列中的任务,如果自己队列中没有任务则从其它线程的任务队列中获取任务。如果都没有则交出cpu资源。

  2. 为了实现try_steal的功能,我们需要修改线程安全队列threadsafe_queue,增加try_steal函数

  1. bool try_steal(T& value) {
  2. std::unique_lock<std::mutex> tail_lock(tail_mutex,std::defer_lock);
  3. std::unique_lock<std::mutex> head_lock(head_mutex, std::defer_lock);
  4. std::lock(tail_lock, head_lock);
  5. if (head.get() == tail)
  6. {
  7. return false;
  8. }
  9. node* prev_node = tail->prev;
  10. value = std::move(*(prev_node->data));
  11. tail = prev_node;
  12. tail->next = nullptr;
  13. return true;
  14. }

因为try_steal是从队列的尾部弹出数据,为了防止此时有其他线程从头部弹出数据造成操作同一个节点,或者其他线程弹出头部数据后接着修改头部节点为下一个节点,此时本线程正在弹出尾部节点,而尾部节点正好是头部的下一个节点造成数据混乱,此时加了两把锁,对头部和尾部都加锁。

我们这里所说的弹出尾部节点不是弹出tail,而是tail的前一个节点,因为tail是尾部表示一个空节点,tail前边的节点才是尾部数据的节点,为了实现反向查找,我们为node增加了prev指针

  1. struct node
  2. {
  3. std::shared_ptr<T> data;
  4. std::unique_ptr<node> next;
  5. node* prev;
  6. };

所以在push节点的时候也要把这个节点的prev指针指向前一个节点

  1. void push(T new_value) //<------2
  2. {
  3. std::shared_ptr<T> new_data(
  4. std::make_shared<T>(std::move(new_value)));
  5. std::unique_ptr<node> p(new node);
  6. {
  7. std::lock_guard<std::mutex> tail_lock(tail_mutex);
  8. tail->data = new_data;
  9. node* const new_tail = p.get();
  10. new_tail->prev = tail;
  11. tail->next = std::move(p);
  12. tail = new_tail;
  13. }
  14. data_cond.notify_one();
  15. }

整体来说steal版本的线程池就这些内容和前边变化不大。

测试

测试用例已经在源代码中写好,感兴趣可以看下

源码链接:

https://gitee.com/secondtonone1/boostasio-learn/tree/master/concurrent/day22-ThreadPool

视频链接:

https://space.bilibili.com/271469206/channel/collectiondetail?sid=1623290

热门评论

热门文章

  1. windows环境搭建和vscode配置

    喜欢(587) 浏览(1871)
  2. slice介绍和使用

    喜欢(521) 浏览(1945)
  3. 解密定时器的实现细节

    喜欢(566) 浏览(2327)
  4. C++ 类的继承封装和多态

    喜欢(588) 浏览(3253)
  5. Linux环境搭建和编码

    喜欢(594) 浏览(7080)

最新评论

  1. C++ 虚函数表原理和类成员内存分布 WangQi888888:class Test{ int m; int b; }中b成员是int,为什么在内存中只占了1个字节。不应该是4个字节吗?是不是int应该改为char。这样的话就会符合图上说明的情况
  2. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。
  3. asio多线程模型IOServicePool Lion:线程池一定要继承单例模式吗
  4. 泛型算法的定制操作 secondtonone1:lambda和bind是C11新增的利器,善于利用这两个机制可以极大地提升编程安全性和效率。
  5. 类和对象 陈宇航:支持!!!!

个人公众号

个人微信