Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否有任何方法可以使用类的成员函数在并发线程中运行,而不在C+中传递*this+;11?_C++_Macos_C++11_Xcode4.6 - Fatal编程技术网

C++ 是否有任何方法可以使用类的成员函数在并发线程中运行,而不在C+中传递*this+;11?

C++ 是否有任何方法可以使用类的成员函数在并发线程中运行,而不在C+中传递*this+;11?,c++,macos,c++11,xcode4.6,C++,Macos,C++11,Xcode4.6,我是C++11线程的新手,尝试使用类的成员函数在并发线程中运行 在回答我先前的问题时,我收到了以下建议: std::thread t1(&SomeClass::threadFunction, *this, arg1, arg2); 我实施了上述建议。它删除了我的编译错误,但引入了运行时错误。在另一个问题中,我收到了删除所有复制机制的建议。实际上,我不想复制数据,因为代码是用于有限元分析的,需要大量内存 我有什么办法可以做到这一点吗 标题类似于以下内容 SomeClass { v

我是C++11线程的新手,尝试使用类的成员函数在并发线程中运行

在回答我先前的问题时,我收到了以下建议:

std::thread t1(&SomeClass::threadFunction, *this, arg1, arg2);
我实施了上述建议。它删除了我的编译错误,但引入了运行时错误。在另一个问题中,我收到了删除所有复制机制的建议。实际上,我不想复制数据,因为代码是用于有限元分析的,需要大量内存

我有什么办法可以做到这一点吗

标题类似于以下内容

SomeClass {
    vector<int*> someVariable;
public:
    ~SomeClass();
    void threadedMethod(bool, bool); // Inside this method the 
                                     // member vector 'someVariable' is used.

    void someMethod();               // In this function the threadedMethod has
                                     // been used twice to make 2 different thread
};
析构函数类似于以下内容:

SomeClass::~SomeClass() {
    int count  = someVariable.size();
    for(int i=0; i < count; i++) {
        delete someVariable[i];
    }
}
SomeClass::~SomeClass(){
int count=someVariable.size();
for(int i=0;i
threadMethod访问变量。这些操作是数据并行的。因此,没有线程会在同一内存块中写入。同样,读和写存储器是不同的。我想我不需要任何锁

如您所见,我正在使用
*此
,这会导致大量复制。我真的需要避免它。有没有人能给我建议其他方法,让我避免抄袭

如果你需要更多的解释,请让我知道。如果在我的能力范围内,我将尽可能详细地阐述

我使用的是英特尔Mac操作系统X 10.8.3。我在Xcode 4.6.1上编码。编译器是Apple LLVM 4.2(默认编译器)。

参数按值传递给
std::thread
的构造函数。因此,本声明:

std::thread t1(&SomeClass::threadFunction, *this, arg1, arg2);
//                                         ^^^^^
触发
*此
的副本,这不是您想要的。但是,
std::thread
也可以接受指向调用成员函数的对象的指针,就像
std::bind
一样

因此,通过将
this
(而不是
*this
)作为参数传递,将通过值传递并最终复制的是指针,而不是指针对象。这将不会触发您想要的
SomeClass
的复制构造

因此,您应该将上述语句重写如下:

std::thread t1(&SomeClass::threadFunction, this, arg1, arg2);
//                                         ^^^^
参数按值传递给
std::thread
的构造函数。因此,本声明:

std::thread t1(&SomeClass::threadFunction, *this, arg1, arg2);
//                                         ^^^^^
触发
*此
的副本,这不是您想要的。但是,
std::thread
也可以接受指向调用成员函数的对象的指针,就像
std::bind
一样

因此,通过将
this
(而不是
*this
)作为参数传递,将通过值传递并最终复制的是指针,而不是指针对象。这将不会触发您想要的
SomeClass
的复制构造

因此,您应该将上述语句重写如下:

std::thread t1(&SomeClass::threadFunction, this, arg1, arg2);
//                                         ^^^^

你试过只传递
这个
(而不是
*这个
)吗?@AndyProwl,你能把它放到答案部分吗?它像魔法一样工作。谢谢。你救了我一天(好吧,晚上:p)。我贴了一个答案:)它能解决你的问题吗?@AndyProwl。对非常感谢,伙计。不客气,很高兴它能帮上忙!你试过只传递
这个
(而不是
*这个
)吗?@AndyProwl,你能把它放到答案部分吗?它像魔法一样工作。谢谢。你救了我一天(好吧,晚上:p)。我贴了一个答案:)它能解决你的问题吗?@AndyProwl。对非常感谢,伙计。不客气,很高兴它能帮上忙!