[英]Wrong Output using Mutex and Pthread Library
The purpose of the below program is to learn Mutex and Pthread library. 下面程序的目的是学习Mutex和Pthread库。 The main() creates three thread. main()创建三个线程。 (thread 1, 2 & 3). (线程1、2和3)。
Each thread one-by-one sequentially, reads one character per file (different file) and stores into the global constant. 每个线程依次一个接一个地读取每个文件(不同文件)一个字符并将其存储到全局常量中。
Example thread 1 reads character 'a' from file1, then wait for thread 2 & 3 to the same (ie read 'b' and 'c' from file2 and file3 respective). 示例线程1从文件1读取字符“ a”,然后等待线程2和3变为相同(即分别从文件2和文件3读取“ b”和“ c”)。
Once the reading is finished, we want the main to print the global constant to file.out 读取完成后,我们希望主体将全局常量打印到file.out中。
I have attempted to program the same but getting incorrect output. 我试图编程相同但得到不正确的输出。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
char glob_var, strtemp[1024];
pthread_mutex_t lock;
int cnt = 0;
char inp[3][10] = {"file-1.in", "file-2.in", "file-3.in"};
void * thread_func(void * arg);
void * thread_func(void * arg)
{
FILE *fp;
char * argu = (char *)arg;
fp = fopen(argu, "r");
while (1) {
pthread_mutex_lock(&lock);
glob_var = fgetc(fp);
if(feof(fp))
{
pthread_mutex_unlock(&lock);
break ;
}
if (glob_var != '\n')
strtemp[cnt++] = glob_var;
pthread_mutex_unlock(&lock);
}
fclose(fp);
return (void *)0;
}
int main(void)
{
FILE * fpout;
pthread_t threads[3];
int i = 0;
fpout = fopen("file.out", "w");
for (i = 0; i < 3; i++)
pthread_create(&threads[i], NULL, thread_func, (void *)inp[i]);
for (i = 0; i < 3; i++)
pthread_join(threads[i], NULL);
strtemp[cnt] = '\0';
cnt = 0;
while(1) {
glob_var = strtemp[cnt++];
fprintf(fpout, "%c\n", glob_var);
if (strtemp[cnt] == '\0') {
break;
}
}
fclose(fpout);
return 0;
}
Input File
File1 File2 File3
a u t
o . #
e p a
i r m
e n $
Output File
From Above Code Desired Output
t a
# u
r t
a o
m .
! #
$ e
s p
m a
u i
. r
p m
r e
n n
Your mutex
here only makes sure no more than one thread accesses your global variables at the same time. 您此处的mutex
只能确保不超过一个线程同时访问全局变量。 It doesn't give you any guarantees on the order in which the threads are scheduled. 它不能为您提供线程调度顺序的任何保证。 For what you want to do, a mutex
is the wrong synchronization primitive because it is designed for exactly that: exclude other threads from accessing the same resource at the same time. 对于您要执行的操作, mutex
是错误的同步原语,因为它的设计正是出于以下目的:排除其他线程同时访问同一资源。
You want to explicitly allow a specific thread after another thread. 您想显式地允许另一个线程之后的特定线程。 This can be done using semaphores
. 这可以使用semaphores
来完成。 You would need one per thread. 每个线程需要一个线程。 So, declare a second sem_t
array globally with 3 entries, initialize the first to 1, the others to 0. Then, pass just a thread number (0 - 2) to your threads and do something like: 因此,使用3个条目全局声明第二个sem_t
数组,将第一个初始化为1,将其他初始化为0。然后,仅将线程号 (0-2)传递给线程,然后执行以下操作:
int next = num + 1;
if (next > 2) next = 0;
sem_wait(sems[num]);
/* your code, accessing inp[num] */
sem_post(sems[next]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.