gpt4 book ai didi

c - 多线程程序中的错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:17 24 4
gpt4 key购买 nike

我必须编写一个具有共享变量(初始值 = 35 的计数器)和 5 个线程的程序。我必须编写程序,以便每个线程访问计数器的值并将其减 1。这应该一直持续到计数器 = 0。

到目前为止,这是我的代码,但问题是只有一个线程将计数器的值递减为 0。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>

#define NTHREADS 5
void *thread_function1(void *);
void *thread_function2(void *);
void *thread_function3(void *);
void *thread_function4(void *);
void *thread_function5(void *);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
short int counter = 35;


main()
{
pthread_t thread_id[NTHREADS];
pthread_t thread1, thread2, thread3, thread4, thread5;
int status, status2, status3, status4, status5;

status = pthread_create(&thread1, NULL, thread_function1, NULL);
if(status!=0){
fprintf(stderr,"thread 1 failed\n");
}

status2 = pthread_create(&thread2, NULL, thread_function2, NULL);
if(status2!=0){
fprintf(stderr,"thread 2 failed\n");
}

status3 = pthread_create(&thread3, NULL, thread_function3, NULL);
if(status3!=0){
fprintf(stderr,"thread 3 failed\n");
}

status4 = pthread_create(&thread4, NULL, thread_function4, NULL);
if(status4!=0){
printf("thread 4 failed\n");
}

status5 = pthread_create(&thread5, NULL, thread_function5, NULL);
if(status5!=0){
fprintf(stderr,"thread 5 failed\n");
}

//pthread_join(thread1, NULL);
//int x = counter;

printf("created all the threads \n");

printf("joining thread 1");
pthread_join(thread1, NULL);
printf("joining thread 2");
pthread_join(thread2, NULL);
printf("joining thread 3");
pthread_join(thread3, NULL);
printf("joining thread 4");
pthread_join(thread4, NULL);
printf("joining thread 5");
pthread_join(thread5, NULL);

printf("Final counter value: %d\n", counter);
}

void *thread_function1(void *dummyPtr)
{
printf("Thread number %ld\n", pthread_self());

while(counter>0){
srand(time(NULL));

int r = rand()%3;
printf(" %d\n", r);
pthread_mutex_lock( &mutex1 );
printf("entered the mutex");
counter--;
printf(" %d\n", counter);
sleep(r);
pthread_mutex_unlock( &mutex1 );
printf("mutex unlocked");
pthread_yield();
}

}

void *thread_function2(void *dummyPtr)
{
printf("Thread number %ld\n", pthread_self());

while(counter>0){
srand(time(NULL));

int r = rand()%3;
printf(" %d\n", r);
pthread_mutex_lock( &mutex1 );
printf("entered the mutex");
counter--;
printf(" %d\n", counter);
sleep(r);
pthread_mutex_unlock( &mutex1 );
pthread_yield();
}

}

void *thread_function3(void *dummyPtr)
{
printf("Thread number %ld\n", pthread_self());

while(counter>0){
srand(time(NULL));
int r = rand()%3;
printf(" %d\n", r);
pthread_mutex_lock( &mutex1 );
printf("entered the mutex");
counter--;
printf(" %d\n", counter);
sleep(r);
pthread_mutex_unlock( &mutex1 );
pthread_yield();
}

}

void *thread_function4(void *dummyPtr)
{
printf("Thread number %ld\n", pthread_self());

while(counter>0){
srand(time(NULL));

int r = rand()%3;
printf(" %d\n", r);
pthread_mutex_lock( &mutex1 );
printf("entered the mutex");
counter--;
printf(" %d\n", counter);
sleep(r);
pthread_mutex_unlock( &mutex1 );
pthread_yield();
}

}

void *thread_function5(void *dummyPtr)
{
printf("Thread number %ld\n", pthread_self());

while(counter>0){
srand(time(NULL));

int r = rand()%3;
printf(" %d\n", r);
pthread_mutex_lock( &mutex1 );
printf("entered the mutex");
counter--;
printf(" %d\n", counter);
sleep(r);
pthread_mutex_unlock( &mutex1 );
pthread_yield();
}

}

有人可以帮忙吗?谢谢

最佳答案

int r = rand()%3;
/* ... */
sleep(rand);

r是一个0到2之间的随机数,但是你睡了rand秒,这是一个函数的地址,会被隐式转换为unsigned int - 所以线程会休眠很长时间。请改用 sleep(r);

此外,请注意,您在读取 ​​counter 时未持有互斥量(在 while(counter > 0) 中),这可能会导致程序无法正常工作,具体取决于体系结构、缓存和编译器优化。您应该锁定互斥量,将 counter 的值读取到局部变量,然后解锁互斥量并检查值是否为正数。

关于c - 多线程程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7782800/

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