gpt4 book ai didi

c++ - 如何使用类中的线程操作 vector ?

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:14 25 4
gpt4 key购买 nike

我打算在 class.cpp 源代码中使用线程而不是通过类成员函数在 main.cpp 源代码中操作 vector 。

我的 main.cpp 是:

#include "stdafx.h"
#include <stdlib.h>
#include <vector>
#include "class.h"

using namespace std;
int main()
{
CLASS* classObject = new CLASS();
classObject->classMemberFucntion(... some arguments....);
return 0;
}

class.h 是:

#include <vector>

#ifndef SLIC_H
#define SLIC_H

class CLASS
{
protected:
std::vector<int> cluster;

public:

void classMemberFunction(... some arguemnts ... );

//creating threads function
void thread2(std::vector<int> *cluster);
void thread3(std::vector<int> *cluster);
void thread4(std::vector<int> *cluster);
};
#endif

class.cpp 是:

#include "class.h"
#include <vector>
#include <thread>
using namespace std;

void CLASS::classMemberFunction(... some arguments ...)
{
thread t2(&CLASS::thread2, CLASS(), &cluster);
t2.join();
thread t3(&CLASS::thread3, CLASS(), &cluster);
t3.join();
thread t4(&CLASS::thread4, CLASS(), &cluster);
t4.join();

//main thread===================================================
for (unsigned n = 0; n < 1000/4; ++n)
{
cluster.push_back(-1);
}
cout << cluster.size() << endl;
}

void CLASS::thread2(std::vector<int> *cluster)
{
for (unsigned n = 0; n < 1000 / 4; ++n)
{
(*cluster).push_back(-1);
}
}

void CLASS::thread3(std::vector<int> *cluster)
{
for (unsigned n = 0; n < 1000 / 4; ++n)
{
(*cluster).push_back(-1);
}
}

void CLASS::thread4(std::vector<int> *cluster)
{
for (unsigned n = 0; n < 1000 / 4; ++n)
{
(*cluster).push_back(-1);
}
}

可以看出,我打算在一个成员函数(成员函数作为主线程,一共有四个线程)内部启动三个线程,并初始化一个vector。我希望程序在调用时打印大小为 1000 cout << cluster.size() << endl;但是每次程序打印的大小都是 250 !我是线程的新手,我不知道我是否使用了正确的语法。所以让我知道我做错了什么。

更新:
所以我像这样修改了我的代码:

//main.cpp
#include "stdafx.h"
#include <stdlib.h>
#include <vector>
#include "class.h"

using namespace std;
int main()
{
myCLASS* classObject = new myCLASS();
classObject->classMemberFunction();
return 0;
}

和:

//class.h
#include "stdafx.h"
#include <vector>
#include <mutex>

#ifndef SLIC_H
#define SLIC_H

class myCLASS
{
protected:
std::vector<int> cluster;
std::mutex cluster_mutex;

public:

void classMemberFunction();
//creating threads function
void thread2();
void thread3();
void thread4();
};
#endif

和:

//class.cpp
#include "stdafx.h"
#include "class.h"
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
using namespace std;

void myCLASS::classMemberFunction(void)
{
thread t2(&myCLASS::thread2, *this);
thread t3(&myCLASS::thread3, *this);
thread t4(&myCLASS::thread4, *this);

//main thread===================================================
for (int n = 0; n < 1000 / 4; ++n)
{
cluster.push_back(-1);
}
t2.join();
t3.join();
t4.join();
cout << cluster.size() << endl;
}

void myCLASS::thread2()
{
cluster_mutex.lock();
for (int n = 0; n < 1000 / 4; ++n)
{
cluster.push_back(-1);
}
cluster_mutex.unlock();
}

void myCLASS::thread3()
{
cluster_mutex.lock();
for (int n = 0; n < 1000 / 4; ++n)
{
cluster.push_back(-1);
}
cluster_mutex.unlock();
}

void myCLASS::thread4()
{
cluster_mutex.lock();
for (int n = 0; n < 1000 / 4; ++n)
{
cluster.push_back(-1);
}
cluster_mutex.unlock();
}

当我在 Visual Studio 2013 中运行代码时,我得到以下信息:

1>------ Build started: Project: threadTEST, Configuration: Release Win32 ------
1> class.cpp
1>D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xlocnum(155): error C2280: 'std::mutex::mutex(const std::mutex &)' : attempting to reference a deleted function
1> D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\mutex(113) : see declaration of 'std::mutex::mutex'
1> This diagnostic occurred in the compiler generated function 'myCLASS::myCLASS(const myCLASS &)'
1> threadTEST.cpp
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我认为默认的复制构造函数有问题!

最佳答案

当你像这样启动线程时:

thread t2(&CLASS::thread2, CLASS(), &cluster);

您将引用传递给新构造的 CLASS 对象 ( CLASS() ),它有自己的成员 cluster .因此线程将推回一个临时对象。

只有决赛 - 你的main所谓的线程正在 cluster 上运行调用成员函数的实例的成员。

可能您想像这样创建线程:

thread t2(&CLASS::thread2, *this, &cluster);

因为它指的是 cluster this的成员对象。

注意您的代码本质上非常线程不安全。您的代码当前有效的唯一原因是因为 .join()在创建下一个线程之前执行此操作,有效地序列化您的代码。

一旦这些线程并行运行,您的代码就会爆炸。段错误、挂起、崩溃和烧毁。共享vector<int> ,所有线程都打算修改,需要受 mutex 保护.

在这种情况下,您可以有效地序列化对共享变量的访问,从而使线程的使用在这种情况下毫无用处,并使代码变得无限复杂。

编辑

class CLASS
{
protected:
std::vector<int> cluster;
std::mutex mtx; // <-- add this
...
};

然后像这样启动线程:

thread t2(&CLASS::thread2, *this);

并更改 threadN成员函数:

void CLASS::thread2( void ) {
mtx.lock();
// ... do things with `cluster`
mtx.unlock();
}

关于c++ - 如何使用类中的线程操作 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com