簡體   English   中英

pthread_create的參數在C ++中使用—每次線程函數獲取值都相同,為什么?

[英]The argument of pthread_create was used in C++ — Every time the thread function get the value was the same, why?

我在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個線程訪問單個文件的不同部分可能不會提高整體性能。 作為過程之間協調的練習,這很好。 作為提高整體性能的一項練習,這可能不是一個好主意。 (原因:文件將在單個文件系統上,因此將使用相同的控制器訪問磁盤的各個部分,從而導致磁頭定位爭用。實際上,性能可能比運行單線程還差。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM