翻译这段代码并在每一行后注释/* 初始化互斥锁 */ pthread_mutex_init(&mutex, NULL); /* 初始化信号量 */ sem_init( &getok , 0 , 1 ); /* 创建读串口子线程 */ res = pthread_create(&thrd_readcom, NULL, thrd_rcom, &no); if (res != 0) { printf("Create COM read thread failed\n"); exit(res); } printf("Create threads success\n Waiting for threads to finish...\n"); while(1){ res = sem_wait( &getok ); res = pthread_create( &thrd_writecom , NULL , thrd_wcom , (void *)test); } //等待回收读串口线程 res = pthread_join(thrd_readcom, &thrd_ret); if (!res) { printf("COM read Thread joined\n"); } else { printf("COM read Thread join failed\n"); } //等待回收写串口线程 res = pthread_join(thrd_writecom, &thrd_ret); if (!res) { printf("COM write Thread joined\n"); } else { printf("COM write Thread join failed\n"); } //释放互斥锁 pthread_mutex_destroy(&mutex);
时间: 2023-08-15 22:07:33 浏览: 133
互斥锁创建-UnixLinux 核心编程教程
/* 初始化互斥锁 */
pthread_mutex_init(&mutex, NULL);
/* 初始化信号量 */
sem_init(&getok, 0, 1);
/* 创建读串口子线程 */
res = pthread_create(&thrd_readcom, NULL, thrd_rcom, &no);
if (res != 0) {
printf("Create COM read thread failed\n");
exit(res);
}
printf("Create threads success\n Waiting for threads to finish...\n");
while (1) {
res = sem_wait(&getok); //等待信号量
res = pthread_create(&thrd_writecom, NULL, thrd_wcom, (void *)test); //创建写串口子线程
}
//等待回收读串口线程
res = pthread_join(thrd_readcom, &thrd_ret);
if (!res) {
printf("COM read Thread joined\n");
} else {
printf("COM read Thread join failed\n");
}
//等待回收写串口线程
res = pthread_join(thrd_writecom, &thrd_ret);
if (!res) {
printf("COM write Thread joined\n");
} else {
printf("COM write Thread join failed\n");
}
//释放互斥锁
pthread_mutex_destroy(&mutex);
阅读全文