我有一個在Ubuntu上使用git://github.com/raspberrypi/tools.git工具在RaspberryPi(3)上交叉編譯的項目。我想與condition_variable
一起使用線程同步,但wait_for
似乎不會釋放提供給它的mutex
上的鎖定,如文檔中所述。下面是轉載了樹莓派的問題簡化代碼(有過多的日誌記錄,以更好地可視化的問題):std :: condition_variable ::等待不釋放RaspberryPi上的鎖
int main(int argc, const char* args[])
{
std::condition_variable cv;
std::mutex m;
bool ok = false;
std::cout << "locking in main" << std::endl;
std::unique_lock<std::mutex> lock(m);
std::cout << "locked in main" << std::endl;
std::cout << "starting thread" << std::endl;
std::thread t([&cv, &m, &ok]() {
std::cout << "locking in thread" << std::endl;
std::unique_lock<std::mutex> lock(m);
std::cout << "locked in thread" << std::endl;
ok = true;
std::cout << "unlocking in thread" << std::endl;
lock.unlock();
std::cout << "signalling cv" << std::endl;
cv.notify_one();
});
std::cout << "starting wait" << std::endl;
bool success = cv.wait_for(lock, std::chrono::seconds(10), [&ok]{ return ok; });
std::cout << "finished waiting: " << success << std::endl;
std::cout << "unlocking in main" << std::endl;
lock.unlock();
std::cout << "joining thread" << std::endl;
t.join();
std::cout << "thread joined" << std::endl;
return 0;
}
這是我比較獲得標準G ++ linux版側樹莓控制檯上並排輸出:
RPi version: linux version:
locking in main locking in main
locked in main locked in main
starting thread starting thread
starting wait starting wait
locking in thread locking in thread
finished waiting: 0 locked in thread
unlocking in main unlocking in thread
joining thread signalling cv
locked in thread finished waiting: 1
unlocking in thread unlocking in main
signalling cv joining thread
thread joined thread joined
這讓我相信代碼很好,並且RaspberryPi交叉編譯或標準庫存在問題。我是否錯過了一些東西,是否有解決這個問題的方法?
編輯: 問題最早是在依賴於升壓和socket.io客戶端一個更大的項目的注意。我將main
簡化爲問題中的一個,但沒有刪除其餘的代碼。它被編譯和鏈接如下:
[...]
arm-linux-gnueabihf-g++ -std=c++1y -I%SRC_DIR%/rapidjson/include -I%SRC_DIR%/boost_1_65_0/install/include -I%SRC_DIR%/socket.io-client-cpp/build/include -O3 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
[...]
arm-linux-gnueabihf-g++ -L%SRC_DIR%/boost_1_65_0/install-arm/lib -L%SRC_DIR%/socket.io-client-cpp/build-arm/lib/Release -L%SRC_DIR%/openssl/build-arm/lib -static -pthread -o "main" [...] ./main.o [...] -lsioclient -lboost_system -lssl -lcrypto -ldl
其中其他目標文件被替換爲[...],socket.io-client需要libssl。但是,如果我只編譯arm-linux-gnueabihf-g++ -pthread -std=c++1y -O3 -g -Wall -o main1 main1.cpp
的張貼片段,它在RaspberryPi上也可以正常工作,所以它可能不是交叉編譯工具的問題。其中一個依賴關係是否有可能對發生的事情負責?
你如何編譯它? – VTT
該日誌僅顯示在主線程中超時之前未在線程中獲取鎖的**。你有沒有嘗試用簡單的'wait'替換'wait_for'?在超時之前沒有獲得鎖*,並且根本沒有獲得鎖*是不同的事情。 – Tsyvarev