我试图将对共享资源的访问权限授予两种类型的线程。当且仅当该线程属于同一类型时,才可以由多个线程访问它。让我们考虑黑人和白人。当白人使用该资源时,黑人无法使用它,反之亦然。
我试图使用信号量来实现这一点。一旦黑人尝试访问资源,它将增加黑人的数量,如果该数量为1,它将阻止白人访问资源。
问题:当每种类型的线程超过1个时(在我的情况下,id为0的线程从未使用过),就会出现明显的饥饿现象。我试图通过添加一个额外的信号量作为队列来解决此问题。
观察:这与读者-作家问题非常相似,只是存在许多访问标准。 (它可以由相同类型的多个线程使用)最近我一直在围绕这个问题猛烈抨击,我似乎无法理解该如何处理。
现在,输入一些代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX_RAND 100
#define TRUE 1
#define FALSE 0
#define WHITES 3
#define BLACKS 2
#define MAX_WORLOAD 10
sem_t semaphore;
sem_t resource_semaphore;
sem_t service_queue;
volatile int resource = 0;
volatile int currentWhites = 0;
volatile int currentBlacks = 0;
typedef struct
{
char *type;
int *id;
} data;
void *white(void *args)
{
data *thread_data = (data *)args;
int id = *(thread_data->id);
char *type = thread_data->type;
for (int i = 0; i < MAX_WORLOAD; i++)
{
sem_wait(&service_queue);
sem_wait(&semaphore);
sem_post(&service_queue);
currentWhites++;
if (currentWhites == 1)
{
sem_wait(&resource_semaphore);
}
sem_post(&semaphore);
sem_wait(&semaphore);
currentBlacks--;
resource = rand() % MAX_RAND;
printf("Thread %d of type %s has updated resource to %d\n\n", id, type, resource);
if (currentWhites == 0)
{
sem_post(&resource_semaphore);
}
sem_post(&semaphore);
}
}
void *black(void *args)
{
data *thread_data = (data *)args;
int id = *(thread_data->id);
char *type = thread_data->type;
for (int i = 0; i < MAX_WORLOAD; i++)
{
sem_wait(&service_queue);
sem_wait(&semaphore);
sem_post(&service_queue);
currentBlacks++;
if (currentBlacks == 1)
{
sem_wait(&resource_semaphore);
}
sem_post(&semaphore);
sem_wait(&semaphore);
currentBlacks--;
resource = rand() % MAX_RAND;
printf("Thread %d of type %s has updated resource to %d\n\n", id, type, resource);
if (currentBlacks == 0)
{
sem_post(&resource_semaphore);
}
sem_post(&semaphore);
}
}
data *initialize(pthread_t threads[], int size, char *type)
{
data *args = malloc(sizeof(data) * size);
int *id = malloc(sizeof(int));
void *function;
if (type == "WHITE")
{
function = white;
}
else
{
function = black;
}
for (int i = 0; i < size; i++)
{
*id = i;
args[i].type = type;
args[i].id = id;
printf("Initializing %d of type %s\n", *args[i].id, args[i].type);
pthread_create(&threads[i], NULL, function, (void **)&args[i]);
}
return args;
}
void join(pthread_t threads[], int size)
{
for (int i = 0; i < size; i++)
{
pthread_join(threads[i], NULL);
}
}
void initialize_locks()
{
sem_init(&semaphore, 0, 1);
sem_init(&resource_semaphore, 0, 1);
sem_init(&service_queue, 0, 1);
}
int main()
{
initialize_locks();
pthread_t whites[WHITES];
pthread_t blacks[BLACKS];
char *white = "white";
char *black = "black";
data *whites_arg = initialize(whites, WHITES, white);
data *blacks_arg = initialize(blacks, BLACKS, black);
join(whites, WHITES);
join(blacks, BLACKS);
free(whites_arg);
free(blacks_arg);
return 0;
}
答案 0 :(得分:1)
如果要强制在两种类型的线程之间进行交替访问,则可以使用两个信号量。做到这一点,使黑人和白人各有自己的信号量,以0键开始一个信号量,然后以10或类似的东西开始另一个信号量,然后使白人将黑色信号量释放一个键,而黑人将其释放信号量。白色信号量,这样,如果您有10条白线,当其中之一解锁时,您将无法插入第10条白线,但是您将能够将黑线插入其中,以便在所有白色线程释放其键,您将没有白色线程正在访问该事物。
TL; DR:两个相互代替而不是相互发送的信号量将允许在组之间进行交替,但是独立于此操作,您还需要确保白人在黑人仍在时不会走动。