在《C++并发编程实战》这本书中第3章主要将的是多线程之间的数据共享同步问题。在多线程之间需要进行数据同步的主要是条件竞争。

1  std::lock_guard<std::mutex>

#include <list>
#include <mutex>
#include <algorithm> std::list<int> some_list;
std::mutex some_mutex; void add_to_list(int new_value)
{
std::lock_guard<std::mutex> guard(some_mutex);
some_list.push_back(new_value);
}
bool list_contains(int value_to_find)
{
std::lock_guard<std::mutex> guard(some_mutex);
return std::find(some_list.begin(),some_list.end(),value_to_find)
!= some_list.end();
} #include <iostream> int main()
{
add_to_list();
std::cout<<"contains(1)="<<list_contains()<<", contains(42)="<<list_contains()<<std::endl;
}

在上述代码中使用了std::lock_guard<>模板,使用该模板定义的mutex在栈空间分配空间,在构造函数中会对传入的mutex变量进行加锁,并在函数运行结束时在其析构函数中调同mutex.unlock()来自动解锁。我们来看看lock_gard<T>的定义:

  /** @brief A simple scoped lock type.
*
* A lock_guard controls mutex ownership within a scope, releasing
* ownership in the destructor.
*/
template<typename _Mutex>
class lock_guard
{
public:
typedef _Mutex mutex_type; explicit lock_guard(mutex_type& __m) : _M_device(__m)
{ _M_device.lock(); } lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
{ } // calling thread owns mutex ~lock_guard()
{ _M_device.unlock(); }   lock_guard(const lock_guard&) = delete;
lock_guard& operator=(const lock_guard&) = delete; private:
mutex_type& _M_device;
};

 2 std::lock(std::mutex,std::mutex)

对于需要一次锁定两个变量的场景,可以使用std::lock(std::mutex,std::mutex)来一次性锁定两个变量,比如在进行swap交换两个变量的值的场景,可以这么使用:

 #include <mutex>

 class some_big_object
{}; void swap(some_big_object& lhs,some_big_object& rhs)
{} class X
{
private:
some_big_object some_detail;
mutable std::mutex m;
public:
X(some_big_object const& sd):some_detail(sd){} friend void swap(X& lhs, X& rhs)
{
if(&lhs==&rhs)
return;
std::lock(lhs.m,rhs.m);
std::lock_guard<std::mutex> lock_a(lhs.m,std::adopt_lock);
std::lock_guard<std::mutex> lock_b(rhs.m,std::adopt_lock);
swap(lhs.some_detail,rhs.some_detail);
}
}; int main()
{}

例子中some_big_object类的实例是需要保护的对象,在交换时,调用 std::lock() 21行锁住两个互斥量,并且两个 std:lock_guard 实例已经创建好了22,23行,还有一个互斥量。提供 std::adopt_lock 参数除了表示 std::lock_guard 的对象已经上锁外,还表示应使用互斥量现成的锁,而非尝试创建新的互斥锁。 然后调用非锁定版本的swap方法来进行交换操作,以确保操作的原子性(中间态不可修改)

3 std::recursive_mutex可重入锁

对于一个std::mutex对象,如果你已经锁定它了,那么在unock()之前你就不可以再次对他执行lock()函数。但在有些情况下,可能希望在解锁前多次锁定一个mutex。对于这种情况,C++标准库提供了std::recursive_mutex。他与std::mutex用法一样,除了可以在一个线程中多次lock一个recursive_lock。但是它要求,在其他线程使用这个lock前,你必须保证使用的lock次数和unlock的次数是一样的。对于这点,正确使用std::lock_guard<std::recursive_mutex>std::unique_lock<std::recursive_mutex>可以达到目的。

通常情况下,如果你想要使用std::recursive_mutex,都要改变程序的设计。一种常见的情况是,一个类需要保护共享数据,所有的公有函数都锁定一个mutex。但有时候一个公有函数需要调用另一个公有函数,这样,另一个公有函数也需要去锁定mutex,这会导致重复锁定同一个mutex,产生未定义行为。一种投机取巧的办法是将mutex换成std::recursive_mutex,使得另一个函数可以毫无顾忌的执行锁定操作。

但是,这种做法是不提倡的。因为这是一种草率的做法、不成熟的设计。保持一个锁状态去意味着类的稳定性被破坏了,也就是说第二个公有函数是在类的稳定性被破坏的前提下被调用的。更好的做法是,提取出一个被两个个公有函数调用的私有函数,这个私有函数无需锁定mutex

4. std::unique_lock

std::unique_lock相比std::lock_gard具有以下特点:

a) unique_lock本身不存储mutex实例,存储空间占用较大,运行速度要慢一些;

b)  更加丰富的API,锁定方式灵活,实现延迟锁定,随时锁定解锁,至此移动构造函数,实现所有权转移。看一下std::unique_lock的定义

/** @brief A movable scoped lock type.
*
* A unique_lock controls mutex ownership within a scope. Ownership of the
* mutex can be delayed until after construction and can be transferred
* to another unique_lock by move construction or move assignment. If a
* mutex lock is owned when the destructor runs ownership will be released.
*/
template<typename _Mutex>
class unique_lock
{
public:
typedef _Mutex mutex_type; unique_lock() noexcept
: _M_device(), _M_owns(false)
{ } explicit unique_lock(mutex_type& __m)
: _M_device(std::__addressof(__m)), _M_owns(false)
{
lock();
_M_owns = true;
} unique_lock(mutex_type& __m, defer_lock_t) noexcept
: _M_device(std::__addressof(__m)), _M_owns(false)
{ } unique_lock(mutex_type& __m, try_to_lock_t)
: _M_device(std::__addressof(__m)), _M_owns(_M_device->try_lock())
{ } unique_lock(mutex_type& __m, adopt_lock_t) noexcept
: _M_device(std::__addressof(__m)), _M_owns(true)
{
// XXX calling thread owns mutex
} template<typename _Clock, typename _Duration>
unique_lock(mutex_type& __m,
const chrono::time_point<_Clock, _Duration>& __atime)
: _M_device(std::__addressof(__m)),
_M_owns(_M_device->try_lock_until(__atime))
{ } template<typename _Rep, typename _Period>
unique_lock(mutex_type& __m,
const chrono::duration<_Rep, _Period>& __rtime)
: _M_device(std::__addressof(__m)),
_M_owns(_M_device->try_lock_for(__rtime))
{ } ~unique_lock()
{
if (_M_owns)
unlock();
} unique_lock(const unique_lock&) = delete;
unique_lock& operator=(const unique_lock&) = delete; unique_lock(unique_lock&& __u) noexcept
: _M_device(__u._M_device), _M_owns(__u._M_owns)
{
__u._M_device = ;
__u._M_owns = false;
} unique_lock& operator=(unique_lock&& __u) noexcept
{
if(_M_owns)
unlock(); unique_lock(std::move(__u)).swap(*this); __u._M_device = ;
__u._M_owns = false; return *this;
} void
lock()
{
if (!_M_device)
__throw_system_error(int(errc::operation_not_permitted));
else if (_M_owns)
__throw_system_error(int(errc::resource_deadlock_would_occur));
else
{
_M_device->lock();
_M_owns = true;
}
} bool
try_lock()
{
if (!_M_device)
__throw_system_error(int(errc::operation_not_permitted));
else if (_M_owns)
__throw_system_error(int(errc::resource_deadlock_would_occur));
else
{
_M_owns = _M_device->try_lock();
return _M_owns;
}
} template<typename _Clock, typename _Duration>
bool
try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
{
if (!_M_device)
__throw_system_error(int(errc::operation_not_permitted));
else if (_M_owns)
__throw_system_error(int(errc::resource_deadlock_would_occur));
else
{
_M_owns = _M_device->try_lock_until(__atime);
return _M_owns;
}
} template<typename _Rep, typename _Period>
bool
try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
{
if (!_M_device)
__throw_system_error(int(errc::operation_not_permitted));
else if (_M_owns)
__throw_system_error(int(errc::resource_deadlock_would_occur));
else
{
_M_owns = _M_device->try_lock_for(__rtime);
return _M_owns;
}
} void
unlock()
{
if (!_M_owns)
__throw_system_error(int(errc::operation_not_permitted));
else if (_M_device)
{
_M_device->unlock();
_M_owns = false;
}
} void
swap(unique_lock& __u) noexcept
{
std::swap(_M_device, __u._M_device);
std::swap(_M_owns, __u._M_owns);
} mutex_type*
release() noexcept
{
mutex_type* __ret = _M_device;
_M_device = ;
_M_owns = false;
return __ret;
} bool
owns_lock() const noexcept
{ return _M_owns; } explicit operator bool() const noexcept
{ return owns_lock(); } mutex_type*
mutex() const noexcept
{ return _M_device; } private:
mutex_type* _M_device;
bool _M_owns; // XXX use atomic_bool
}; /// Swap overload for unique_lock objects.
template<typename _Mutex>
inline void
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) noexcept
{ __x.swap(__y); } // @} group mutexes
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

使用std::unique_lock进行交换的例子如下:

 #include <mutex>

 class some_big_object
{}; void swap(some_big_object& lhs,some_big_object& rhs)
{} class X
{
private:
some_big_object some_detail;
mutable std::mutex m;
public:
X(some_big_object const& sd):some_detail(sd){} friend void swap(X& lhs, X& rhs)
{
if(&lhs==&rhs)
return;
std::unique_lock<std::mutex> lock_a(lhs.m,std::defer_lock);
std::unique_lock<std::mutex> lock_b(rhs.m,std::defer_lock);
std::lock(lock_a,lock_b);
swap(lhs.some_detail,rhs.some_detail);
}
}; int main()
{}

第21,22行中std::defer_lock的意思是表明互斥量在结构上应该保持解锁状态。这样,就可以被后面调用lock()函数的 std::unique_lock 对象(不是互斥量)所获取.

使用std::unique_lock进行灵活锁定并延迟初始化的例子如下:

 #include <memory>
#include <mutex> struct some_resource
{
void do_something()
{} }; std::shared_ptr<some_resource> resource_ptr;
std::mutex resource_mutex;
void foo()
{
std::unique_lock<std::mutex> lk(resource_mutex);
if(!resource_ptr)
{
resource_ptr.reset(new some_resource);
}
lk.unlock();
resource_ptr->do_something();
} int main()
{
foo();
}

例子中第12行代表某种共享的昂贵初始化资源,我们可以在真正使用到这个被变量的时候,使用std::unique_lock对共享变量进行保护 16行,在检验对象没有初始化17行的时候,动态初始化该资源,并在初始化完成后立刻解锁 21行。后面的22行就可以实现并发处理了。

注意:此案例仅用于说明std::unique_lock的灵活解锁模式,实际上不建议这么使用来作延迟初始化。更好的方式见后面条款6 std::once_flag与std::call_once.

5.死锁

线程在占有了一个资源时,还需要另外的资源才能完成一个操作。而两个以上的线程,互相在等待着别的线程占有的资源,整个系统陷入停顿状态。死锁产生的原因很多:
a). 可能发生在多线程中每个线程需要锁定两个及以上的互斥元的情况,所以叫死锁。

b). 可能与mutex、lock无关。比如,两个线程互相join对方,等待对方终止,而自己不会先终止,这就会陷入死锁。

作者在书中给出了避免死锁的方法有:
a) 同时锁定std::lock(mutex,mutex),

b) 避免重复加锁(如果实在有这种需求,可以使用std::recursive_mutex互斥量,或重新设计接口).

c) 多线程之间以固定顺序获取锁,

d) 使用层次锁,在锁建立的时候,为每种锁确定一个层次值,这个值到后面会允许,低的层次可以在高层次的基础上继续上锁,但反之不行。同时同层的不能再锁。

并且作者在书中给出了一个层次锁的代码案例:

 #include <mutex>
#include <stdexcept> class hierarchical_mutex
{
std::mutex internal_mutex;
unsigned long const hierarchy_value;
unsigned long previous_hierarchy_value;
static thread_local unsigned long this_thread_hierarchy_value; void check_for_hierarchy_violation()
{
if(this_thread_hierarchy_value <= hierarchy_value)
{
throw std::logic_error("mutex hierarchy violated");
}
}
void update_hierarchy_value()
{
previous_hierarchy_value=this_thread_hierarchy_value;
this_thread_hierarchy_value=hierarchy_value;
}
public:
explicit hierarchical_mutex(unsigned long value):
hierarchy_value(value),
previous_hierarchy_value()
{}
void lock()
{
check_for_hierarchy_violation();
internal_mutex.lock();
update_hierarchy_value();
}
void unlock()
{
this_thread_hierarchy_value=previous_hierarchy_value;
internal_mutex.unlock();
}
bool try_lock()
{
check_for_hierarchy_violation();
if(!internal_mutex.try_lock())
return false;
update_hierarchy_value();
return true;
}
};
thread_local unsigned long
hierarchical_mutex::this_thread_hierarchy_value(ULONG_MAX);

说明如下:

a) 使用了thread_local的值来代表当前线程的层级值:this_thread_hierarchy_value。它被初始话为最大值49行,所以最初所有线程都能被锁住。因为其声明中有thread_local,所以每个线程都有其拷贝副本,这样在线程中变量的状态就完全独立了,当从另一个线程进行读取时,变量的状态也是完全独立的。

b) 第一次线程锁住一个hierarchical_mutex时,this_thread_hierarchy_value的值是ULONG_MAX。由于其本身的性质,这个值会大于其他任何值,所以会通过check_for_hierarchy_vilation() 13行的检查。在这种检查方式下,lock()代表内部互斥锁已被锁住31行。一旦成功锁住,你可以更新层级值了32行。

c) 当你现在锁住另一个hierarchical_mutex时,还持有第一个锁,this_thread_hierarchy_value的值将会显示第一个互斥量的层级值。第二个互斥量的层级值必须小于已经持有互斥量检查函数2才能通过。
d) 当前线程存储之前的层级值,所以你可以调用unlock() 36行对层级值进行保存;否则,你就锁不住任何互斥量(第二个互斥量的层级数高于第一个互斥量),即使线程没有持有任何锁。因为你保存了之前的层级值,只有当你持有internal_mutex 20行,并且在解锁内部互斥量 36行之前存储它的层级值,你才能安全的将hierarchical_mutex自身进行存储。这是因为hierarchical_mutex被内部互斥量的锁所保护着。
e) try_lock()与lock()的功能相似,除非在调用internal_mutex的try_lock() 42行失败时,然后你就不能持有对应锁了,所以不必更新层级值,并直接返回false就好。

虽然是运行时检测,但是它至少没有时间依赖性——不必去等待那些导致死锁出现的罕见条件。同时,设计过程需要去拆分应用,互斥量在这样的情况下可以帮助消除很多可能导致死锁的情况。

6. std:once_flag 与std::call_once

使用std::once_flag与std::call_once一起配合来完成延迟初始化的过程。

在《C++并发编程实战》这本书中,作者狠狠批评了大家在使用多线程开发中经常使用的一个双重锁定的代码写法,按照我的印象,好像很多单例模式都是这么写的吧,哈哈!!作者对这种写法使用的形容词是“声名狼藉”!!!直接上案例代码片段:

 struct some_resource
{
void do_something()
{
}
}; std::shared_ptr<some_resource> resource_ptr;
std::mutex resource_mutex;
void undefined_behaviour_with_double_checked_locking()
{
if (!resource_ptr) //
{
std::lock_guard<std::mutex> lk(resource_mutex);
if (!resource_ptr) //
{
resource_ptr.reset(new some_resource); //
}
}
resource_ptr->do_something();
//
}

在此案例中,第12行检查共享变量resource_ptr智能指针是否初始化,如果没有初始化,则14行构造出一个std:lock_gard锁住互斥量,并在15行对共享变量进行再次,在共享变量被保护的情况下,如果共享变量依然没有被初始化,则在17行完成对共享资源的初始化操作。看起来好像是对的,但是内里玄机暗藏。解释说明如下:

因为外部的读取锁12行没有与内部的写入锁进行同步17行。因此就会产生条件竞争,这个条件竞争不仅覆盖指针本身,还会影响到其指向的对象;即使一个线程知道另一个线程完成对指针进行写入,它可能没有看到新创建的some_resource实例,然后调用do_something() 20行后,得到不正确的结果。这个例子是在一种典型的条件竞争——数据竞争,C++标准中这就会被指定为“未定义行为”(underfined behavior)。这种竞争肯定是可以避免的。

解决办法为使用std::once_flag和std::once_call,直接上代码:

 #include <mutex>

 struct connection_info
{}; struct data_packet
{}; struct connection_handle
{
void send_data(data_packet const&)
{}
data_packet receive_data()
{
return data_packet();
}
}; struct remote_connection_manager
{
connection_handle open(connection_info const&)
{
return connection_handle();
}
} connection_manager; class X
{
private:
connection_info connection_details;
connection_handle connection;
std::once_flag connection_init_flag; void open_connection()
{
connection=connection_manager.open(connection_details);
}
public:
X(connection_info const& connection_details_):
connection_details(connection_details_)
{}
void send_data(data_packet const& data)
{
std::call_once(connection_init_flag,&X::open_connection,this);
connection.send_data(data);
}
data_packet receive_data()
{
std::call_once(connection_init_flag,&X::open_connection,this);
return connection.receive_data();
}
};

代码描述的是一个数据访问的例子,我们以数据库访问为例吧,类X封装了数据库访问逻辑,其中的connection_handle connection 创建是比较昂贵的资源。这里使用了std::once_flag connection_init_flag来标识connection是否已经初始化。并在send_data,receive_data

中需要和数据库进行通讯操作时才进行具体的初始化,配对std::call_once来调用具体的初始化代码,并在成功后设置std::once_flag。每个线程只需要使用 std::call_once ,在 std::call_once 的结束时,就能安全的知道指针已经被其他的线程初始化了。使用 std::call_once 比显式使用互斥量消耗的资源更少,特别是当初始化完成后。

当然作者也提出了一个延迟加载的替代方案,具体说明请参见Effective C++ 3nd.

class my_class;
my_class& get_my_class_instance()
{
static my_class instance; // 线程安全的初始化过程
return instance;
}

7. 接口设计导致的竞争条件

先来看一个存在条件竞争的stack的接口设计

 #include <deque>
template<typename T,typename Container=std::deque<T> >
class stack
{
public:
explicit stack(const Container&);
explicit stack(Container&& = Container());
template <class Alloc> explicit stack(const Alloc&);
template <class Alloc> stack(const Container&, const Alloc&);
template <class Alloc> stack(Container&&, const Alloc&);
template <class Alloc> stack(stack&&, const Alloc&); bool empty() const;
size_t size() const;
T& top();
T const& top() const;
void push(T const&);
void push(T&&);
void pop();
void swap(stack&&);
};

在这个stack的接口中,是存在竞争条件存在的。 主要发生在size(),empty(),top()和pop()接口中。

1. 在多线程情况下,即使使用了mutex对stack进行保护,在empty()和size()函数调用完成后,另外的线程可能进行了push()或pop()操作,这个就会导致empty(),size()调用返回的值只能保证在返回那一刻是正确的,后续就不对了。

2. 从stack弹出一个值的操作分为两步进行,即先调用top获取栈顶元素,再调用pop删除元素。用于是采用了两个api来操作的,这个可能会破环中间的不可变态。

解决办法是从接口层面进行重新设计,将size() api去掉,并将top和pop 进行合并,并在pop时进行stack是否为空的检查,如果为空,则抛出异常,看下面代码.

 #include <exception>
#include <stack>
#include <mutex>
#include <memory> struct empty_stack: std::exception
{
const char* what() const throw()
{
return "empty stack";
} }; template<typename T>
class threadsafe_stack
{
private:
std::stack<T> data;
mutable std::mutex m;
public:
threadsafe_stack(){}
threadsafe_stack(const threadsafe_stack& other)
{
std::lock_guard<std::mutex> lock(other.m);
data=other.data;
}
threadsafe_stack& operator=(const threadsafe_stack&) = delete; void push(T new_value)
{
std::lock_guard<std::mutex> lock(m);
data.push(new_value);
}
std::shared_ptr<T> pop()
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw empty_stack();
std::shared_ptr<T> const res(std::make_shared<T>(data.top()));
data.pop();
return res;
}
void pop(T& value)
{
std::lock_guard<std::mutex> lock(m);
if(data.empty()) throw empty_stack();
value=data.top();
data.pop();
}
bool empty() const
{
std::lock_guard<std::mutex> lock(m);
return data.empty();
}
};

线程安全队列

#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory> template<typename T>
class threadsafe_queue
{
private:
mutable std::mutex mut;
std::queue<std::shared_ptr<T> > data_queue;
std::condition_variable data_cond;
public:
threadsafe_queue()
{} void wait_and_pop(T& value)
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk,[this]{return !data_queue.empty();});
value=std::move(*data_queue.front());
data_queue.pop();
} bool try_pop(T& value)
{
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return false;
value=std::move(*data_queue.front());
data_queue.pop();
} std::shared_ptr<T> wait_and_pop()
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk,[this]{return !data_queue.empty();});
std::shared_ptr<T> res=data_queue.front();
data_queue.pop();
return res;
} std::shared_ptr<T> try_pop()
{
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return std::shared_ptr<T>();
std::shared_ptr<T> res=data_queue.front();
data_queue.pop();
return res;
} bool empty() const
{
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
} void push(T new_value)
{
std::shared_ptr<T> data(
std::make_shared<T>(std::move(new_value)));
std::lock_guard<std::mutex> lk(mut);
data_queue.push(data);
data_cond.notify_one();
} };

8. boost::shared_mutex读写锁

对于一些很少变动的数据,可以使用boost库提供的shared_mutex来进行允许并发读,但是独占写的功能,从而以高多线程的程序的并发性能。关于boost库,请参阅boost.org这里不再赘述。

a) 共享锁的获取boost::shared_lock(boost::shared_mutex);

b) 独占锁的获取std::lock_guard<boost::shared_mutex>或std::unique_lock<boost::shared_mutex>。

当任一线程拥有一个共享锁时,这个线程就会尝试获取一个独占锁,直到其他线程放弃他们的锁;同样的,当任一线程拥有一个独占锁是,其他线程就无法获得共享锁或独占锁,直到第一个线程放弃其拥有的锁。

Linux之父说过一句话: Talk is nothing, show me the code,就不多说了,直接上代码示例吧。

 #include <map>
#include <string>
#include <mutex>
#include <boost/thread/shared_mutex.hpp> class dns_entry
{}; class dns_cache
{
std::map<std::string,dns_entry> entries;
boost::shared_mutex entry_mutex;
public:
dns_entry find_entry(std::string const& domain)
{
boost::shared_lock<boost::shared_mutex> lk(entry_mutex);
std::map<std::string,dns_entry>::const_iterator const it=
entries.find(domain);
return (it==entries.end())?dns_entry():it->second;
}
void update_or_add_entry(std::string const& domain,
dns_entry const& dns_details)
{
std::lock_guard<boost::shared_mutex> lk(entry_mutex);
entries[domain]=dns_details;
}
};

代码中:

第16行,find_entry()使用了 boost::shared_lock<> 实例来保护其共享和只读权限;这就使得,多线程可以同时调用find_entry(),且不会出错.

第24行,update_or_add_entry()使用 std::lock_guard<> 实例,当表格需要更新时,为其提供独占访问权限;在update_or_add_entry()函数调用时,独占锁会阻止其他线程对数据结构进行修改,并且这些线程在这时,也不能调用find_entry()。

C++并发编程 02 数据共享的更多相关文章

  1. 并发编程 02—— ConcurrentHashMap

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  2. python并发编程02 /多进程、进程的创建、进程PID、join方法、进程对象属性、守护进程

    python并发编程02 /多进程.进程的创建.进程PID.join方法.进程对象属性.守护进程 目录 python并发编程02 /多进程.进程的创建.进程PID.join方法.进程对象属性.守护进程 ...

  3. python基础-并发编程02

    并发编程 子进程回收的两种方式 join()让主进程等待子进程结束,并回收子进程资源,主进程再结束并回收资源 from multiprocessing import Process import ti ...

  4. Java并发编程(02):线程核心机制,基础概念扩展

    本文源码:GitHub·点这里 || GitEE·点这里 一.线程基本机制 1.概念描述 并发编程的特点是:可以将程序划分为多个分离且独立运行的任务,通过线程来驱动这些独立的任务执行,从而提升整体的效 ...

  5. day39 Pyhton 并发编程02 后

    一.开启子进程的另一种方式 import os from multiprocessing import Process class MyProcess(Process): def __init__(s ...

  6. day39 Pyhton 并发编程02

    一.内容回顾 并发和并行的区别 并发 宏观上是在同时运行的 微观上是一个一个顺序执行 同一时刻只有一个cpu在工作 并行 微观上就是同时执行的 同一时刻不止有一个cpu在工作 什么是进程 一个运行中的 ...

  7. JUC 并发编程--02,生产者和消费者 synchronized的写法 , juc的写法. Condition的用法

    synchronized的写法 class PCdemo{ public static void main(String[] args) { //多个线程操作同一资源 Data data = new ...

  8. 并发编程 01—— ThreadLocal

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  9. 并发编程 20—— AbstractQueuedSynchronizer 深入分析

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

随机推荐

  1. WebStrom 多项目展示及vuejs插件安装

    2. Vuejs 插件安装: ① ②

  2. JavaScript权威指南——跳转语句

    前言:JavaScript中有一类语句叫做跳转语句.从名称就可以看出,它使得JavaScript的执行可以从一个位置跳转到另一个位置. return语句让解释器跳出循环体的执行,并提供本次调用的返回值 ...

  3. 为什么在开发中大部分的时候都在用session而Application基本上都不去使用?

    问题描述 为什么在开发中大部分的时候都在用session而Application基本上都不去使用? 为什么在开发中大部分的时候都在用session而Application基本上都不去使用?为什么在开发 ...

  4. 使用自己的域名解析cnblogs博客(CSDN也可以)

    本文主要介绍怎样使用自己购买的域名指向cnblogs博客 通常来说技术人员都会创建个自己的技术博客,总结下工作中的问题,经验等等,不过某些博客的访问链接的确是不太容易记忆或者输入,对我们分享造成一定的 ...

  5. C高级第一次PTA作业(2)

    6-1 在数组中查找指定元素 本题要求实现一个在数组中查找指定元素的简单函数. 1.设计思路 (1)算法: 第一步:定义一个数组名为a的数组,循环变量i,需要查找的数x,和数组元素的个数n. 第二步: ...

  6. hdu1255 覆盖的面积 线段树-扫描线

    矩形面积并 线段树-扫描线裸题 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...

  7. echar生成雷达图

    function createRadarChart(indicatorData, personData) { var myChart = echarts.init(document.getElemen ...

  8. git代码回退

    情况1.还没有push可能 git add ,commit以后发现代码有点问题,想取消提交,用: reset git reset [--soft | --mixed | --hard] eg:  gi ...

  9. 第2季:从官方例程深度学习海思SDK及API

    2.1.官方mppsample的总体分析2.1.sample的整体架构(1)sample其实是很多个例程,所以有很多个main(2)每一个例程面向一个典型应用,common是通用性主体函数,我们只分析 ...

  10. 【转】每天一个linux命令(59):rcp命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/03/14/2958685.html rcp代表“remote file copy”(远程文件拷贝).该命 ...