SFML fails in multithreading(SFML 在多线程中失败)
问题描述
我是 sfml 和 c++ 的新手.我有一个使用 sfml 库来绘制图形的项目,但是当我向我的程序添加一个额外的线程时,它无法执行线程内的代码.这是我的代码:(请帮助我!)
im new to sfml and c++.and I have a project that uses the sfml library's to draw the graphics but when I add an additional thread to my program it fails to execute the code inside the thread. this is my code:(please help me!)
#include <SFMLGraphics.hpp>
#include <SFMLwindow.hpp>
#include <SFMLsystem.hpp>
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int h(sf::RenderWindow* win){
//do something
win->close();
this_thread::sleep_for(chrono::milliseconds(10));
return 0;
}
int main(){
sf::RenderWindow window(sf::VideoMode(800,600),"My window");
thread t1(h,&window);
_sleep(10000000);
t1.join();
return 0;
}
推荐答案
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php#drawing-from-threads
SFML 支持多线程绘图,你甚至不必做任何让它工作的东西.唯一要记住的是停用在另一个线程中使用它之前的一个窗口;那是因为一扇窗户(更准确地说是它的 OpenGL 上下文)不能在多个同时线程.
SFML supports multi-threaded drawing, and you don't even have to do anything to make it work. The only thing to remember is to deactivate a window before using it in another thread; that's because a window (more precisely its OpenGL context) cannot be active in multiple threads at the same time.
调用 window.setActive(false);在您的 main() 中,在您将其传递给线程之前.
call window.setActive(false); in your main(), before you pass it off to the thread.
请记住,您必须在 GUI 线程(主线程)中处理事件以获得最大的可移植性.
And remember that you must handle events in the GUI thread (the main thread) for maximum portability.
这篇关于SFML 在多线程中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SFML 在多线程中失败


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07