STL thread - yszheda/wiki GitHub Wiki
- Multithreading in C++0x Series
#include <thread>
#include <iostream>
class SayHello
{
public:
void greeting(std::string const& message) const
{
std::cout<<message<<std::endl;
}
};
int main()
{
std::shared_ptr<SayHello> p(new SayHello);
std::thread t(&SayHello::greeting,p,"goodbye");
t.join();
}
// Exception Safety and Mutexes
std::mutex m;
std::string s;
void append_with_lock_guard(std::string const& extra)
{
std::lock_guard<std::mutex> lk(m);
s+=extra;
}
void append_with_manual_lock(std::string const& extra)
{
m.lock();
try
{
s+=extra;
m.unlock();
}
catch(...)
{
m.unlock();
throw;
}
}
class account
{
std::mutex m;
currency_value balance;
public:
friend void transfer(account& from,account& to,
currency_value amount)
{
std::lock(from.m,to.m);
std::lock_guard<std::mutex> lock_from(from.m,std::adopt_lock);
std::lock_guard<std::mutex> lock_to(to.m,std::adopt_lock);
from.balance -= amount;
to.balance += amount;
}
friend void transfer(account& from,account& to,
currency_value amount)
{
std::unique_lock<std::mutex> lock_from(from.m,std::defer_lock);
std::unique_lock<std::mutex> lock_to(to.m,std::defer_lock);
std::lock(lock_from,lock_to);
from.balance -= amount;
to.balance += amount;
}
};
// Divide and Conquer using std::future
template<typename Iterator,typename Func>
void parallel_for_each(Iterator first,Iterator last,Func f)
{
ptrdiff_t const range_length=last-first;
if(!range_length)
return;
if(range_length==1)
{
f(*first);
return;
}
Iterator const mid=first+(range_length/2);
std::future<void> bgtask=std::async(¶llel_for_each<Iterator,Func>,
first,mid,f);
try
{
parallel_for_each(mid,last,f);
}
catch(...)
{
bgtask.wait();
throw;
}
bgtask.get();
}
class aio
{
class io_request
{
std::streambuf* is;
unsigned read_count;
std::promise<std::vector<char> > p;
public:
explicit io_request(std::streambuf& is_,unsigned count_):
is(&is_),read_count(count_)
{}
io_request(io_request&& other):
is(other.is),
read_count(other.read_count),
p(std::move(other.p))
{}
io_request():
is(0),read_count(0)
{}
std::future<std::vector<char> > get_future()
{
return p.get_future();
}
void process()
{
try
{
std::vector<char> buffer(read_count);
unsigned amount_read=0;
while((amount_read != read_count) &&
(is->sgetc()!=std::char_traits<char>::eof()))
{
amount_read+=is->sgetn(&buffer[amount_read],read_count-amount_read);
}
buffer.resize(amount_read);
p.set_value(std::move(buffer));
}
catch(...)
{
p.set_exception(std::current_exception());
}
}
};
thread_safe_queue<io_request> request_queue;
std::atomic_bool done;
void io_thread()
{
while(!done)
{
io_request req=request_queue.pop();
req.process();
}
}
std::thread iot;
public:
aio():
done(false),
iot(&aio::io_thread,this)
{}
std::future<std::vector<char> > queue_read(std::streambuf& is,unsigned count)
{
io_request req(is,count);
std::future<std::vector<char> > f(req.get_future());
request_queue.push(std::move(req));
return f;
}
~aio()
{
done=true;
request_queue.push(io_request());
iot.join();
}
};
void do_stuff()
{}
void process_data(std::vector<char> v)
{
for(unsigned i=0;i<v.size();++i)
{
std::cout<<v[i];
}
std::cout<<std::endl;
}
int main()
{
aio async_io;
std::filebuf f;
f.open("my_file.dat",std::ios::in | std::ios::binary);
std::future<std::vector<char> > fv=async_io.queue_read(f,1048576);
do_stuff();
process_data(fv.get());
return 0;
}
- C++0x thread interruption
- How to interrupt a waiting C++0x thread?
- How do I terminate a thread in C++11?
- abort, terminate or exit?
- How to terminate a C++ std::thread?
- http://en.cppreference.com/w/cpp/thread/thread/native_handle
- Obtaining thread Core affinity in C++ 11 through pthreads
- How to ensure that std::thread are created in multi core?
-
Is there an implementation of std::async which uses thread pool?
-
The promises and challenges of std::async task-based parallelism in C++11
template<typename R>
bool is_ready(std::future<R> const& f)
{ return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }
-
- C++11
thread_local
- C++11
- http://en.cppreference.com/w/cpp/thread/yield
- Multithreading, when to yield versus sleep
- C++11 Thread waiting behaviour: std::this_thread::yield() vs. std::this_thread::sleep_for( std::chrono::milliseconds(1) )
- std::this_thread::yield() vs std::this_thread::sleep_for()?
- Giving up the time slice for current thread in C++ [duplicate]
- http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until
- http://en.cppreference.com/w/cpp/thread/condition_variable/wait_for
- Stopping long-sleep threads
struct timer_killer {
// returns false if killed:
template<class R, class P>
bool wait_for( std::chrono::duration<R,P> const& time ) const {
std::unique_lock<std::mutex> lock(m);
return !cv.wait_for(lock, time, [&]{return terminate;});
}
void kill() {
{
std::unique_lock<std::mutex> lock(m);
terminate=true; // should be modified inside mutex lock
}
cv.notify_all(); // it is safe, and *sometimes* optimal, to do this outside the lock
}
// I like to explicitly delete/default special member functions:
timer_killer() = default;
timer_killer(timer_killer&&)=delete;
timer_killer(timer_killer const&)=delete;
timer_killer& operator=(timer_killer&&)=delete;
timer_killer& operator=(timer_killer const&)=delete;
private:
std::condition_variable cv;
mutable std::mutex m;
bool terminate = false;
};
- C++ Core Guidelines: Be Aware of the Traps of Condition Variables
- How does condition_variable::wait_for() deal with spurious wakeups?
- Always declare std::mutex as mutable in C++11?
- Should mutexes be mutable?
- GotW #6a Solution: Const-Correctness, Part 1
- https://github.com/isocpp/CppCoreGuidelines/issues/655
- How Sutter’s Wrong About const in C++ 11
- https://en.cppreference.com/w/cpp/thread/shared_timed_mutex
- https://en.cppreference.com/w/cpp/thread/shared_mutex
- How would you implement your own reader/writer lock in C++11?
- C++11 multiple read and one write thread mutex [duplicate]
- http://www.cplusplus.com/reference/mutex/unique_lock/try_lock/
- 锁类的策略:std :: try_to_lock,std :: adopt_lock,std :: defer_lock
- What's the difference between first locking and creating a lock_guard(adopt_lock) and creating a unique_lock(defer_lock) and locking?
- http://www.cplusplus.com/reference/mutex/adopt_lock/
- How to get integer thread id in c++11
- https://en.cppreference.com/w/cpp/thread/get_id
- https://en.cppreference.com/w/cpp/thread/thread/id/hash
- How to use std::atomic<> effectively for non-primitive types?
- https://cplusplus.github.io/LWG/issue3012
- https://en.wikipedia.org/wiki/Active_object
- Prefer Using Active Objects Instead of Naked Threads
- std::async analogue for specified thread
- https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag
- Difference between execution policies and when to use them
- What's the point of std::execution::par_unseq? [duplicate]
- thread terminate called without an active exception
- C++ terminate called without an active exception
boost::condition_variable_any::~condition_variable_any
Assertion failed !pthread_mutex_destroy(&internal_mutex)
- https://www.boost.org/doc/libs/1_51_0/boost/thread/pthread/condition_variable.hpp
- boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' failed
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42734
- Why does this simple std::thread example not work?
- Raspberry Pi C++11 std::thread: pure virtual method called
- https://github.com/android-ndk/ndk/issues/151
- https://github.com/ARM-software/ComputeLibrary/issues/12
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62100
- https://gcc.gnu.org/ml/gcc-help/2010-05/msg00029.html