多任务互斥与同步(互斥锁, 读写锁, 条件变量, 信号量, 命名信号量): 作业
This commit is contained in:
parent
8e8af08fcf
commit
8b63cc049a
|
@ -7,7 +7,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
pthread_mutex_t mutux = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
typedef struct node
|
typedef struct node
|
||||||
{
|
{
|
||||||
|
@ -21,7 +21,7 @@ void *find(void *data)
|
||||||
{
|
{
|
||||||
int n = 0; // 标识待查询内容在链表中的索引号
|
int n = 0; // 标识待查询内容在链表中的索引号
|
||||||
char *name = (char *)data;
|
char *name = (char *)data;
|
||||||
pthread_mutex_lock(&mutux);
|
pthread_mutex_lock(&mutex);
|
||||||
// 查询
|
// 查询
|
||||||
Node *p = head->next;
|
Node *p = head->next;
|
||||||
while (p != NULL)
|
while (p != NULL)
|
||||||
|
@ -29,7 +29,7 @@ void *find(void *data)
|
||||||
if (strcmp(p->name, name) == 0)
|
if (strcmp(p->name, name) == 0)
|
||||||
{
|
{
|
||||||
printf("%s 在链表的索引为 %d\n", name, n);
|
printf("%s 在链表的索引为 %d\n", name, n);
|
||||||
pthread_mutex_unlock(&mutux);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ void *find(void *data)
|
||||||
p = p->next;
|
p = p->next;
|
||||||
}
|
}
|
||||||
printf("%s 在链表中未找到\n", name);
|
printf("%s 在链表中未找到\n", name);
|
||||||
pthread_mutex_unlock(&mutux);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -89,5 +89,7 @@ int main(int argc, char const *argv[])
|
||||||
pthread_join(t[i], NULL);
|
pthread_join(t[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_destroy(&mutex);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue