蘑菇街面试

用python 启动3个线程打印递增的数字,线程1先打印1,2,3,4,5,线程2打印6,7,8,9,10,线程3打印11,12,13,14,15。接着再由线程1打印16,17,18,19,20,以此类推,知道打印到75#蘑菇街##笔试题目#
全部评论
pthread_mutex_t mutex; pthread_cond_t print; int printId = 0; void *func(void *arg) {     int *id = (int*)arg;     for (int i = 0; i < 5; ++i)     {         pthread_mutex_lock(&mutex);         while (*id != printId)             pthread_cond_wait(&print, &mutex);         cout << *id + 1 << " : ";         for (int j = 1; j <= 5; ++j)             cout << i*15 + j + (*id)*5 << " ";         cout << endl;         printId = (printId+1) % 3;         pthread_cond_broadcast(&print);         pthread_mutex_unlock(&mutex);     }     return NULL; } int main() {     pthread_t pid[3];     for (int i = 0; i < 3; ++i)  pid[i] = i;     pthread_mutex_init(&mutex, NULL);     pthread_cond_init(&print, NULL);     int id[3];     for (int i = 0; i < 3; ++i) id[i] = i;     for (int i = 0; i < 3; ++i)         pthread_create(&pid[i], NULL, func, (void*)(&id[i]));     for (int i = 0; i < 3; ++i)         pthread_join(pid[i], NULL);     pthread_cond_destroy(&print);     pthread_mutex_destroy(&mutex);     return 0; } 来个 C 语言的
点赞 回复
分享
发布于 2019-08-30 16:03
竟然会规定语言??
点赞 回复
分享
发布于 2019-08-30 23:58
联想
校招火热招聘中
官网直投
import threading class prthread(threading.Thread):     def __init__(self,thid,thname,s):         threading.Thread.__init__(self)         self.thid = thid         self.thname = thname         self.s = s     def run(self):         # print('start'+self.thname)         pr(self.thname,self.s) def pr(tname,s):     for i in range(s,s+5):         print('%s:%d'%(tname,i)) for i in range(1,75,15):     th1 = prthread(1,'th1',i)     if i < 70 :         th2 = prthread(2,'th2',i+5)     if i < 65 :         th3 = prthread(3,'th3',i+10)     th1.start()     th2.start()     th3.start() 大概就是这么实现的吧..0.0
点赞 回复
分享
发布于 2019-09-01 11:56

相关推荐

2 19 评论
分享
牛客网
牛客企业服务