每一部分都单独注释的,运行时取消注释,将其他部分注释起来就可以。
第一部分:线程管理
#include <boost/thread.hpp>
#include <iostream>
void wait0(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
void thread0()
{
for (int i = 0; i < 5; ++i)
{
wait0(1);
std::cout << i << std::endl;
}
}
void thread1()
{
try
{
for (int i = 0; i < 5; ++i)
{
wait0(1);
std::cout << i << std::endl;
}
}
catch (boost::thread_interrupted&)
{
}
}
int test()
{
std::cout << boost::this_thread::get_id() << std::endl;
std::cout << boost::thread::hardware_concurrency() << std::endl;
system("pause");
return 0;
}
第二部分:线程同步
#include <boost/thread.hpp>
#include <iostream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
void wait1(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
boost::mutex mutex0;
void thread3()
{
for (int i = 0; i < 5; ++i)
{
wait1(1);
mutex0.lock();
std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
mutex0.unlock();
}
}
void thread4()
{
for (int i = 0; i < 5; ++i)
{
wait1(1);
boost::lock_guard<boost::mutex> lock(mutex0);
std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
}
}
boost::timed_mutex mutex1;
void thread5()
{
for (int i = 0; i < 5; ++i)
{
wait1(1);
boost::unique_lock<boost::timed_mutex> lock(mutex1, boost::try_to_lock);
if (!lock.owns_lock())
lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(1));
std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
boost::timed_mutex *m = lock.release();
m->unlock();
}
}
boost::shared_mutex mutex2;
std::vector<int> random_numbers;
void fill0()
{
std::srand(static_cast<unsigned int>(std::time(0)));
for (int i = 0; i < 3; ++i)
{
boost::unique_lock<boost::shared_mutex> lock(mutex2);
random_numbers.push_back(std::rand());
wait1(1);
}
}
void print0()
{
for (int i = 0; i < 3; ++i)
{
wait1(1);
boost::shared_lock<boost::shared_mutex> lock(mutex2);
std::cout << random_numbers.back() << std::endl;
}
}
int sum = 0;
void count0()
{
for (int i = 0; i < 3; ++i)
{
wait1(1);
boost::shared_lock<boost::shared_mutex> lock(mutex2);
sum += random_numbers.back();
}
}
boost::mutex mutex3;
boost::condition_variable_any cond;
std::vector<int> random_numbers1;
void fill1()
{
std::srand(static_cast<unsigned int>(std::time(0)));
for (int i = 0; i < 3; ++i)
{
boost::unique_lock<boost::mutex> lock(mutex3);
random_numbers1.push_back(std::rand());
cond.notify_all();
cond.wait(mutex3);
}
}
void print1()
{
std::size_t next_size = 1;
for (int i = 0; i < 3; ++i)
{
boost::unique_lock<boost::mutex> lock(mutex3);
while (random_numbers1.size() != next_size)
cond.wait(mutex3);
std::cout << random_numbers1.back() << std::endl;
++next_size;
cond.notify_all();
}
}
int test2()
{
boost::thread t1(fill1);
boost::thread t2(print1);
t1.join();
t2.join();
system("pause");
return 0;
}
第三部分:本地存储
#include <boost/thread.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
void init_number_generator0()
{
static bool done = false;
if (!done)
{
done = true;
std::srand(static_cast<unsigned int>(std::time(0)));
}
}
boost::mutex mutex4;
void random_number_generator0()
{
init_number_generator0();
int i = std::rand();
boost::lock_guard<boost::mutex> lock(mutex4);
std::cout << i << std::endl;
}
void init_number_generator1()
{
static boost::thread_specific_ptr<bool> tls;
if (!tls.get())
tls.reset(new bool(false));
if (!*tls)
{
*tls = true;
std::srand(static_cast<unsigned int>(std::time(0)));
}
}
boost::mutex mutex5;
void random_number_generator1()
{
init_number_generator1();
int i = std::rand();
boost::lock_guard<boost::mutex> lock(mutex5);
std::cout << i << std::endl;
}
int main()
{
boost::thread t[3];
for (int i = 0; i < 3; ++i)
t[i] = boost::thread(random_number_generator1);
for (int i = 0; i < 3; ++i)
t[i].join();
system("pause");
return 0;
}