简体   繁体   English

如何使用std :: thread?

[英]How to use std::thread?

When I use std::thread like this: 当我像这样使用std::thread

func()
{
   std::thread(std::bind(&foo, this));
}

the thread object is allocated in stack and is destroyed when func() returns. 线程对象在堆栈中分配,并在func()返回时销毁。 So I try to use it like: 所以我尝试像这样使用它:

func()
{
   std::thread* threadPtr = new std::thread(std::bind(&foo, this));
}

Where should I delete threadPtr ? 我应该在哪里delete threadPtr And how can I create a thread that is initially suspended? 以及如何创建最初被挂起的线程?

If you want the thread to run independently, you need to use the detach() method on the object. 如果希望线程独立运行,则需要在对象上使用detach()方法。 Otherwise, the thread destructor will terminate your program if the object is destroyed while the thread is still running. 否则,如果在线程仍在运行时销毁了对象,则thread析构函数将终止您的程序。

Threads start running as soon as they are created. 线程在创建后即开始运行。 You cannot create a thread object in a suspended state. 您不能在挂起状态下创建线程对象。 You can either store the arguments to create the thread instead of actually creating it (possibly using, for instance, std::function ), or make it block on a mutex or a condition variable until you are ready to let it run. 您可以存储用于创建线程的参数,而不是实际创建线程(可能使用例如std::function ),或者使其在互斥量或条件变量上阻塞,直到准备好让其运行为止。

How to use std::thread? 如何使用std :: thread?

It depends on what you're doing in the thread, but most likely you'll want to use join . 这取决于您在线程中执行的操作,但是最有可能要使用join It is also possible to use detach but care must be taken to make sure it doesn't use any resources that may be destroyed while its executing. 也可以使用detach但是必须注意确保它不使用任何在执行过程中可能被破坏的资源。

std::thread(std::bind(&foo, this)); std :: thread(std :: bind(&foo,this));

This doesn't make any sense. 这没有任何意义。 You're binding (using bind is not necessary) a this pointer but &foo is not a pointer to a member function (which would look like &Foo::foo ). 您正在绑定(不必使用bindthis指针,但是&foo不是指向成员函数的指针(看起来像&Foo::foo )。 Assuming you meant to use a pointer to a member function that would mean func is also a member function of the same class (ie, since it has access to the this pointer) therefore the following code gives you an example of something you could do . 假设您打算使用指向成员函数的指针,那将意味着func也是同一类的成员函数(即,由于它可以访问this指针),因此以下代码为您提供了可以执行的示例。

Example Code 范例程式码

#include <iostream>
#include <thread>

class Foo
{
public:
    Foo() = default;

    ~Foo()
    {
        if (mThread.joinable())
        {
            mThread.join();
        }
    }

    void foo()
    {
        std::cout << "Foo::foo\n";
    }

    void func()
    {
        if (mThread.joinable())
        {
            mThread.join();
        }

        // Creates the thread without using 'std::bind'
        mThread = std::thread(&Foo::foo, this);
    }

private:
    std::thread mThread;
};

int main()
{
    {
        Foo f;
        f.func();
        f.func();
    }

    return 0;
}

Example Output 示例输出

Foo::foo
Foo::foo

Where should I delete threadPtr? 我应该在哪里删除threadPtr?

I wouldn't dynamically allocate the thread, but in the above example code you would delete it after joining. 我不会动态分配线程,但是在上面的示例代码中,您将在加入后将其删除。

how can I create a thread that is initially suspended? 如何创建最初被挂起的线程?

C++ doesn't directly support this but you can use platform specific APIs with std::thread::native_handle . C ++不直接支持此功能,但是您可以将平台特定的API与std::thread::native_handle Note , if you wanted to block just once at the very start you could possibly achieve this with a synchronization primitive (eg, block on a std::mutex before running the actual thread). 注意 ,如果您只想在一开始就阻塞一次,则可以使用同步原语来实现(例如,在运行实际线程之前在std::mutex上阻塞)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM