gpt4 book ai didi

c - Pthread编程: Segmentation Fault: 11 while using mutex

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

我正在尝试使用互斥锁来锁定代码区域。我已尝试发布尽可能多的代码。

//Global Variables
int sum;
long long fact=1;
pthread_mutex_t lock;
pthread_t id1,id2;

void *thread_1_And_2(void *accept_1)
{
//Accepting Input
int *a_ptr = (int*) accept_1;
int a= *a_ptr;

//Locking the resource
pthread_mutex_lock(&lock);

//Returning the process ID that is working right now!
pthread_t my_current_id;
my_current_id= pthread_self();

if((pthread_equal(id1,my_current_id))>0)
{

printf("\nI am Thread 1: Processing Sum\n");

printf("\n I am in Thread 1 \n");
for (int i=0; i<a; i++)
{
sum= sum+i;
}
}
else if((pthread_equal(id2, my_current_id))>0)
{
printf("\nI am Thread 2: Processing Factorial\n");
if (a<0)
{
printf("\n Error! Factorial of Negative number not possible \n");
}
else
{
for(int i=1; i<=a; i++)
{
fact=fact*i;
}
}
}

//Unlocking the Mutex
pthread_mutex_unlock(&lock);

return 0;
}

在主函数中,它的初始化和调用如下:

printf("\n Input for thread_1:\n");
int store_1; //= atoi(argv[1]);
printf("\nEnter number up to which you want a sum of natural numbers\n");
scanf("%d", &store_1);

printf("\nInput for thread_2:\n");
int store_2; //= atoi(argv[2]);
printf("\nEnter number whose factorial needs to be found\n");
scanf("%d", &store_2);
//Mutex Initialisation
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\nMutex Initialisation has Failed\n");
return 1;
}

//Creating Threads
pthread_create(&id1, NULL, &thread_1_And_2, &store_1);//For Sum
pthread_create(&id2, NULL, &thread_1_And_2, &store_2);//For Factorial


//Joining All Threads
pthread_join(id1, NULL);
pthread_join(id2, NULL);

//Destroying the Mutex
pthread_mutex_destroy(&lock);


//Printing
printf("\n Output of Thread 1 is Sum = %d \n", sum);
printf("\n Output of Thread 2 is Factorial = %llu \n", fact);

输出:

Asking input for:
"Enter number upto which you want a sum of natural numbers"
Input Given: 5
Asking input for:
"Enter number whose factorial needs to be found"
Input Given: 3

现在屏幕正在打印:

"I am in Thread 1"
"I am in Thread 2"

暂停一会儿

Segmentation Fault: 11

我不知道出了什么问题或哪里出了问题。

最佳答案

下面是更新为使用 C++ std::thread 的同一程序。更简单的程序可以避免崩溃:

#include <thread>
#include <mutex>
#include <cstdio>

//Global Variables
int sum;
long long fact = 1;
//pthread_mutex_t lock;
std::mutex m;
//pthread_t id1, id2;
std::thread id1, id2;

//void *thread_1_And_2(void *accept_1)
void thread_1_And_2(int a)
{
////Accepting Input
//int *a_ptr = (int*)accept_1;
//int a = *a_ptr;

//Locking the resource
//pthread_mutex_lock(&lock);
std::lock_guard<std::mutex> lock(m);

//Returning the process ID that is working right now!
//pthread_t my_current_id;
//my_current_id = pthread_self();
std::thread::id my_current_id = std::this_thread::get_id();

//if((pthread_equal(id1, my_current_id))>0)
if(id1.get_id() == my_current_id)
{

printf("\nI am Thread 1: Processing Sum\n");

printf("\n I am in Thread 1 \n");
for(int i = 0; i < a; i++)
{
sum = sum + i;
}
}
//else if((pthread_equal(id2, my_current_id))>0)
else if(id2.get_id() == my_current_id)
{
printf("\nI am Thread 2: Processing Factorial\n");
if(a < 0)
{
printf("\n Error! Factorial of Negative number not possible \n");
}
else
{
for(int i = 1; i <= a; i++)
{
fact = fact*i;
}
}
}

//Unlocking the Mutex
//pthread_mutex_unlock(&lock); // RAII will do this

//return 0;
}

int main()
{
////Mutex Initialisation
//if(pthread_mutex_init(&lock, NULL) != 0)
//{
// printf("\nMutex Initialisation has Failed\n");
// return 1;
//}
{

std::lock_guard<std::mutex> lock(m);

//Creating Threads
id1 = std::thread{[]() {thread_1_And_2(3); }};
id2 = std::thread{[]() {thread_1_And_2(4); }};
//pthread_create(&id1, NULL, &thread_1_And_2, &store_1);//For Sum
//pthread_create(&id2, NULL, &thread_1_And_2, &store_2);//For Factorial

//Destroying the Mutex
//pthread_mutex_destroy(&lock); // RAII will take care of the lock
}

id1.join();
id2.join();
}

关于c - Pthread编程: Segmentation Fault: 11 while using mutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514218/

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