我有一个使用git://github.com/raspberrypi/tools.git的工具在Ubuntu上针对RaspberryPi(3)交叉编译的项目。我想将线程同步与 condition_variable
一起使用,但是 wait_for
似乎不释放对提供给它的mutex
的锁定,如文档中所述。这是在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;
}
这是我在Raspberry控制台上与标准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交叉编译或标准库存在问题。我是否缺少某些东西,并且有解决此问题的方法吗?
编辑:
最初在依赖boost和socket.io-client的更大项目中注意到了此问题。我将
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上正常工作,因此它可能不是交叉编译工具的问题。依赖关系之一可能负责发生了什么事吗? 最佳答案
看来我的链接器命令不正确,这很可能导致静态链接了pthread
,由于交叉编译,它不是RaspberryPi上系统所需的正确版本。根据我的问题编辑,我原来的错误(简化)的链接命令就像:
arm-linux-gnueabihf-g++ -Lsome_dirs -static -pthread -o "main" ./main.o ./some_object_files -lsome_libs
在单个main.cpp文件的简单情况下,此类链接选项在创建线程
terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted
时报告了错误。最初它使我失望,但实际上是一个有值(value)的线索。要解决此问题,我必须修改链接器命令以静态链接库,而在动态链接pthread时,我需要静态链接,如下所示:
arm-linux-gnueabihf-g++ -o "main" ./main.o ./some_object_fiels -Lsome_dirs -Wl,-Bstatic -lsome_libs -Wl,-Bdynamic -ldl -pthread