b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

[C/C++] 多线程

电脑杂谈  发布时间:2019-06-20 00:04:51  来源:网络整理

线程池 c_c/c++ 线程池_c 多线程 多任务

最初的进程定义都包含程序、资源及其执行三部分,其中程序通常指代码,资源在操作系统层面上通常包括内存资源、io资源、信号处理等部分,而程序的执行通常理解为执行上下文,包括对cpu的占用,后来发展为线程。线程它允许一个进程执行一个或多个执行路径(即1个进程可以有多个线程,来执行不同的程序),这些执行路径由系统异步调度。一个进程可以包含若干线程,线程可以帮助应用程序同时做几件事,在程序被运行后中,系统首先要做的就是为该程序进程建立一个默认线程,然后程序可以根据需要自行添加或删除相关的线程。

官方文档

1. 最简单的线程

void foo()
{
    // do something...
}
thread t1(foo);
t1.join();
thread t2(foo);
t2.join();

t1.join() 阻塞当前线程,直到t1线程执行完成才执行t2线程

2. detach 容许线程从线程句柄独立开来执行

void foo()
{
    // do something...
}
thread t1(foo);
t1.detach();
thread t2(foo);
t2.detach();

从 thread 对象分离执行的线程,允许执行独立地持续。一旦线程退出,则释放所有分配的资源。调用 detach 后, t1 不再占有任何线程。

线程池 c_c/c++ 线程池_c 多线程 多任务

3. std::bind()

利用std::bind()表达式绑定对象和其非静态成员函数

class Ttest
{
public:
    Ttest(){}
    ~Ttest(){}
    void Hello(int a)
    {
        printf("thread4 hello world. int a = %d\n", a);
    }
private:
};
Ttest test;
thread t4(std::bind(&Ttest::Hello, &test, 100));
printf("t4.joinable = %d\n", t4.joinable());
t4.join();

Lambda表达式

thread t3([](int a, int b) {
        printf("thread3 in a=%d, int b =%d\n", a, b);
    }, 20, 22);
    t3.join();

类模板std::future提供访问异步操作结果的机制。(通过 std::async 、 std::packaged_task 或 std::promise 创建的)异步操作能提供一个 std::future 对象给该异步操作的创建者。

1. std::async

int f(int x, int y)
{
    return std::pow(x, y);
}
future<int> f1 = async(launch::async, f, 100, 2);
int result1 = f1.get();
printf("result1=%d\n", result1);

c 多线程 多任务_c/c++ 线程池_线程池 c

std::packaged_task

int f(int x, int y)
{
    return std::pow(x, y);
}
packaged_task<int(int, int)> task(f);
future<int> result = task.get_future();
thread tf(move(task), 2, 10);
tf.join();
printf("ft(f) : %d\n", result.get());

std::promise

int f(int x, int y)
{
    return pow(x, y);
}
void f_wrapper(promise<int> p, int a, int b)
{
    p.set_value(f(a, b));
}
promise<int> p1;
future<int> f11 = p1.get_future();
thread tp1(f_wrapper, move(p1), 100, 2);
tp1.join();
int result2 = f11.get();
printf("result2=%d\n", result2);

mutex类用于保护共享数据免受从多个线程同时访问。

注意:通常不直接使用 std::mutex : std::unique_lock 、 std::lock_guard 或 std::scoped_lock (C++17 起)以更加异常安全的方式管理锁定。

1. std::lock_guard

class  LogFile
{
public:
    LogFile()
    {
        f_.open("log.txt");
    }
    void shared_print(string s, int value)
    {
        lock_guard<mutex> guard(mu_);
        f_ << "From " << s << " : " << value << endl;
    }
private:
    mutex mu_;
    ofstream f_;
};
void print(LogFile &log)
{
    for (int i = 0; i > -100; i--)
    {
        log.shared_print("print", i);
    }
}
LogFile log;
thread t100(print, std::ref(log));
for (int i = 0; i < 100; i++)
    log.shared_print("From main", i);
t100.join();

c/c++ 线程池_线程池 c_c 多线程 多任务

class  LogFile
{
public:
    LogFile()
    {
        f_.open("log.txt");
    }
    void shared_print(string s, int value)
    {
        lock_guard<mutex> guard(mu_);
        lock_guard<mutex> guard2(mu2_);
        cout << "From " << s << " : " << value << endl;
    }
    void shared_print2(string s, int value)
    {
        lock_guard<mutex> guard2(mu2_);
        lock_guard<mutex> guard(mu_);
        cout << "From " << s << " : " << value << endl;
    }
private:
    mutex mu_;
    mutex mu2_;
    ofstream f_;
};
void print(LogFile &log)
{
    for (int i = 0; i > -100; i--)
    {
        log.shared_print("print", i);
    }
}
LogFile log;
thread t100(print, std::ref(log));
for (int i = 0; i < 100; i++)
    log.shared_print2("From main", i);
t100.join();

死锁的解决方式1:

- shared_print1和shared_print2函数内,guard1,guard2声明顺序保证相同

void shared_print(string s, int value)
    {
        lock_guard<mutex> guard(mu_);
        lock_guard<mutex> guard2(mu2_);
        cout << "From " << s << " : " << value << endl;
    }
    void shared_print2(string s, int value)
    {
        lock_guard<mutex> guard(mu_);
        lock_guard<mutex> guard2(mu2_);
        cout << "From " << s << " : " << value << endl;
    }
void shared_print(string s, int value)
    {
        std::lock(mu_, mu2_);
        lock_guard<mutex> guard(mu_, adopt_lock);
        lock_guard<mutex> guard2(mu2_, adopt_lock);
        cout << "From " << s << " : " << value << endl;
    }
    void shared_print2(string s, int value)
    {
        std::lock(mu_, mu2_);
        lock_guard<mutex> guard2(mu2_, adopt_lock);
        lock_guard<mutex> guard(mu_, adopt_lock);
        cout << "From " << s << " : " << value << endl;
    }

std::unique_lock

类 unique_lock 是通用互斥包装器,允许延迟锁定、锁定的有时限尝试、递归锁定、所有权转移和与条件变量一同使用。

class  LogFile
{
public:
    LogFile()
    {
        f_.open("log.txt");
    }
    void shared_print(string s, int value)
    {
        unique_lock<mutex> lock(mu_, defer_lock);
        lock.lock();
        cout << "From " << s << " : " << value << endl;
        lock.unlock();
    }
private:
    mutex mu_;
    ofstream f_;
};
void print(LogFile &log)
{
    for (int i = 0; i > -100; i--)
    {
        log.shared_print("print", i);
    }
}
LogFile log;
thread t100(print, std::ref(log));
for (int i = 0; i < 100; i++)
    log.shared_print("From main", i);
t100.join();

c 多线程 多任务_c/c++ 线程池_线程池 c

(1)隐藏成就任务 在游戏中玩家会操作许多有名字的单位,他们是用来丰富战役剧情,丰富游戏性的存在,属性一般相当于三级兵,拥有三个装备栏,可以使用重生、血瓶、医疗剂、tp等少数消耗道具。4决策执行与协同应用:对资源消耗情况、个人绩效情况、收支情况对未来经营提供辅助决策,提供即时通讯、网络会议等形式实现协同办公功能。我们来分析一下,为什么这么喜欢苹果公司的产品,第一它薄(外观),第二它快(性能),其实八个要素只要有一个接近最终理想解(理想度=∑有用功能/∑有害功能+资源消耗,最终理想解即有用功能∞,而有害功能、资源消耗0),这个产品就是有市场的,反之是没有出路的,所以我们必须要依靠提高国货的质量来占领市场,这对于经济取得新一轮的发展,意义是相当重大的,我认为系统向最终理想解进化的过程就是供给侧改革。

准确执行一次可调用 (Callable) 对象 fc 多线程 多任务,即使同时从多个线程调用。

若在调用 call_once 的时刻, flag 指示已经调用了 f ,则 call_once 立即返回(称这种对 call_once 的调用为消极)。

class  LogFile
{
public:
    LogFile()
    {
    }
    void shared_print(string s, int value)
    {
        call_once(flag1_, [&]() {
            f_.open("log.txt");
        }); // 仅仅调用一次
        unique_lock<mutex> lock(mu_, defer_lock);
        lock.lock();
        cout << "From " << s << " : " << value << endl;
        lock.unlock();
    }
private:
    mutex mu_;
    ofstream f_;
    once_flag flag1_;
};

condition_variable 类是同步原语,能用于阻塞一个线程,或同时阻塞多个线程c 多线程 多任务,直至另一线程修改共享变量(条件)并通知 condition_variable 。

有意修改变量的线程必须:

- 获得 std::mutex (典型地通过 std::unique_lock )

- 在保有锁时进行修改

- 在 std::condition_variable 上执行 notify_one 或 notify_all (不需要为通知保有锁)


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-107165-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      每日福利
      热点图片
      拼命载入中...