pthread_rwlock_t pthread_mutex_t
时间: 2024-04-11 15:25:02 浏览: 77
pthread_rwlock_t是POSIX线程库中的读写锁类型,用于实现多线程对共享资源的读写操作的同步。读写锁允许多个线程同时读取共享资源,但只允许一个线程进行写操作。
pthread_mutex_t是POSIX线程库中的互斥锁类型,用于实现多线程对共享资源的互斥访问。互斥锁保证在任意时刻只有一个线程可以访问共享资源,其他线程需要等待锁的释放才能继续执行。
相关问题
pthread_mutex_t pthread_cond_t
pthread_mutex_t是线程互斥量,用于保护共享资源的访问。它的初始化可以使用pthread_mutex_init函数,默认属性下可以将第二个参数设置为NULL。销毁互斥量可以使用pthread_mutex_destroy函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [互斥锁pthread_mutex_t与条件变量pthread_cond_t的使用笔记](https://blog.csdn.net/m0_53539646/article/details/115509348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [linux 线程锁pthread_mutex_t ,pthread_rwlock_t ,pthread_cond_t ,pthread_spinlock_t](https://blog.csdn.net/weixin_43224306/article/details/125050244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
分析一下下面这段代码while(1) { revents = 0; #ifndef DISABLE_LIBSSH if (session->ssh_chan != NULL) { /* we are getting data from libssh's channel */ status = ssh_channel_poll_timeout(session->ssh_chan, timeout, 0); if (status > 0) { revents = POLLIN; } } else #endif #ifdef ENABLE_TLS if (session->tls != NULL) { /* we are getting data from TLS session using OpenSSL */ fds.fd = SSL_get_fd(session->tls); fds.events = POLLIN; fds.revents = 0; status = poll(&fds, 1, timeout); revents = (unsigned long int) fds.revents; } else #endif if (session->fd_input != -1) { /* we are getting data from standard file descriptor */ fds.fd = session->fd_input; fds.events = POLLIN; fds.revents = 0; status = poll(&fds, 1, timeout); revents = (unsigned long int) fds.revents; } else { ERROR("Invalid session to receive data."); return (NC_MSG_UNKNOWN); } /* process the result */ if (status == 0) { /* timed out */ DBG_UNLOCK("mut_channel"); pthread_mutex_unlock(session->mut_channel); return (NC_MSG_WOULDBLOCK); } else if (((status == -1) && (errno == EINTR)) #ifndef DISABLE_LIBSSH || (status == SSH_AGAIN) #endif ) { /* poll was interrupted */ continue; } else if (status < 0) { /* poll failed - something wrong happend, close this socket and wait for another request */ DBG_UNLOCK("mut_channel"); pthread_mutex_unlock(session->mut_channel); #ifndef DISABLE_LIBSSH if (status == SSH_EOF) { emsg = "end of file"; } else if (!session->ssh_chan) { emsg = strerror(errno); } else if (session->ssh_sess) { emsg = ssh_get_error(session->ssh_sess); } else { emsg = "description not available"; } #else emsg = strerror(errno); #endif WARN("Input channel error (%s)", emsg); nc_session_close(session, NC_SESSION_TERM_DROPPED); if (nc_info) { pthread_rwlock_wrlock(&(nc_info->lock)); nc_info->stats.sessions_dropped++; pthread_rwlock_unlock(&(nc_info->lock)); } return (NC_MSG_UNKNOWN); } /* status > 0 */ /* check the status of the socket */ /* if nothing to read and POLLHUP (EOF) or POLLERR set */ if ((revents & POLLHUP) || (revents & POLLERR)) { /* close client's socket (it's probably already closed by client */ DBG_UNLOCK("mut_channel"); pthread_mutex_unlock(session->mut_channel); ERROR("Input channel closed"); nc_session_close(session, NC_SESSION_TERM_DROPPED); if (nc_info) { pthread_rwlock_wrlock(&(nc_info->lock)); nc_info->stats.sessions_dropped++; pthread_rwlock_unlock(&(nc_info->lock)); } return (NC_MSG_UNKNOWN); } /* we have something to read */ break; }
这段代码是一个循环,用于从网络通道中读取数据,直到有数据可读或者等待超时。在循环中,代码会根据条件编译选择不同的代码路径,从不同的网络通道中获取数据。当获取到数据后,代码会根据获取到的数据的状态进行处理。如果等待超时,则返回NC_MSG_WOULDBLOCK表示没有数据可读;如果获取到的数据出现错误,则关闭网络通道,并返回NC_MSG_UNKNOWN表示未知错误;如果获取到的数据可用,则退出循环,返回获取到的数据。
循环的目的是保证能够不断地从网络通道中获取数据,直到获取到数据为止。在循环中,代码会不断地进行读取数据的尝试,直到获取到数据或者等待超时为止。如果循环执行完毕后仍未获取到数据,则说明网络通道中没有可读数据。
阅读全文