简体   繁体   English

C ++ 11线程,在主线程上运行

[英]C++11 threads, run on main thread

I am doing some development trying out C++11 threads. 我正在尝试使用C ++ 11线程进行一些开发。 I would like to run some code in an asynchronous thread and when that code is done I would like to run other code on the main thread But only when it's done! 我想在异步线程中运行一些代码,当该代码完成后,我想在主线程上运行其他代码, 但是只有在完成后才可以!

This is because the things I would like to run async is loading OpenGL stuff, and it's a bit tricky with the OpenGL contexts when doing threads, as far as I know it's pretty much a don't run the same context in different threads. 这是因为我想异步运行的东西正在加载OpenGL内容,并且在执行线程时使用OpenGL上下文有点棘手,据我所知,在不同的线程中不要运行相同的上下文。

However I would like to create a loader thread, which loads collada files and the time consuming stuff here really is to parse the file and set up the data and all of that I could (technically) do in a separate thread and then just do the opengl specific tasks on the main thread. 但是,我想创建一个加载程序线程,用于加载collada文件,这里耗时的东西实际上是解析文件并设置数据,而我可以(技术上)在单独的线程中完成所有操作,然后执行opengl在主线程上的特定任务。 (this is my initial thought and I might just be going at it the wrong way).. (这是我最初的想法,我可能会以错误的方式进行操作)。

So I am thinking that if I could detach one thread async to load up the collada file and fill the out the data, then once it's finished I'll invoke on the main thread to bind buffers, set up shaders and so on. 因此,我在想,如果我可以分离一个异步线程来加载collada文件并填充数据,那么一旦完成,我将在主线程上调用以绑定缓冲区,设置着色器等。 I can do it without threads, but would be pretty smooth to load new data in the background without GL freaking out.. 我可以在不使用线程的情况下完成此操作,但是在后台加载新数据而不会导致GL异常运行会非常顺利。

So I'll try to line up the steps I want to do: 因此,我将尝试整理我想做的步骤:

  1. Main thread goes around doing what it does... 主线程绕着它做的事...
  2. Someone asks for a new mesh to be loaded 有人要求加载新的网格
  3. the mesh initialized by creating an async thread and loading inside it the collada data 通过创建异步线程并在其中加载collada数据初始化的网格
  4. meanwhile the main thread goes on doing it's stuff 同时主线程继续做它的东西
  5. once the collada loading is done the async thread informs the main thread that it wishes to do additional loading (ie setup buffers, and so on) ON main thread. 一旦完成了collada加载,异步线程就会通知主线程它希望在主线程上进行其他加载(即设置缓冲区等)。
  6. the setting up finishes and the mesh adds itself to a render queue 设置完成,网格将自身添加到渲染队列中

I do have all of it working synchronous and what I want is a way to do some things once the detached async thread finishes. 我确实将所有这些工作同步进行,一旦分离的异步线程完成,我想要的是一种做一些事情的方式。

Any ideas or of course constructive criticism of my thinking here :P is greeted with a warm welcome! 任何想法或对我的思想的建设性批评:P都受到热烈欢迎! I might be thinking about it all the wrong way, I've been thinking about doing something like an observer pattern but I don't really know how to tackle it the best way. 我可能会以错误的方式思考它,我一直在考虑做类似观察者模式的事情,但是我真的不知道如何以最佳方式解决它。 I wouldn't mind threading the OpenGL stuff, but it seem a bit like asking for trouble.. 我不介意将OpenGL东西穿线,但似乎有点像自找麻烦。

If I understood your use case correctly, then I think the std::async() function, started with the std::launch::async policy to make sure the operation is really started on another thread, does just what you want: 如果我正确理解了您的用例,那么我认为以std::launch::async策略开始的std::async()函数可以确保操作确实在另一个线程上启动,它可以满足您的要求:

// Your function to be executed asynchronously
bool load_meshes();

// You can provide additional arguments if load_meshes accepts arguments
auto f = std::async(std::launch::async, load_meshes); 

// Here, the main thread can just do what it has to do...

// ...and when it's finished, it synchronizes with the operation
// and retrieve its result (if any)

bool res = f.get(); // res will hold the return value of load_meshes,
                    // or this will throw an exception if one was
                    // thrown inside load_meshes()

if (res) 
{ 
    // ... and then it will go on doing the remaining stuff on the main thread
}

One tricky thing to be aware of here, is that you should always assign the return value of std::async() to some variable (of type std::future<T> ), where T is the type returned by load_meshes() . 这里要注意的一件棘手的事情是,您应该始终将std::async()的返回值分配给某个变量(类型为std::future<T> ),其中Tload_meshes()返回的类型。 。 Failing to do so will cause the main thread to wait until load_meshes() is finished (thus, it's as if the function was invoked in the main thread). 否则,主线程将等待直到load_meshes()完成(因此,就好像该函数在主线程中被调用一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM