#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
pthread_mutex_t ma,mb;
int a;
void* task(void* arg)
{
while(1)
{
pthread_mutex_lock(&ma);
scanf("%d",&a);
sleep(1);
pthread_mutex_unlock(&mb);
}
}
void* task2(void* arg)
{
while(1)
{
pthread_mutex_lock(&mb);
printf("%d\n",a);
sleep(1);
pthread_mutex_unlock(&ma);
}
}
int main(int argc, char const *argv[])
{
pthread_mutex_init(&ma,0);
pthread_mutex_init(&mb,0);
pthread_mutex_lock(&mb);
pthread_t id;
pthread_create(&id,0,task,0);
pthread_detach(id);
pthread_t id2;
pthread_create(&id2,0,task2,0);
pthread_detach(id2);
while(1){}
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t ma,mb,mc,md,me;//创建线程属性的变量是互斥锁
void* task(void* arg) {
while (1) {
pthread_mutex_lock(&mb);//上锁自己的互斥锁
printf("2\n");
sleep(1);
pthread_mutex_unlock(&mc);//解锁下一个线程的互斥锁
}
}
void* task2(void* arg) {
while (1) {
pthread_mutex_lock(&ma);//上锁自己的互斥锁
printf("1\n");
sleep(1);
pthread_mutex_unlock(&mb);//解锁下一个线程的互斥锁
}
}
void* task3(void* arg) {
while (1) {
pthread_mutex_lock(&mc);//上锁自己的互斥锁
printf("3\n");
sleep(1);
pthread_mutex_unlock(&md);//解锁下一个线程的互斥锁
}
}
void* task4(void* arg) {
while (1) {
pthread_mutex_lock(&md);//上锁自己的互斥锁
printf("4\n");
sleep(1);
pthread_mutex_unlock(&me);//解锁下一个线程的互斥锁
}
}
void* task5(void* arg) {
while (1) {
pthread_mutex_lock(&me);//上锁自己的互斥锁
printf("5\n");
sleep(1);
pthread_mutex_unlock(&ma);//解锁下一个线程的互斥锁
}
}
int main(int argc, const char *argv[]) {
pthread_mutex_init(&ma, 0);
pthread_mutex_init(&mb, 0);
pthread_mutex_lock(&mb);
pthread_mutex_init(&mc, 0);
pthread_mutex_lock(&mc);
pthread_mutex_init(&md, 0);
pthread_mutex_lock(&md);
pthread_mutex_init(&me, 0);
pthread_mutex_lock(&me);
pthread_t id, id2, id3 ,id4, id5;
pthread_create(&id, 0, task, 0);
pthread_detach(id);
pthread_create(&id2, 0, task2, 0);
pthread_detach(id2);
pthread_create(&id3, 0, task3, 0);
pthread_detach(id3);
pthread_create(&id4, 0, task4, 0);
pthread_detach(id4);
pthread_create(&id5, 0, task5, 0);
pthread_detach(id5);
while (1) {
/* pthread_mutex_lock(&ma);//上锁自己的互斥锁
printf("A\n");
sleep(1);
pthread_mutex_unlock(&mb);//解锁下一个线程的互斥锁
*/
}
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h> // 为了使用 rand() 需要包含这个头文件
#define TRAIN_COUNT 5
pthread_mutex_t fast_tunnel_mutex;// 定义快速隧道锁
pthread_mutex_t normal_tunnel_mutex;// 定义普通隧道锁
void* train_thread(void* arg) {
int train_type = *(int*)arg;// 获取火车类型
char* train_name = train_type == 1 ? "复兴号" : "绿皮扭扭车";// 获取火车名称
if (train_type == 1) {
// 复兴号,随机选择隧道
int tunnel_choice = rand() % 2;// 随机选择隧道
if (tunnel_choice == 0) {
pthread_mutex_lock(&fast_tunnel_mutex);
printf("%s 正在通过快速隧道\n", train_name);
sleep(1); // 假设通过隧道需要1秒
pthread_mutex_unlock(&fast_tunnel_mutex);
} else {
pthread_mutex_lock(&normal_tunnel_mutex);
printf("%s 正在通过普通隧道\n", train_name);
sleep(2); // 假设通过普通隧道需要2秒
pthread_mutex_unlock(&normal_tunnel_mutex);
}
} else {
// 绿皮扭扭车,只能走普通隧道
pthread_mutex_lock(&normal_tunnel_mutex);
printf("%s 正在通过普通隧道\n", train_name);
sleep(3); // 假设通过普通隧道需要2秒
pthread_mutex_unlock(&normal_tunnel_mutex);
}
// 火车通过隧道
printf("%s 已通过隧道\n", train_name);
return NULL;
}
int main() {
pthread_t threads[TRAIN_COUNT];
int train_types[TRAIN_COUNT]; // 直接使用数组,而不是指针数组
// 分配火车类型
for (int i = 0; i < 3; i++) {
train_types[i] = 1; // 复兴号
}
for (int i = 3; i < TRAIN_COUNT; i++) {
train_types[i] = 0; // 绿皮扭扭车
}
// 初始化随机数生成器
srand(time(NULL));
// 创建线程
for (int i = 0; i < TRAIN_COUNT; i++) {
pthread_create(&threads[i], 0, train_thread, &train_types[i]); // 注意传递的是数组元素的地址
}
// 等待所有线程完成
for (int i = 0; i < TRAIN_COUNT; i++) {
pthread_join(threads[i], 0);
}
printf("所有火车都已通过隧道\n");
return 0;
}