更改结构的值
Change value of a struct
我正在学习如何使用多线程,我遇到了一个练习题。
如何使用函数将结构的 bool 值更改为 true? (我对指针不好)。锁应该在主函数中。
目的是锁定线程并防止其他人在达到该状态后执行。
pd:我使用 pthreads
typedef struct Data{
bool used;
}data;
void lock(data *info){
info -> used = true;
}
使用&
运算符获取对象的地址。地址是指向对象的指针。
typedef struct Data{
bool used;
}data;
void lock(data *info){
info -> used = true;
}
int main(int argc, char *argv[])
{
data my_struct = {0};
lock(&my_struct);
if (my_struct.used == true)
printf("It is true!\n");
return 0;
}
我对你的情况的理解是,你想在锁函数中使用 pthread 锁来保护写操作 (info->used = true)。
您应该在使用 lock(data *) 函数之前创建 pthread_mutex_t(用于锁定的数据结构)。下面是一个例子。
#include <stdio.h>
#include <stdbool.h>
#include <pthread.h>
typedef struct data
{
bool used;
}data;
pthread_mutex_t spin_lock;
void* lock(void *xxinfo)
{
if (xxinfo != NULL)
{
data *info= (data *)xxinfo;
pthread_mutex_lock(&spin_lock);
info->used = true;
printf("Set the used status\n");
pthread_mutex_unlock(&spin_lock);
}
return NULL;
}
pthread_t threads[2]; // Used it for demonstrating only
int main()
{
int status = 0;
data some_data;
if(0 != pthread_mutex_init(&spin_lock, NULL))
{
printf("Error: Could not initialize the lock\n");
return -1;
}
status = pthread_create(&threads[0], NULL, &lock, &some_data);
if (status != 0)
{
printf("Error: Could not create 0th thread\n");
}
status = pthread_create(&threads[1], NULL, &lock, &some_data);
if (status != 0)
{
printf("Error: Could not create 1st thread\n");
}
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
pthread_mutex_destroy(&spin_lock);
return 0;
}
在这个例子中,我使用了全局 spin_lock(这不是一个好主意)。在您的代码中考虑将其保留在适当的范围内。我在这里创建了两个线程用于演示。据我了解,他们根本不参加比赛。我希望这能让您了解在您的情况下使用 pthread 锁。您应该只对修改或读取数据的代码部分使用锁定。
请注意,您应该在创建线程之前创建锁 。您还可以将锁作为参数发送给线程。
使用后销毁锁
我正在学习如何使用多线程,我遇到了一个练习题。
如何使用函数将结构的 bool 值更改为 true? (我对指针不好)。锁应该在主函数中。 目的是锁定线程并防止其他人在达到该状态后执行。
pd:我使用 pthreads
typedef struct Data{
bool used;
}data;
void lock(data *info){
info -> used = true;
}
使用&
运算符获取对象的地址。地址是指向对象的指针。
typedef struct Data{
bool used;
}data;
void lock(data *info){
info -> used = true;
}
int main(int argc, char *argv[])
{
data my_struct = {0};
lock(&my_struct);
if (my_struct.used == true)
printf("It is true!\n");
return 0;
}
我对你的情况的理解是,你想在锁函数中使用 pthread 锁来保护写操作 (info->used = true)。
您应该在使用 lock(data *) 函数之前创建 pthread_mutex_t(用于锁定的数据结构)。下面是一个例子。
#include <stdio.h>
#include <stdbool.h>
#include <pthread.h>
typedef struct data
{
bool used;
}data;
pthread_mutex_t spin_lock;
void* lock(void *xxinfo)
{
if (xxinfo != NULL)
{
data *info= (data *)xxinfo;
pthread_mutex_lock(&spin_lock);
info->used = true;
printf("Set the used status\n");
pthread_mutex_unlock(&spin_lock);
}
return NULL;
}
pthread_t threads[2]; // Used it for demonstrating only
int main()
{
int status = 0;
data some_data;
if(0 != pthread_mutex_init(&spin_lock, NULL))
{
printf("Error: Could not initialize the lock\n");
return -1;
}
status = pthread_create(&threads[0], NULL, &lock, &some_data);
if (status != 0)
{
printf("Error: Could not create 0th thread\n");
}
status = pthread_create(&threads[1], NULL, &lock, &some_data);
if (status != 0)
{
printf("Error: Could not create 1st thread\n");
}
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
pthread_mutex_destroy(&spin_lock);
return 0;
}
在这个例子中,我使用了全局 spin_lock(这不是一个好主意)。在您的代码中考虑将其保留在适当的范围内。我在这里创建了两个线程用于演示。据我了解,他们根本不参加比赛。我希望这能让您了解在您的情况下使用 pthread 锁。您应该只对修改或读取数据的代码部分使用锁定。
请注意,您应该在创建线程之前创建锁
使用后销毁锁