<< endl;pthread_mutex_unlock(&mutex);});//1.给pthread_cond_wait和pthread_cond_timedwait发信号(它俩是成对出现的),使其后边的代码可以执行.sleep(1);pthread_cond_broadcast(&cond);//可以唤醒全部线程sleep(2);printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));thread.join();return 0;}
v4.0 st和it用法
#include #include #include #include #include using namespace std;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond = PTHREAD_COND_INITIALIZER;int main() {//1.给pthread_cond_wait和pthread_cond_timedwait发信号(它俩是成对出现的),使其后边的代码可以执行.std::thread thread = std::thread([&]() {cout << "线程等待信号到来..." << endl;struct timeval now;struct timespec outtime;gettimeofday(&now, NULL);outtime.tv_sec = now.tv_sec + 10;//如果没有等到信号,则超过10s后自动处理.outtime.tv_nsec = now.tv_usec * 1000;pthread_mutex_lock(&mutex);printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));//2.可以设置超时时间,一旦超过设定时间,自动执行pthread_cond_timedwait函数后边的代码,它不受pthread_cond_signal的影响.pthread_cond_timedwait(&cond, &mutex, &outtime);printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));cout << "Thread timed wait for 10s." << endl;pthread_mutex_unlock(&mutex);});sleep(1);pthread_cond_broadcast(&cond);//可以唤醒全部线程sleep(2);thread.join();return 0;}
v5.0 : 观察和结构体中变量:、、变化
和用法
#include #include #include #include using namespace std;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond = PTHREAD_COND_INITIALIZER;int main() {std::thread thread = std::thread([&]() {cout << "线程等待信号到来..." << endl;printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));pthread_mutex_lock(&mutex);//1.如果不调用pthread_cond_signal(&cond)函数发信号,永远死等.pthread_cond_wait(&cond, &mutex);printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));cout << "Thread timed wait for 10s." << endl;pthread_mutex_unlock(&mutex);});//1.给pthread_cond_wait和pthread_cond_timedwait发信号(它俩是成对出现的),使其后边的代码可以执行.sleep(1);printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*(reinterpret_cast(&(cond.__data))));printf("%s() [%d], cond.__size = %d\n",__FUNCTION__,__LINE__,*((int*)(&(cond.__size))));printf("%s() [%d], cond.__align = %d\n",__FUNCTION__,__LINE__,*((int*)(&(cond.__align))));printf("%s() [%d], mutex.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(mutex.__data))));printf("%s() [%d], mutex.__size = %d\n",__FUNCTION__,__LINE__,*((int*)(&(mutex.__size))));printf("%s() [%d], mutex.__align = %d\n",__FUNCTION__,__LINE__,*((int*)(&(mutex.__align))));//pthread_cond_signal(&cond);//仅可以唤醒一个线程pthread_cond_broadcast(&cond);//可以唤醒全部线程printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));printf("%s() [%d], cond.__size = %d\n",__FUNCTION__,__LINE__,*((int*)(&(cond.__size))));printf("%s() [%d], cond.__align = %d\n",__FUNCTION__,__LINE__,*((int*)(&(cond.__align))));printf("%s() [%d], mutex.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(mutex.__data))));printf("%s() [%d], mutex.__size = %d\n",__FUNCTION__,__LINE__,*((int*)(&(mutex.__size))));printf("%s() [%d], mutex.__align = %d\n",__FUNCTION__,__LINE__,*((int*)(&(mutex.__align))));sleep(2);printf("%s() [%d], cond.__data = http://www.kingceram.com/post/%d/n",__FUNCTION__,__LINE__,*((int*)(&(cond.__data))));thread.join();return 0;}