小编Cod*_*esi的帖子

在 For 循环 C++ 中创建线程

我一直在 for 循环中尝试多线程。基本的代码块将是这样的,

void function(int a, string b, MyClass &Obj, MyClass2 &Obj2)
{

//execution part

}

void anotherclass::MembrFunc()
{

std::vector<std::thread*> ThreadVector;

for(some condition)
{

    std::thread *mythread(function,a,b,obj1,obj2) // creating a thread that will run parallely until it satisfies for loop condition
    ThreadVector.push_back(mythread)

}
for(condition to join threads in threadvector)
{

    Threadvector[index].join();

}

}
Run Code Online (Sandbox Code Playgroud)

对于此块,我收到一条错误消息,提示“void* function() 的值类型不能用于初始化 std::thread 的实体类型......

我如何纠正我的错误.. 有没有其他有效的方法来做到这一点。

c++ multithreading

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

从静态成员函数访问私有非静态类变量-C ++

私有非静态变量/方法可以在静态函数中访问吗?如果是,那么“专用”访问修饰符的用途是什么?请检查以下代码。

// Testclassheader.h file
class TestClass{
private:
int TestVariable;    //Private Variable
int TestFunction();   //Private Method

public:
static void TeststaticFn();   //Static Method

};

void TestClass::TeststaticFn()
{
   TestClass TestObj;
   TestObj.TestVariable = 10; // Compiles perfectly
   TestObj.TestFunction(); //Compiles Perfectly

}

// Another Class
//#include "Testclassheader.h"
class AnotherClass{
public:
int DummyFunc();
};

int AnotherClass::DummyFunc()
{
  TestClass AnotherObj;
  AnotherObj.TestVariable = 15; //error: 'TestVariable' is a private member of 'TestClass'
  AnotherObj.TestFunction();    //error: 'TestFunction' is a private member of 'TestClass'
}

Run Code Online (Sandbox Code Playgroud)

我在Visual Studio 12中尝试了上述代码。谁能解释为什么私有变量/方法可以在静态方法中访问(实际上不应该)?

c++ oop static-methods private

-3
推荐指数
1
解决办法
104
查看次数

标签 统计

c++ ×2

multithreading ×1

oop ×1

private ×1

static-methods ×1