C++ 多線程互斥同步

#include
#include <pthread.h>
#include<unistd.h>
#include<mutex>


//特別注意,多線程程序中不能進行大量耗時操作,會照成進程無故退出或停止運行,應將大數據壓入隊列處理


using namespace std;
mutex p12,p13;


void *thread_func2(void *param)
{
while(1)
{
p13.lock();
//pthread_mutex_lock(&mutex);
printf("**simon**%s:%d\n",__FUNCTION__,__LINE__);
//sleep(1);
//pthread_mutex_unlock(mutex2);
p12.unlock();
}
}
void *thread_func1(void *param)
{
while(1)
{
p12.lock( );
//pthread_mutex_lock(mutex2);
printf("**simon**%s:%d\n",__FUNCTION__,__LINE__);
//sleep(2);
//pthread_mutex_unlock(&mutex);
p13.unlock( );
}
}
int main()
{
pthread_t pid1,pid2;
pthread_mutex_t mutex,mutex2;




pthread_mutex_init(&mutex,NULL);
//pthread_mutex_init(&mutex2,NULL);


pthread_create(&pid1,NULL,thread_func1,NULL);
pthread_create(&pid2,NULL,thread_func2,NULL);

pthread_join(pid1,NULL);
pthread_join(pid2,NULL);


return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章