C++ &引用;pthread“cond”u wait“;没有';Don’不要等到主线程睡眠,为什么?
我做了一个简单的实验来测试:C++ &引用;pthread“cond”u wait“;没有';Don’不要等到主线程睡眠,为什么?,c++,linux,multithreading,variables,conditional,C++,Linux,Multithreading,Variables,Conditional,我做了一个简单的实验来测试: 主线程创建一个子线程 子线程等待主线程向条件变量发送信号 主线程休眠3秒并发出“cond”信号。然后我期望子线程将从“cond_wait”中醒来并打印 代码: #include <pthread.h> #include <unistd.h> #include <cassert> #include <iostream> using namespace std; pthread_mutex_t mt; pthread_c
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#include <iostream>
using namespace std;
pthread_mutex_t mt;
pthread_cond_t cond;
pthread_t tid;
void* tf(void*arg){
pthread_mutex_lock(&mt);
pthread_cond_wait(&cond, &mt);
cout<<"After main thread sleeps 3 seconds\n";
return NULL;
}
int main(){
assert(0==pthread_mutex_init(&mt,NULL));
pthread_create(&tid,NULL,tf,NULL);
sleep(3);
pthread_cond_signal(&cond);
pthread_join(tid,NULL);//Is 2nd parameter useful?
pthread_cond_destroy(&cond);
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
pthread_mutex_t mt;
pthread_cond_t cond;
pthread_t tid;
void*tf(void*arg){
pthread_mutex_lock(&mt);
pthread_cond_wait(&cond,&mt);
cout我不知道Linux线程函数,但在Windows中,您必须初始化与Linux中的pthread\u cond\u t cond
对应的变量
有一个名为pthread\u cond\u init
的函数的手册页,它似乎就是这样做的。我不知道Linux线程函数,但在Windows中,您必须初始化与Linux中的pthread\u cond\t cond
对应的变量
有一个函数名为“代码> pthRead SndInIn/Cuth>函数,它看起来确实是这样的。
最重要的是,由于您将C++标记附加到这个问题上,使用C++线程特性,而不是pToX库。您不能保证总是访问它(例如在Windows上)。,而
std::thread
被设计成跨平台的,没有使用pthread()
库的C接口带来的一些麻烦
第二,初始化你的变量,C和C API都很烦人。第三,你需要考虑到虚假的唤醒,在条件变量wait周围放置一个while循环,并附加一个实际的条件,例如
while (not_signalled) {
pthread_cond_wait(&cond, &mt);
}
可能发生的情况是,您的线程被错误唤醒,然后结束,因为您没有一个while循环来防止错误唤醒
工作C++代码< /P>
#include <thread>
#include <iostream>
#include <chrono>
using std::cout;
using std::endl;
std::mutex mtx;
std::condition_variable cv;
bool has_signalled{false};
void th_function() {
// acquire the lock
auto lck = std::unique_lock<std::mutex>{mtx};
// loop to protect against spurious wakeups
while (!has_signalled) {
// sleep
cv.wait(lck);
}
cout << "Thread has been signalled" << endl;
}
int main() {
auto th = std::thread{th_function};
// sleep for 2 seconds
std::this_thread::sleep_for(std::chrono::seconds(2));
// signal and change the variable
{
std::lock_guard<std::mutex> lck{mtx};
has_signalled = true;
}
// signal
cv.notify_one();
th.join();
}
#包括
#包括
#包括
使用std::cout;
使用std::endl;
std::互斥mtx;
std::条件变量cv;
布尔发出了{false}信号;
void th_函数(){
//获得锁
auto-lck=std::unique_lock{mtx};
//循环以防止虚假唤醒
而(!已发出信号){
//睡眠
cv.等待(lck);
}
最重要的是,由于您将C++标记附加到这个问题上,所以使用C++线程特性,而不是pthLoad库。您不能保证总是访问该线程(例如在Windows上)。,而std::thread
被设计成跨平台的,没有使用pthread()
库的C接口带来的一些麻烦
第二,初始化你的变量,C和C API都很烦人。第三,你需要考虑到虚假的唤醒,在条件变量wait周围放置一个while循环,并附加一个实际的条件,例如
while (not_signalled) {
pthread_cond_wait(&cond, &mt);
}
可能发生的情况是,您的线程被错误唤醒,然后结束,因为您没有一个while循环来防止错误唤醒
工作C++代码< /P>
#include <thread>
#include <iostream>
#include <chrono>
using std::cout;
using std::endl;
std::mutex mtx;
std::condition_variable cv;
bool has_signalled{false};
void th_function() {
// acquire the lock
auto lck = std::unique_lock<std::mutex>{mtx};
// loop to protect against spurious wakeups
while (!has_signalled) {
// sleep
cv.wait(lck);
}
cout << "Thread has been signalled" << endl;
}
int main() {
auto th = std::thread{th_function};
// sleep for 2 seconds
std::this_thread::sleep_for(std::chrono::seconds(2));
// signal and change the variable
{
std::lock_guard<std::mutex> lck{mtx};
has_signalled = true;
}
// signal
cv.notify_one();
th.join();
}
#包括
#包括
#包括
使用std::cout;
使用std::endl;
std::互斥mtx;
std::条件变量cv;
布尔发出了{false}信号;
void th_函数(){
//获得锁
auto-lck=std::unique_lock{mtx};
//循环以防止虚假唤醒
而(!已发出信号){
//睡眠
cv.等待(lck);
}
没办法,我没有初始化两个pthread_xxxx_全局变量!只是修复了它们!明白了,我没有初始化两个pthread_xxxx_全局变量!只是修复了它们!