我正在尝试同步读取 5 个文件,以便从一个文件中读取每个字符,然后从下一个文件中读取另一个字符,依此类推。最后数组将打印出内容。我可以读取文件,但同步已关闭。我尝试使用控制变量来修复它,以便仅在文件转动时运行代码块,但我得到了一个不稳定的输出。这是我在关键部分进行工作的部分
while(!feof(drive1)) {
if(control == 0) {
pthread_mutex_lock(&thread1);
//printf("Mutex lock\n");
c = getc(drive1);
printf("%c", (char)c);
control = 1;
pthread_mutex_unlock(&thread1);
//printf("Mutex unlock\n");
} else if(control == 1) {
pthread_mutex_lock(&thread2);
//printf("Mutex lock\n");
a = getc(drive2);
printf("%c", (char)a);
control = 2;
pthread_mutex_unlock(&thread2);
//printf("Mutex unlock\n");
} else if(control == 2) {
pthread_mutex_lock(&thread3);
//printf("Mutex lock\n");
b = getc(drive3);
printf("%c", (char)b);
control = 3;
pthread_mutex_unlock(&thread3);
//printf("Mutex unlock\n");
} else if(control == 3) {
pthread_mutex_lock(&thread4);
//printf("Mutex lock\n");
d = getc(drive4);
printf("%c", (char)d);
control = 4;
pthread_mutex_unlock(&thread4);
//printf("Mutex unlock\n");
} else if(control == 4) {
pthread_mutex_lock(&thread5);
//printf("Mutex lock\n");
e = getc(drive5);
printf("%c", (char)e);
control = 0;
pthread_mutex_unlock(&thread5);
//printf("Mutex unlock\n");
}
我最初尝试只使用一个线程 1 来锁定互斥体并解锁它们,但后来决定创建 5 个线程来看看是否有帮助,但没有。我还必须使用 5 个线程来为每个文件执行此操作。
pthread_t th1;
pthread_create(&th1, NULL, processing, NULL);
pthread_t th2;
pthread_create(&th2, NULL, processing, NULL);
pthread_t th3;
pthread_create(&th3, NULL, processing, NULL);
pthread_t th4;
pthread_create(&th4, NULL, processing, NULL);
pthread_t th5;
pthread_create(&th5, NULL, processing, NULL);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
pthread_join(th3, NULL);
pthread_join(th4, NULL);
pthread_join(th4, NULL);
输出应该是“1234567890abcdefghij”
更新:根据评论之一,我修改了代码以使用变量“test”作为关键部分中正在测试的内容。使用此代码我得到输出 1212。
void* disk1(void* args) {
//Initializing array of files
FILE *drive[5];
drive[0] = fopen("drive1.data", "r");
drive[1] = fopen("drive2.data", "r");
drive[2] = fopen("drive3.data", "r");
drive[3] = fopen("drive4.data", "r");
drive[4] = fopen("drive5.data", "r");
int c;
if(test < initialFileSize * 2) {
pthread_mutex_lock(&thread1);
if(test % 2 == 0) {
c = getc(drive[0]);
printf("%c", (char)c);
test++;
}
if(test % 2 == 1) {
c = getc(drive[1]);
printf("%c", (char)c);
test++;
}
pthread_mutex_unlock(&thread1);
}
}
最佳答案
您可以将它们与单个标志同步,但它们将花费大部分时间等待,因此这可能不是最佳选择:
char shouldRead(){
static unsigned int activeThread = 0; //used as index into the file to read and the thread compare.
char x = 0;
//wait for mutex
pthread_mutex_lock(&readVariableMutex);
//acquired mutex
//read variable to see what thread should run
if(availableThreads[activeThread] == myThreadId){
x = getc(drive[activeThread]); //or w/e your doing when it should run
activeThread++;
activeThread %= MAX_THREADS; //should be 5 right
}
pthread_mutex_unlock(&readVariableMutex);
return x;
}
//where ever you spawn your threads add them to a global availableThreads[MAX_THREADS] array;
//Also, when you intialize your FILE *'s, add them to a drive array.
//The above idea is to wrap access to variable "activeThread" around the mutex such that the controlling thread will always be the next thread, or it will do nothing and release the mutex.
所有线程都会有类似的内容:
while(!eof(drive[thead_id])){ //Each thread needs to know when its at end of file somehow
printf("%c",shouldRead());
}
关于c - 互斥文件读取同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52826113/