gpt4 book ai didi

c++ - C++中使用了pthread_create的参数——线程函数每次get的值都是一样的,为什么呢?

转载 作者:行者123 更新时间:2023-11-28 02:45:01 24 4
gpt4 key购买 nike

我在C++中使用了pthread_create,程序运行了,但是在pthread函数中,参数结果是一样的。为什么?我用过pthread_mutex_lock,但是没有效果。我找不到原因。

下面的代码:

最小:

const int MAX_THREADS = 5;
class FileCpThread
{
public:
FileCpThread(const string &src,const string & des){
srcFile = src.c_str();
desFile = des.c_str();
}
~FileCpThread(){}
startThreadCopy();

private:
static void *threadCp(void *param);
static int getFileSize(const std::string &filename);
string srcFile;
int num;
string desFile;
};

完成:

bool FileCpThread::startThreadCopy()
{

pthread_t pid[MAX_THREADS];

pthread_mutex_init(&mutex, NULL);

for (int i = 0; i < MAX_THREADS; i++)
{
pthread_mutex_lock(&mutex);
this->num = i;
pthread_mutex_unlock(&mutex);
pthread_create(&pid[i], NULL, threadCp, (void *)this);
}

for (int j = 0; j < MAX_THREADS; j++)
{
pthread_join(pid[j], NULL);
}

return true;
}
int FileCpThread::getFileSize(const string &filename)
{
struct stat st;
memset(&st, 0, sizeof(st));
stat(filename.c_str(), &st);
return st.st_size;
}

void *FileCpThread::threadCp(void *param)
{
FileCpThread *ft = (FileCpThread *)param;
FILE *fin = fopen(ft->srcFile.c_str(), "r+");
FILE *fout = fopen(ft->desFile.c_str(), "w+");

int size = getFileSize(ft->srcFile.c_str());
pthread_mutex_lock(&ft->mutex);
int number = ft->num;
pthread_mutex_unlock(&ft->mutex);
cout << "number:::" << number << endl;

fseek(fin, size * (number) / MAX_THREADS, SEEK_SET);
fseek(fout, size * (number) / MAX_THREADS, SEEK_SET);

char buff[1024] = {'\0'};
int len = 0;
int total = 0;
while ((len = fread(buff, 1, sizeof(buff), fin)) > 0)
{
fwrite(buff, 1, len, fout);
total += len;

if (total > size / MAX_THREADS)
{
break;
}
}

fclose(fin);
fclose(fout);
}

可验证:

bool FileCpThread::startThreadCopy()

pthread_mutex_lock(&ft->mutex);
int number = ft->num;
pthread_mutex_unlock(&ft->mutex);
cout << "number:::" << number << endl;

最佳答案

你在线程函数中得到相同的值,因为你每次都传递相同的值给它。您的代码是:

for (int i = 0; i < MAX_THREADS; i++)
{
pthread_mutex_lock(&mutex);
this->num = i;
pthread_mutex_unlock(&mutex);
pthread_create(&pid[i], NULL, threadCp, (void *)this);
}

this 的值在迭代之间不会改变,所以相同的值被传递给 threadCp每次使用它。

如果您的问题是所有线程都看到相同的 this->num 值,那么问题仍然是你每次都将相同的指针传递给函数,但你也有线程调度的不确定性,并且 Sod 定律规定线程在最后一个创建之前不会被激活,所以他们都在 this->num 中看到相同的值.您必须确保每个线程都获得独立的信息来使用 - 假设每个线程都应该获得独立的信息。


对于 N 个线程,您需要创建一个包含 N 个值的数组——看起来 N int可能就足够了,并且 (a) 适本地设置元素,以及 (b) 将数组的一个元素传递给每个线程。

作为类的一个元素:

int thread_number[MAX_THREADS];

作为初始化:

for (int i = 0; i < MAX_THREADS; i++)
this->thread_number[i] = i;

在调用 pthread_create() 时:

pthread_create(&pid[i], NULL, threadCp, &this->thread_number[i]);

在函数中 threadCp() ,而不是:

FileCpThread *ft = (FileCpThread *)param;

使用:

int number = *(int *)param;

而且您不再需要用于同步的互斥量(至少,不需要控制对线程号的访问)。

如果线程数可变,使用 vector<int> .如果在启动线程之前计算文件的拆分会更明智,那么就这样做(这可能是明智的)。

请注意,让 5 个线程访问单个文件的不同部分可能不会提高整体性能。作为进程间协调的练习,这很好。作为提高整体性能的练习,这可能不是一个好主意。 (原因:文件将位于单个文件系统上,因此将使用相同的 Controller 来访问磁盘的不同部分,从而导致磁头定位争用。性能实际上可能比运行单线程更差。)

关于c++ - C++中使用了pthread_create的参数——线程函数每次get的值都是一样的,为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708958/

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