标签: stdthread

std :: thread,类构造函数和析构函数

在C++ 11中测试线程时,我创建了以下示例:

#include <iostream>
#include <thread>

class Foo {
public:
    Foo(void) {
        std::cout << "Constructor called: " << this << std::endl;
    }
    ~Foo(void) {
        std::cout << "Destructor called: " << this << std::endl;
    }
    void operator()() const {
        std::cout << "Operatior called: " << this << std::endl;
    }
};

void test_normal(void) {
    std::cout << "====> Standard example:" << std::endl;
    Foo f;
}

void test_thread(void) {
    std::cout << "====> Thread example:" << std::endl;
    Foo f;
    std::thread t(f);
    t.detach();
}


int main(int …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

2
推荐指数
1
解决办法
2900
查看次数

如果父/主线程死亡,std :: async调用会发生什么

如果我是对的,std :: async使用一个新线程并调用其中的方法.我想知道如果主线程或父线程死亡会发生什么.控制异步方法的线程是否也会死亡.

c++ std c++11 stdasync stdthread

2
推荐指数
1
解决办法
533
查看次数

将std :: thread对象存储为类成员

我试图std::thread在一个类中保留一个对象.

class GenericWindow
{
    public:
        void Create()
        {
            // ...
            MessageLoopThread = std::thread(&GenericWindow::MessageLoop, *this);
        }
    private:
        std::thread MessageLoopThread;
        void GenericWindow::Destroy()   // Called from the destructor
        {
            SendMessageW(m_hWnd, WM_DESTROY, NULL, NULL);
            UnregisterClassW(m_ClassName.c_str(), m_WindowClass.hInstance);
            MessageLoopThread.join();
        } 
        void GenericWindow::MessageLoop()
        {
            MSG Msg;
            while (GetMessageW(&Msg, NULL, 0, 0))
            {
                if (!IsDialogMessageW(m_hWnd, &Msg))
                {
                    TranslateMessage(&Msg);
                    DispatchMessageW(&Msg);
                }
            }
        }
};      // LINE 66
Run Code Online (Sandbox Code Playgroud)

错误给出:

[Line 66] Error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread'

此错误消息对我没有帮助,我不是要尝试访问std::thread该类的任何私有成员.

我的代码有什么问题?我如何解决它?

c++ winapi multithreading c++11 stdthread

2
推荐指数
1
解决办法
1749
查看次数

无法将类成员函数传递给另一个函数(std :: thread :: thread)

看看这两个代码.

下面的代码工作正常.

void someFunction () {

    // Some unimportant stuff
}

MainM::MainM(QObject *parent) :
    QObject(parent)
{

    std::thread oUpdate (someFunction);

}
Run Code Online (Sandbox Code Playgroud)

此代码抛出错误:

void MainM::someFunction () {      //as a class member


}


MainM::MainM(QObject *parent) :
    QObject(parent)
{

    std::thread oUpdate (someFunction);

}
Run Code Online (Sandbox Code Playgroud)

错误:

error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
     std::thread oUpdate (someFunction);
                                     ^
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers c++11 stdthread

2
推荐指数
1
解决办法
799
查看次数

std::thread 在模板函数之外调用模板函数

我正在尝试从模板函数创建线程,为线程提供另一个模板函数。

我附加了一个出现相同错误的情况的示例。为线程提供一个非模板化的函数(即此处一个 withint和一个 with float)不会导致错误。但是,由于我计划将此函数与许多不同类型一起使用,因此我不想指定模板类型。我还尝试了几种模板类型的指定(例如std::thread<T>std::thread(function<T>),但没有成功。

问题:如何使用模板函数std:thread外调用模板函数?

以下是该情况的最小编译示例,实际上模板是自己的类:

#include <thread>
#include <string>
#include <iostream>

template<class T>
void print(T* value, std::string text)
{
   std::cout << "value: " << *value << std::endl;
   std::cout << text << std::endl;
}

template<class T>
void threadPool(T* value)
{
   std::string text = "this is a text with " + std::to_string(*value);
   std::thread(&print, value, text);
}

int main(void)
{
   unsigned int a = 1;
   float b = 2.5;
   threadPool<unsigned int>(&a); …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 stdthread

2
推荐指数
1
解决办法
2311
查看次数

如何用参数写lambda函数?C++

我想用lambda函数调用一个方法(对于这个例子std :: thread构造函数),传递int值:

int a=10;

std::thread _testThread = thread([a](int _a){
  //do stuff using a or _a ?
});
_testThread.detach();
Run Code Online (Sandbox Code Playgroud)

我不知道如何正确编写这样的函数,我得到这个错误:C2064:term不计算为0参数的函数

c++ argument-passing inline-functions stdthread

2
推荐指数
1
解决办法
8928
查看次数

传递对线程的引用时真的需要 std::ref 吗?

我正在阅读 C++ Concurrency in Action 和第 2 章,我相信即使是函数原型,例如:

void MagicFunc(Data& myData);
Run Code Online (Sandbox Code Playgroud)

旨在如下使用:

Data dataExample;
thread t(MagicFunc,dataExample);
Run Code Online (Sandbox Code Playgroud)

我真的应该这样做

Data dataExample
thread t(MagicFunc,std::ref(dataExample));
Run Code Online (Sandbox Code Playgroud)

否则我期望发生在“dataExample”上的更改将不会发生。具体来说,它声明如下:

尽管 MagicFunc 期望通过引用传递第二个参数,但 std::thread 构造函数 t 不知道;它无视函数预期的参数类型,并盲目地复制提供的值。当它调用 Magicfunc 时,它最终会传递对数据内部副本的引用,而不是对数据本身的引用。因此,当线程完成时,这些更新将被丢弃,因为所提供参数的内部副本被销毁,并且 process_widget_data 将传递一个未更改的 Data myData 而不是正确更新的版本。

但是,使用以下程序对此进行测试

#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <assert.h>
using namespace std;
using namespace std::chrono;

const int NUM_VALS = 50000000;

#define _MULTICORE 

void AddValuesToSlots(vector<int>& vecVals,vector<int>::iterator& begin,
                      int num,int startNum){
    int i = startNum;
    auto end = begin + num;
    for (auto itr …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

2
推荐指数
1
解决办法
938
查看次数

std::thread 在 cygwin 中有效,但在 MinGw 中无效

所以我今天决定尝试一下c++。我下载了MinGw和它附带的g++编译器。我决定测试以下代码:

#include <iostream>
#include <thread>

int foo()
{
    std::cout << "foo" << std::endl;
}

int main()
{
    std::thread t1(foo);
    t1.join();
    std::cout << "done" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下行在命令行上编译它:

g++ -std=c++11 main.cpp
Run Code Online (Sandbox Code Playgroud)

这适用于你好世界。然而这一次,它给了我这个错误:

error: 'thread' is not a member of 'std'

我使用 cygwin 提供的 g++ 尝试了完全相同的代码,并且它有效。那么为什么它在 MinGw 中不起作用呢?是不是已经过时了或者什么的?我想使用 c++11 和 c++14 编译东西,就像在 cygwin 终端上一样,但在 cygwin 环境之外。

c++ cygwin mingw c++11 stdthread

2
推荐指数
1
解决办法
6671
查看次数

使用std :: mutex进行死锁模拟

我有以下示例:

template <typename T>
class container
{
public:
    std::mutex _lock;
    std::set<T> _elements;

    void add(T element)
    {
        _elements.insert(element);
    }

    void remove(T element)
    {
        _elements.erase(element);
    }
};

void exchange(container<int>& cont1, container<int>& cont2, int value)
    {
        cont1._lock.lock();
        std::this_thread::sleep_for(std::chrono::seconds(1));

        cont2._lock.lock();

        cont1.remove(value);
        cont2.add(value);

        cont1._lock.unlock();
        cont2._lock.unlock();
    }

    int main() 
    {
        container<int> cont1, cont2;

        cont1.add(1);
        cont2.add(2);

        std::thread t1(exchange, std::ref(cont1), std::ref(cont2), 1);
        std::thread t2(exchange, std::ref(cont2), std::ref(cont1), 2);

        t1.join();
        t2.join();

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我正在陷入僵局.但是当我使用std :: lock_guard而不是手动锁定和解锁互斥锁时,我没有死锁.为什么?

void exchange(container<int>& cont1, container<int>& cont2, int value)
{
    std::lock_guard<std::mutex>(cont1._lock);
    std::this_thread::sleep_for(std::chrono::seconds(1));

    std::lock_guard<std::mutex>(cont2._lock);

    cont1.remove(value);
    cont2.add(value); …
Run Code Online (Sandbox Code Playgroud)

c++ mutex stdthread

2
推荐指数
1
解决办法
1490
查看次数

可以在C++ 11中检索线程函数的返回值吗?

如果函数具有非void返回值并且我使用该.join函数将其连接,那么有没有办法检索它的返回值?

这是一个简化的例子:

float myfunc(int k)
{
  return exp(k);
}

int main()
{
  std::thread th=std::thread(myfunc, 10);

  th.join();

  //Where is the return value?
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

2
推荐指数
1
解决办法
1729
查看次数