gpt4 book ai didi

无法解决此错误 : mutex in c and the lock problems

转载 作者:行者123 更新时间:2023-11-30 21:00:25 25 4
gpt4 key购买 nike

我的 pthreads 程序产生不一致的结果:

  1. 信息

  2. 364.000000

  3. 360.017792(右溶胶)

因此,我认为我在锁定方面遇到了问题,但我无法弄清楚应该在哪里或确切地如何使用互斥体来解决问题。

这是我的代码:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sched.h>
#define _GNU_SOURCE
#include<math.h>
#include <unistd.h>
pthread_mutex_t mutex;

float B=10;
float C=12;
float D=2;
float E=13;
float F=4;
float G=2;
float X,Y,Z,W,V;
float A;

void *t1(void *param);
void *t2(void *param);
void *t3(void *param);
void *t4(void *param);
void *t5(void *param);


int main(int argc, char *argv[])
{



int num_of_args ;
pthread_t tid_1;
pthread_t tid_2;
pthread_t tid_3;
pthread_t tid_4;
pthread_t tid_5;

pthread_mutex_init(&mutex,NULL);

pthread_create(&tid_1, NULL, t1, NULL);
pthread_create(&tid_2, NULL, t2, NULL);
pthread_create(&tid_3, NULL, t3, NULL);
pthread_create(&tid_4, NULL, t4, NULL);
pthread_create(&tid_5, NULL, t5, NULL);




pthread_join(tid_1, NULL);
pthread_join(tid_2, NULL);
pthread_join(tid_3, NULL);
pthread_join(tid_4, NULL);
pthread_join(tid_5, NULL);

A=X+V;

printf("______________________________________________\n");
printf("THE FINAL RESULT = %f\n",A);
pthread_mutex_destroy(&mutex);

return(0);
}

void *t1(void *param)
{
pthread_mutex_lock(&mutex);

printf("first Thread\n");
X=3*B*C;
printf("the result in this thread : %f\n\n",X);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);
}
void *t2(void *param)
{
pthread_mutex_lock(&mutex);

printf("sec Thread\n");
Y=D+E;
printf("the result in this thread : %f\n\n",Y);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);
}
void *t3(void *param)
{
pthread_mutex_lock(&mutex);

printf("third Thread\n");
Z=F-G;
printf("the result in this thread : %f\n\n",Z);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);
}
void *t4(void *param)
{
pthread_mutex_lock(&mutex);

printf("fourth Thread\n");
W=pow(Y,Z);
printf("the result in this thread : %f\n\n",W);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);
}
void *t5(void *param)
{
pthread_mutex_lock(&mutex);

printf("fifth Thread\n");
V=4/W;
printf("the result in this thread : %f\n\n",V);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);
}

我需要如何更改此设置才能确保始终正确的结果?

最佳答案

正如您从程序的输出中看到的,结果不同的情况是当线程按照您可能不期望的顺序执行时。例如

first Thread
the result in this thread : 360.000000

sec Thread
the result in this thread : 15.000000

third Thread
the result in this thread : 2.000000

fourth Thread
the result in this thread : 225.000000

fifth Thread
the result in this thread : 0.017778

______________________________________________
THE FINAL RESULT = 360.017792

在这种情况下,所有线程都按预期顺序执行,结果很好。

另一方面,他们没有这样做,结果很糟糕:

first Thread
the result in this thread : 360.000000

sec Thread
the result in this thread : 15.000000

fifth Thread
the result in this thread : inf

third Thread
the result in this thread : 2.000000

fourth Thread
the result in this thread : 225.000000

______________________________________________
THE FINAL RESULT = inf

事实是,通常您无法预测哪个线程将是下一个锁定给定互斥锁的线程。此外,在这种情况下,线程毫无用处,因为使用线程是为了“同时”完成一些工作,但在这里它们没有。您可以从 main() 函数

一个接一个地运行函数

关于无法解决此错误 : mutex in c and the lock problems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41451092/

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