
作业社区
探索学习新天地,共享知识资源!
沫颖 的学生作业:
代码 /** * 1-6 线程同步与条件变量-课后练习 示例 基于条件变量实现生产者与消费者模型(多个生产者对应一个消费者) */ #include #include #include #include #include #include // 全局共享变量 产品数量 static int num = 0; // 线程互斥锁 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 条件变量 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 子线程执行函数 void *thread_handler(void *arg) { // 按照用户输入的产品数,创建产品数量 int n = atoi((char *)arg); int i; for (i = 0; i < n; i++) { pthread_mutex_lock(&mutex); printf("线程 [%ld] 生产一个产品,产品数量为:%d\n", pthread_self(), ++num); pthread_mutex_unlock(&mutex); // 唤醒消费者线程,消费产品 pthread_cond_signal(&cond); } // 子线程退出 pthread_exit(NULL); } int main(int argc, char *argv[]) { // 总的线程数量 int thread_count = argc - 1; pthread_t *tid = malloc(thread_count * sizeof(pthread_t)); // 动态分配内存 if (NULL == tid) { fprintf(stderr, "内存分配失败\n"); exit(EXIT_FAILURE); } memset(tid, 0, thread_count * sizeof(pthread_t)); // 初始化为0 int i; int err; int total_of_produce = 0; // 总的生产产品的数量 int total_of_consume = 0; // 总的消费产品的数量 bool done = false; // 创建生产者线程 for (i = 1; i < argc; i++) { total_of_produce += atoi(argv[i]); // 生产数量的总和 err = pthread_create(&tid[i - 1], NULL, thread_handler, (void *)argv[i]); if (0 != err) { fprintf(stderr, "pthread_create error\n"); free(tid); exit(EXIT_FAILURE); } } // 消费者线程 for (;;) { pthread_mutex_lock(&mutex); while (num == 0) { // 当产品数量为 0时,让线程阻塞,并释放锁,这里一般设置循环,防止没有重新获取到锁 pthread_cond_wait(&cond, &mutex); } while (num > 0) { total_of_consume++; // 消费产品总数 printf("消费一个产品,产品数量为:%d\n", --num); done = total_of_consume >= total_of_produce; // 判断消费者数量与产品数量 sleep(1); } pthread_mutex_unlock(&mutex); // 退出 if (done) break; } // 等待所有生产者线程结束 for (i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } free(tid); // 释放内存 return 0; } 执行效果 felix@felixlinux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class10$ ./a.out 1 2 3 4 线程 [281473766846752] 生产一个产品,产品数量为:1 线程 [281473758392608] 生产一个产品,产品数量为:2 线程 [281473758392608] 生产一个产品,产品数量为:3 线程 [281473749938464] 生产一个产品,产品数量为:4 消费一个产品,产品数量为:3 消费一个产品,产品数量为:2 消费一个产品,产品数量为:1 消费一个产品,产品数量为:0 线程 [281473607266592] 生产一个产品,产品数量为:1 线程 [281473607266592] 生产一个产品,产品数量为:2 线程 [281473607266592] 生产一个产品,产品数量为:3 线程 [281473607266592] 生产一个产品,产品数量为:4 消费一个产品,产品数量为:3 消费一个产品,产品数量为:2 消费一个产品,产品数量为:1 消费一个产品,产品数量为:0 线程 [281473749938464] 生产一个产品,产品数量为:1 线程 [281473749938464] 生产一个产品,产品数量为:2 消费一个产品,产品数量为:1 消费一个产品,产品数量为:0





weixin_慕哥3021856 的学生作业:
#ifndef _JUDGE_HEAD_H #define _JUDGE_HEAD_H #include "player.hpp" #include "piece.hpp" #include "board.hpp" class Judge { public: bool isWin(Player *player) { bool IWin = false; string color = player->getPieceColor(); IWin = isHorizontalWin(color); if (IWin) return true; IWin = isVerticalWin(color); if (IWin) return true; IWin = isUphillWin(color); if (IWin) return true; IWin = isDownhillWin(color); if (IWin) return true; return false; } bool isHorizontalWin(const string &color) { int count = 0, i = 0; Board *board = Board::getBoardObj(); int line = board->getCurrentLine(); int column = board->getCurrentColumn(); // right 行不变,列增加 for (i = 0; i < 5; i++) { bool ok = board->isSamePiece(color, line, column+i); if (ok) count++; else break; } // 同颜色的棋子数大于等于5 if (count >= 5) return true; // left 行不变,列减少 for (i = 0; i < 5; i++) { bool ok = board->isSamePiece(color, line, column-i); if (ok) count++; else break; } // 中间的棋子多算了一次。满足赢棋条件,返回true if (--count >= 5) return true; return false; } bool isVerticalWin(const string &color) { int count = 0, i = 0; Board *board = Board::getBoardObj(); int line = board->getCurrentLine(); int column = board->getCurrentColumn(); // Up for (i = 0; i < 5;i++) { if (board->isSamePiece(color, line - i, column)) count++; else break; } // Down if (count >= 5) return true; for (i = 0; i < 5; i++) { if (board->isSamePiece(color, line + i, column)) count++; else break; } if (--count >= 5) return true; return false; } bool isUphillWin(const string &color) { int count = 0, i = 0; Board *board = Board::getBoardObj(); int line = board->getCurrentLine(); int column = board->getCurrentColumn(); // Uphill up for (i = 0; i < 5; i++) { if (board->isSamePiece(color, line - i, column + i)) count++; else break; } if (count >= 5) return true; // Uphill down for (i = 0; i < 5; i++) { if (board->isSamePiece(color, line + i, column - i)) count++; else break; } if (--count >= 5) return true; return false; } bool isDownhillWin(const string &color) { int count = 0, i = 0; Board *board = Board::getBoardObj(); int line = board->getCurrentLine(); int column = board->getCurrentColumn(); // Downhill up for (i = 0; i < 5; i++) { if (board->isSamePiece(color, line - i, column - i)) count++; else break; } if (count >= 5) return true; // Uphill down for (i = 0; i < 5; i++) { if (board->isSamePiece(color, line + i, column + i)) count++; else break; } if (--count >= 5) return true; return false; } }; #endif





沫颖 的学生作业:
代码 /** * 1-4 线程同步-课后练习 示例 基于互斥锁实现生产者与消费者模型 主线程为消费者 n 个子线程作为生产者 */ #include #include #include #include #include #include // 共享变量 static int num = 0; // 互斥锁 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 生产者(子线程执行函数) void *producer(void *arg) { // 按照用户输入的产品数,创建产品数量 int n = atoi((char *)arg); int i; for (i = 0; i < n; i++) { pthread_mutex_lock(&mutex); printf("线程 [%ld] 生产一个产品,产品数量为:%d\n", pthread_self(), ++num); pthread_mutex_unlock(&mutex); } // 子线程退出 pthread_exit(NULL); } int main(int argc, char *argv[]) { // 总的线程数量 int thread_count = argc - 1; pthread_t *tid = malloc(thread_count * sizeof(pthread_t)); // 动态分配内存 if (NULL == tid) { fprintf(stderr, "内存分配失败\n"); exit(EXIT_FAILURE); } memset(tid, 0, thread_count * sizeof(pthread_t)); // 初始化为0 int i; int err; int total_of_produce = 0; // 总的生产产品的数量 int total_of_consume = 0; // 总的消费产品的数量 bool done = false; // 创建生产者线程 for (i = 1; i < argc; i++) { total_of_produce += atoi(argv[i]); // 生产数量的总和 err = pthread_create(&tid[i - 1], NULL, producer, (void *)argv[i]); if (0 != err) { fprintf(stderr, "pthread_create error\n"); free(tid); exit(EXIT_FAILURE); } } // 消费者线程 for (;;) { pthread_mutex_lock(&mutex); while (num > 0) { total_of_consume++; // 消费产品总数 printf("消费一个产品,产品数量为:%d\n", --num); done = total_of_consume >= total_of_produce; // 判断消费者数量与产品数量 sleep(1); } pthread_mutex_unlock(&mutex); // 退出 if (done) break; } // 等待所有生产者线程结束 for (i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } free(tid); // 释放内存 return 0; } 执行效果 felix@felixlinux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class10$ ./a.out 1 2 3 4 5 6 线程 [281473197666592] 生产一个产品,产品数量为:1 消费一个产品,产品数量为:0 线程 [281473089204512] 生产一个产品,产品数量为:1 线程 [281473089204512] 生产一个产品,产品数量为:2 线程 [281473089204512] 生产一个产品,产品数量为:3 线程 [281473089204512] 生产一个产品,产品数量为:4 线程 [281473089204512] 生产一个产品,产品数量为:5 线程 [281473189212448] 生产一个产品,产品数量为:6 线程 [281473189212448] 生产一个产品,产品数量为:7 消费一个产品,产品数量为:6 消费一个产品,产品数量为:5 消费一个产品,产品数量为:4 消费一个产品,产品数量为:3 消费一个产品,产品数量为:2 消费一个产品,产品数量为:1 消费一个产品,产品数量为:0 线程 [281473180758304] 生产一个产品,产品数量为:1 线程 [281473180758304] 生产一个产品,产品数量为:2 线程 [281473180758304] 生产一个产品,产品数量为:3 线程 [281473080750368] 生产一个产品,产品数量为:4 线程 [281473080750368] 生产一个产品,产品数量为:5 线程 [281473080750368] 生产一个产品,产品数量为:6 线程 [281473080750368] 生产一个产品,产品数量为:7 线程 [281473080750368] 生产一个产品,产品数量为:8 线程 [281473080750368] 生产一个产品,产品数量为:9 消费一个产品,产品数量为:8 消费一个产品,产品数量为:7 消费一个产品,产品数量为:6 消费一个产品,产品数量为:5 消费一个产品,产品数量为:4 消费一个产品,产品数量为:3 消费一个产品,产品数量为:2 消费一个产品,产品数量为:1 消费一个产品,产品数量为:0 线程 [281473097658656] 生产一个产品,产品数量为:1 线程 [281473097658656] 生产一个产品,产品数量为:2 线程 [281473097658656] 生产一个产品,产品数量为:3 线程 [281473097658656] 生产一个产品,产品数量为:4 消费一个产品,产品数量为:3 消费一个产品,产品数量为:2 消费一个产品,产品数量为:1 消费一个产品,产品数量为:0





阿大月 的学生作业:
#include #include #include #include struct person{ char name[32]; int age; }; void* thread_do(void* arg) { struct person* p = (struct person*)malloc(sizeof(struct person)); strcpy(p->name, "Tom"); p->age = 18; return p; } int main() { pthread_t cthread; int ret; struct person *p; if ((ret = pthread_create(&cthread, NULL, thread_do, NULL)) != 0){ fprintf(stderr, "ERROR create child thread failed: %s\n", strerror(ret)); return -1; } if ((ret = pthread_join(cthread, (void**)&p)) != 0){ fprintf(stderr, "ERROR failed to join thread: %s\n", strerror(ret)); return -1; } printf("name=%s, age=%d\n", p->name, p->age); free(p); return 0; }




