免责声明:这篇文章包含对以下答案所做的编辑,所有学分归其各自所有者所有。
我正在尝试实现一个问题,该问题指出资源可能被两种类型的线程使用。每种类型可以有更多的线程。(4 个白色类型的螺纹和 6 个黑色类型的螺纹)。任意数量的黑人可以同时使用该资源。白人也一样。我仍然无法解决这个问题......
我尝试使用互斥锁来实现这一点。我还想考虑此实现可能存在的饥饿问题,因此我决定检查是否已达到某种类型的服务线程数,从而允许其他类型工作。我似乎无法实施最新的。
我还想考虑到,每当其他类型想要使用资源时,它必须等待轮到它,以及其他类型完成使用资源。
编辑:我尝试使用@Nominal-Animal的解决方案,但似乎有时也会出现这种死锁。此外,我在结构中添加了缺少的转弯。现在,我有一些额外的问题:
- 这似乎是正确的,但不起作用,为什么?
- 为什么
isBLack
里面的参数需要双重否定bwlock_lock()
现在,对于一些代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <pthread.h>
#define WHITES 31
#define BLACKS 33
#define TYPES 2
#define W_ID 0
#define B_ID 1
struct bwlock
{
pthread_mutex_t lock; /* Protects the rest of the fields */
pthread_cond_t wait[2]; /* To wait for their turn */
volatile int waiting[2]; /* Number of threads waiting */
volatile int running[2]; /* Number of threads running */
volatile int started[2]; /* Number of threads started in this turn */
const int limit[2]; /* Maximum number of starts per turn */
volatile int black; /* Black threads' turn */
volatile int turn; /*The turn */
};
#define BWLOCK_INIT(whites, blacks, turn) \
{ \
PTHREAD_MUTEX_INITIALIZER, \
{PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER}, \
{0, 0}, {0, 0}, {0, 0}, {whites, blacks}, 0, turn \
}
struct bwlock resource = BWLOCK_INIT(4, 5, W_ID);
void bwlock_unlock(struct bwlock *bwl, const int isblack)
{
const int black = !!isblack; /* 0 if white, 1 if black */
pthread_mutex_lock(&(bwl->lock));
/* This thread is no longer using the resource. */
bwl->running[black]--;
/* Was this the last of this color, with others waiting? */
if (bwl->running[black] <= 0 && bwl->waiting[!black])
{
/* Yes. It's their turn. */
if (bwl->turn == black)
{
bwl->turn = !black;
/* Clear their started counter. */
bwl->started[!black] = 0;
}
/* Wake them all up. */
pthread_cond_broadcast(&(bwl->wait[!black]));
}
pthread_mutex_unlock(&(bwl->lock));
}
void bwlock_lock(struct bwlock *bwl, const int isblack)
{
const int black = !!isblack; /* 0 if white, 1 if black */
pthread_mutex_lock(&(bwl->lock));
while (1)
{
/* No runners or waiters of the other color? */
if (!(bwl->waiting[!black] < 1) && bwl->running[!black] < 1)
{
/* No; we can run. Does this change the turn? */
if (bwl->turn != black)
{
bwl->turn = black;
/* Clear started counter. */
bwl->started[black] = 0;
}
break;
}
/* Still our turn, and not too many started threads? */
if (bwl->turn == black && bwl->started[black] < bwl->limit[black])
break;
/* We must wait. */
bwl->waiting[black]++;
pthread_cond_wait(&(bwl->wait[black]), &(bwl->lock));
bwl->waiting[black]--;
}
bwl->started[black]++;
bwl->running[black]++;
pthread_mutex_unlock(&(bwl->lock));
}
typedef struct
{
int thread_id;
char *type;
int type_id;
} data;
void use_resource(int thread_id, char *type)
{
printf("U: Thread %d of type %s is using the resource!\n", thread_id, type);
}
void take_resource(int thread_id, char *type, int type_id)
{
printf("W:Thread %d of type %s is trying to get the resource!\n", thread_id, type);
bwlock_lock(&resource, type_id);
printf("W:Thread %d of type %sB got resource!\n", thread_id, type);
}
void release_resource(int thread_id, char *type, int type_id)
{
bwlock_unlock(&resource, type_id);
printf("R:Thread %d of type %s has released the resource!\n", thread_id, type);
}
void *doWork(void *arg)
{
data thread_data = *((data *)arg);
int thread_id = thread_data.thread_id;
char *type = thread_data.type;
int type_id = thread_data.type_id;
take_resource(thread_id, type, type_id);
use_resource(thread_id, type);
release_resource(thread_id, type, type_id);
return NULL;
}
data *initialize(pthread_t threads[], int size, char *type, int type_id)
{
data *args = malloc(sizeof(data) * size);
for (int i = 0; i < size; i++)
{
args[i].type = type;
args[i].thread_id = i;
args[i].type_id = type_id;
pthread_create(&threads[i], NULL, doWork, (void **)&args[i]);
}
return args;
}
void join(pthread_t threads[], int size)
{
for (int i = 0; i < size; i++)
{
pthread_join(threads[i], NULL);
}
}
int main()
{
pthread_t whites[WHITES];
pthread_t blacks[BLACKS];
char *white = "WHITE";
char *black = "BLACK";
data *w_args = initialize(whites, WHITES, white, W_ID);
data *b_args = initialize(blacks, BLACKS, black, B_ID);
join(whites, WHITES);
join(blacks, BLACKS);
free(w_args);
free(b_args);
return 0;
}
这是使用编译的gcc -g -o ba blacks_whites.c -Wall -Wextra -pthread
。