SSM校园社团信息管理系统6k87t(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
2026/1/10 2:40:06
#ifndef LOCKER_H #define LOCKER_H #include<exception> #include<pthread.h> #include<semaphore.h> class locker{ public: locker(){ if(pthread_mutex_init(&mutex,NULL)!=0){ throw std::exception(); } } ~locker(){ pthread_mutex_destroy(&mutex); } bool lock(){ return pthread_mutex_lock(&mutex)==0; } bool unlock(){ return pthread_mutex_unlock(&mutex)==0; } pthread_mutex_t *get(){ return &mutex; } private: pthread_mutex_t mutex; }; class cond{ public: cond(){ if(pthread_cond_init(&m_cond,NULL)!=0){ throw std::exception(); } } ~cond(){ pthread_cond_destroy(&m_cond); } bool wait(pthread_mutex_t *mutex){ // int ret=0; //ret=pthread_cond_wait(&m_cond,mutex); //return ret==0; return pthread_cond_wait(&m_cond,mutex)==0; } bool timewait(pthread_mutex_t *mutex,timespec t){ //int ret=0; //ret=pthread_cond_timedwait(&m_cond,mutex,&t); //return ret==0; return pthread_cond_timedwait(&m_cond,mutex,&t)==0; } bool signal(){ return pthread_cond_signal(&m_cond)==0; } bool broadcast(){ return pthread_cond_broadcast(&m_cond)==0; } private: pthread_cond_t m_cond; }; class sem{ public: sem(){ if(sem_init(&m_sem,0,0)!=0){ throw std::exception(); } } sem(int num){ if(sem_init(&m_sem,0,num)!=0){ throw std::exception(); } } ~sem(){ sem_destroy(&m_sem); } bool wait(){ return sem_wait(&m_sem)==0; } bool post(){ return sem_post(&m_sem)==0; } private: sem_t m_sem; }; #endif1.使用构造和析构来封装对锁的初始化和销毁的处理。
2.对于cond
pthread_cond_wait和pthread_cond_signal
参数是要绑定的锁的指针。
3.对于sem
有sem_wait和sem_post
4.对于成功的判别主要是通过==0