无法理解为什么我不能从一个线程获取作为输入的数组,并尝试使用另一个线程打印它 #包括 #包括 pthread_mutex_t mutex; pthread_cond_t cond; void*生产者(void*arg); 无效*消费者(无效*arg); int缓冲区[100]; 静态int n; void*producer(void*arg){/`用于获取用户输入` int i; printf(“\n输入%d个术语的数组”,n); 对于(i=0;iscanf(“%d”、&buffer[i]);现在它对我有效。我已在Update-1中对其进行了更新。
在producer函数中,在thread(pThread)的帮助下,我获取了一个数组的用户输入,然后在cThread的帮助下,我尝试在consumer函数中打印相同的数组。但我只能打印数组的第一个元素。我需要做哪些更改才能将整个数组作为输出?回答您的问题是,在创建新线程之前,您正在为一个线程执行pthread_join()。这意味着,在启动新线程之前,您将退出第一个线程。所以所有的操作都是完全串行的 您应该启动这两个线程,然后按如下方式连接它们无法理解为什么我不能从一个线程获取作为输入的数组,并尝试使用另一个线程打印它 #包括 #包括 pthread_mutex_t mutex; pthread_cond_t cond; void*生产者(void*arg); 无效*消费者(无效*arg); int缓冲区[100]; 静态int n; void*producer(void*arg){/`用于获取用户输入` int i; printf(“\n输入%d个术语的数组”,n); 对于(i=0;iscanf(“%d”、&buffer[i]);现在它对我有效。我已在Update-1中对其进行了更新。,c,linux,pthreads,C,Linux,Pthreads,在producer函数中,在thread(pThread)的帮助下,我获取了一个数组的用户输入,然后在cThread的帮助下,我尝试在consumer函数中打印相同的数组。但我只能打印数组的第一个元素。我需要做哪些更改才能将整个数组作为输出?回答您的问题是,在创建新线程之前,您正在为一个线程执行pthread_join()。这意味着,在启动新线程之前,您将退出第一个线程。所以所有的操作都是完全串行的 您应该启动这两个线程,然后按如下方式连接它们 #include <pthread.h>
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg);
void *consumer(void *arg);
int buffer[100];
static int n;
void *producer(void *arg) { //`For taking user input`
int i;
printf("\n Enter the Array of %d terms",n);
for (i = 0; i < n; i++)
{
pthread_mutex_lock(&mutex);
scanf(" %d\n",&buffer[i]);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
void *consumer(void *arg) { // For printing the input array
int i;
printf("\nConsumer Function");
pthread_mutex_lock(&mutex);
for (i = 0; i < n; i++)
{
printf("%d\n",buffer[i]);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
int i=0;
pthread_mutex_init(&mutex, 0);
pthread_cond_init(&cond, 0);
pthread_t pThread, cThread;
printf("\n Enter no of terms");
scanf("%d",&n);
pthread_create(&pThread, 0, producer, 0);
pthread_join(pThread,NULL);
pthread_create(&cThread, 0, consumer,0);
pthread_join(cThread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void *producer(void *arg) {
while(1) {
pthread_mutex_lock(&mutex);
if(1 == consumerStartedFlag) {
pthread_mutex_unlock(&mutex);
break;
}
pthread_mutex_unlock(&mutex);
usleep(1000); //Sleep for 1ms to prevent this thread from consuming large CPU
}
//Rest of the producer functionality
}
void *consumer(void *arg) { // For printing the input array
pthread_mutex_lock(&mutex);
consumerStartedFlag = 1; //Global flag, intialize it to zero in main before starting threads.
pthread_mutex_unlock(&mutex);
//Rest of the consumer functionality
}
但代码中还有一个问题。在consumer()函数中,您正在为循环解锁内部的互斥锁。我假设,您希望用户在生产者线程中从用户读取一个输入,并在消费者线程中打印该输入值。在这种情况下,您必须将互斥解锁调用移到循环之外。请注意,pthread_cond_wait()将在等待期间对互斥锁进行内部解锁。另外,您需要在pthread\u cond\u wait()
之后打印值,以确保用户输入了输入值
pthread_create(&pThread, 0, producer, 0); //Check return value and handle error
pthread_create(&cThread, 0, consumer,0); //Check return value and handle error
pthread_join(pThread,NULL);
pthread_join(cThread, NULL);
现在,如果生产者首先启动,它将在循环中等待。如果使用者首先启动,它将在pthread\u cond\u wait()
中等待
根据下面的评论更新1
在producer()中,scanf()位于互斥锁内。它将不确定地阻止消费者。因此,您可能无法正确获得输出。因此,将scanf()放在锁外,如下所示
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg);
void *consumer(void *arg);
int buffer[100];
static int n;
void *producer(void *arg) { //`For taking user input`
int i;
printf("\n Enter the Array of %d terms",n);
for (i = 0; i < n; i++)
{
pthread_mutex_lock(&mutex);
scanf(" %d\n",&buffer[i]);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
void *consumer(void *arg) { // For printing the input array
int i;
printf("\nConsumer Function");
pthread_mutex_lock(&mutex);
for (i = 0; i < n; i++)
{
printf("%d\n",buffer[i]);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
int i=0;
pthread_mutex_init(&mutex, 0);
pthread_cond_init(&cond, 0);
pthread_t pThread, cThread;
printf("\n Enter no of terms");
scanf("%d",&n);
pthread_create(&pThread, 0, producer, 0);
pthread_join(pThread,NULL);
pthread_create(&cThread, 0, consumer,0);
pthread_join(cThread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void *producer(void *arg) {
while(1) {
pthread_mutex_lock(&mutex);
if(1 == consumerStartedFlag) {
pthread_mutex_unlock(&mutex);
break;
}
pthread_mutex_unlock(&mutex);
usleep(1000); //Sleep for 1ms to prevent this thread from consuming large CPU
}
//Rest of the producer functionality
}
void *consumer(void *arg) { // For printing the input array
pthread_mutex_lock(&mutex);
consumerStartedFlag = 1; //Global flag, intialize it to zero in main before starting threads.
pthread_mutex_unlock(&mutex);
//Rest of the consumer functionality
}
您的问题的答案是,在创建新线程之前,您正在为一个线程执行pthread_join()。这意味着,在启动新线程之前,您将退出第一个线程。所以所有的操作都是完全串行的
您应该启动这两个线程,然后按如下方式连接它们
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg);
void *consumer(void *arg);
int buffer[100];
static int n;
void *producer(void *arg) { //`For taking user input`
int i;
printf("\n Enter the Array of %d terms",n);
for (i = 0; i < n; i++)
{
pthread_mutex_lock(&mutex);
scanf(" %d\n",&buffer[i]);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
void *consumer(void *arg) { // For printing the input array
int i;
printf("\nConsumer Function");
pthread_mutex_lock(&mutex);
for (i = 0; i < n; i++)
{
printf("%d\n",buffer[i]);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
int i=0;
pthread_mutex_init(&mutex, 0);
pthread_cond_init(&cond, 0);
pthread_t pThread, cThread;
printf("\n Enter no of terms");
scanf("%d",&n);
pthread_create(&pThread, 0, producer, 0);
pthread_join(pThread,NULL);
pthread_create(&cThread, 0, consumer,0);
pthread_join(cThread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void *producer(void *arg) {
while(1) {
pthread_mutex_lock(&mutex);
if(1 == consumerStartedFlag) {
pthread_mutex_unlock(&mutex);
break;
}
pthread_mutex_unlock(&mutex);
usleep(1000); //Sleep for 1ms to prevent this thread from consuming large CPU
}
//Rest of the producer functionality
}
void *consumer(void *arg) { // For printing the input array
pthread_mutex_lock(&mutex);
consumerStartedFlag = 1; //Global flag, intialize it to zero in main before starting threads.
pthread_mutex_unlock(&mutex);
//Rest of the consumer functionality
}
但代码中还有一个问题。在consumer()函数中,您正在为循环解锁内部的互斥锁。我假设,您希望用户在生产者线程中从用户读取一个输入,并在消费者线程中打印该输入值。在这种情况下,您必须将互斥解锁调用移到循环之外。请注意,pthread_cond_wait()将在等待期间对互斥锁进行内部解锁。另外,您需要在pthread\u cond\u wait()
之后打印值,以确保用户输入了输入值
pthread_create(&pThread, 0, producer, 0); //Check return value and handle error
pthread_create(&cThread, 0, consumer,0); //Check return value and handle error
pthread_join(pThread,NULL);
pthread_join(cThread, NULL);
现在,如果生产者首先启动,它将在循环中等待。如果使用者首先启动,它将在pthread\u cond\u wait()
中等待
根据下面的评论更新1
在producer()中,scanf()位于互斥锁内。它将不确定地阻止消费者。因此,您可能无法正确获得输出。因此,将scanf()放在锁外,如下所示
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void *producer(void *arg);
void *consumer(void *arg);
int buffer[100];
static int n;
void *producer(void *arg) { //`For taking user input`
int i;
printf("\n Enter the Array of %d terms",n);
for (i = 0; i < n; i++)
{
pthread_mutex_lock(&mutex);
scanf(" %d\n",&buffer[i]);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
void *consumer(void *arg) { // For printing the input array
int i;
printf("\nConsumer Function");
pthread_mutex_lock(&mutex);
for (i = 0; i < n; i++)
{
printf("%d\n",buffer[i]);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
int i=0;
pthread_mutex_init(&mutex, 0);
pthread_cond_init(&cond, 0);
pthread_t pThread, cThread;
printf("\n Enter no of terms");
scanf("%d",&n);
pthread_create(&pThread, 0, producer, 0);
pthread_join(pThread,NULL);
pthread_create(&cThread, 0, consumer,0);
pthread_join(cThread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void *producer(void *arg) {
while(1) {
pthread_mutex_lock(&mutex);
if(1 == consumerStartedFlag) {
pthread_mutex_unlock(&mutex);
break;
}
pthread_mutex_unlock(&mutex);
usleep(1000); //Sleep for 1ms to prevent this thread from consuming large CPU
}
//Rest of the producer functionality
}
void *consumer(void *arg) { // For printing the input array
pthread_mutex_lock(&mutex);
consumerStartedFlag = 1; //Global flag, intialize it to zero in main before starting threads.
pthread_mutex_unlock(&mutex);
//Rest of the consumer functionality
}
思考函数的作用。你的线程不是并行运行的,而是串行运行的。还有一个问题是消费者线程。互斥解锁在for循环中。它应该在外面。想想函数的作用。你的线程不是并行运行的,而是串行运行的。还有一个问题是消费者线程。互斥解锁在for循环中。它应该在外面。我尝试了你提到的所有方法,但还是无法获得第一个元素之后的数组。知道为什么吗@MayurK@DevanshuTripathi我在回答中添加了一个更新。在给出数组的第一个元素后,它仍然会被卡住,我必须强制终止程序。将scanf置于锁上方后,对于任何大于2的n值,它只接受两个元素作为输入。@DevanshuTripathi删除“scanf”(%d\n)中的“\n”“,&buffer[i]);-->scanf(“%d”和缓冲区[i]);现在它对我起作用了。我在Update-1中对它进行了更新。我尝试了您提到的所有方法,但在第一个元素之后仍然无法获得数组。知道为什么吗@MayurK@DevanshuTripathi我在我的答案中添加了一个更新。在给出数组的第一个元素后,它仍然会被卡住,我必须强制终止t在将scanf置于锁之上之后,对于n大于2的任何值,它只接受两个元素作为输入。@DevanshuTripathi删除“scanf”(%d\n)中的“\n”,以及缓冲区[i]);-->scanf(“%d”、&buffer[i]);现在它对我有效。我已在Update-1中对其进行了更新。