首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
. In a broader sense, concurrently can also mean in parallel. A thread is part of the process. A process can spawn one or more threads. Threads share the same memory and thus can communicate with each other using this shared memory.gV0拜客生活常识网

To create a thread object, we use the std::thread class template from a header file. Once defined, the thread starts executing. To create a thread that executes a code inside a function, we supply the function name to the thread constructor as a parameter. Example:gV0拜客生活常识网

#include 
#include 
void function1()
{
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Executing function1." << '
';
    }
}
int main()
{
    std::thread t1{ function1 }; // create and start a thread
    t1.join(); // wait for the t1 thread to finish
}

Here we have defined a thread called t1 that executes a function function1. We supply the function name to the std::thread constructor as a first parameter. In a way, our program now has a main thread, which is the main() function itself, and the t1 thread, which was created from the main thread. The .join() member function says: “hey, main thread, please wait for me to finish my work before continuing with yours.” If we left out the .join() function, the main thread would finish executing before the t1 thread has finished its work. We avoid this by joining the child thread to the main thread.gV0拜客生活常识网

If our function accepts parameters, we can pass those parameters when constructing the std::thread object:gV0拜客生活常识网

#include 
#include 
#include 
void function1(const std::string& param)
{
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Executing function1, " << param << '
';
    }
}
int main()
{
    std::thread t1{ function1, "Hello World from a thread." };
    t1.join();
}

We can spawn multiple threads in our program/process by constructing multiple std::thread objects. An example where we have two threads executing two different functions concurrently/simultaneously:gV0拜客生活常识网

#include 
#include 
void function1()
{
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Executing function1." << '
';
    }
}
void function2()
{
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Executing function2." << '
';
    }
}
int main()
{
    std::thread t1{ function1 };
    std::thread t2{ function2 };
    t1.join();
    t2.join();
}

This example creates two threads executing two different functions concurrently.gV0拜客生活常识网

The function1 code executes in a thread t1, and the function2 code executes in a separate thread called t2.gV0拜客生活常识网

We can also have multiple threads executing code from the same function concurrently:gV0拜客生活常识网

#include 
#include 
#include 
void myfunction(const std::string& param)
{
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Executing function from a " << param << '
';
    }
}
int main()
{
    std::thread t1{ myfunction, "Thread 1" };
    std::thread t2{ myfunction, "Thread 2" };
    t1.join();
    t2.join();
}

Threads sometimes need to access the same object. In our example, both threads are accessing the global std::cout object in order to output the data. This can be a problem. Accessing the std::cout object from two different threads at the same time allows one thread to write a little to it, then another thread jumps in and writes a little to it, and we can end up with some strange text in the console window:gV0拜客生活常识网

Executi.Executingng function1.Executing function2.

This means we need to synchronize the access to a shared std::cout object somehow. While one thread is writing to it, we need to ensure that the thread does not write to it.gV0拜客生活常识网

We do so by locking and unlocking mutex-es. A mutex is represented by std::mutex class template from a header. A mutex is a way to synchronize access to shared objects between multiple threads. A thread owns a mutex once it locks the mutex, then performs access to shared data and unlocks the mutex when access to shared data is no longer needed. This ensures only one thread at the time can have access to a shared object, which is std::cout in our case.

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。