Boost.Threads

From:http://www.boost.org/doc/libs/1_31_0/libs/thread/doc/definitions.html

Definitions

Thread

Thread is short for "thread of execution". A thread of execution is an execution environment [1.9/7] within the execution environment of a C++ program [1.9]. The main() function [3.6.1] of the program is the initial function of the initial thread. A program in a multithreading environment always has an initial thread even if the program explicitly creates no additional threads.

Unless otherwise specified, each thread shares all aspects of its execution environment with other threads in the program. Shared aspects of the execution environment include, but are not limited to, the following:

  • Static storage duration (static, extern) objects [3.7.1].
  • Dynamic storage duration (heap) objects [3.7.3]. Thus each memory allocation will return a unique addresses, regardless of the thread making the allocation request.
  • Automatic storage duration (stack) objects [3.7.2] accessed via pointer or reference from another thread.
  • Resources provided by the operating system. For example, files.
  • The program itself. In other words, each thread is executing some function of the same program, not a totally different program.

Each thread has its own:

  • Registers and current execution sequence (program counter) [1.9/5].
  • Automatic storage duration (stack) objects [3.7.2].

Thread-safe

A program is thread-safe if it has no race conditions, does not deadlock, and has no priority failures.

Note that thread-safety does not necessarily imply efficiency, and than while some thread-safety violations can be determined statically at compile time, many thread-safety errors can only only be detected at runtime.

Thread State

During the lifetime of a thread, it shall be in one of the following states:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Boost.Thread 实现线程池的示例代码: ```c++ #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/condition.hpp> #include <queue> class ThreadPool { public: ThreadPool(int num_threads) : num_threads_(num_threads), stop_(false) { for (int i = 0; i < num_threads_; ++i) { threads_.create_thread(boost::bind(&ThreadPool::worker_thread, this)); } } ~ThreadPool() { { boost::unique_lock<boost::mutex> lock(mutex_); stop_ = true; cond_.notify_all(); } threads_.join_all(); } void enqueue(boost::function<void()> task) { boost::unique_lock<boost::mutex> lock(mutex_); tasks_.push(task); cond_.notify_one(); } private: void worker_thread() { while (!stop_) { boost::function<void()> task; { boost::unique_lock<boost::mutex> lock(mutex_); while (tasks_.empty() && !stop_) { cond_.wait(lock); } if (stop_) { return; } task = tasks_.front(); tasks_.pop(); } task(); } } int num_threads_; boost::thread_group threads_; std::queue<boost::function<void()> > tasks_; boost::mutex mutex_; boost::condition_variable cond_; bool stop_; }; ``` 这个线程池可以通过 `ThreadPool pool(num_threads)` 构造函数创建,其 `num_threads` 参数指定线程池线程的数量。任务可以通过 `pool.enqueue(task)` 方法提交到线程池执行,其 `task` 是一个 `boost::function<void()>` 类型的可调用对象。 该线程池使用一个任务队列来存储待执行的任务,并使用互斥量和条件变量来实现线程同步。在析构函数,首先设置 `stop_` 标志,然后通知所有等待线程的条件变量,以便它们退出。最后,等待所有线程结束并加入到线程组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值