# May I create a glFenceSync in one thread, and wait for it in another thread(glClientWaitSync) in GE8430?

**URL:** <https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439>\
**Category:** PowerVR Insider\
**Created:** [September 30, 2021, 5:36am UTC](https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439 "2021-09-30T05:36:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhuyingliang](https://avatars.discourse-cdn.com/v4/letter/z/e19adc/32.png) [@zhuyingliang](https://forums.imgtec.com/u/zhuyingliang)\
**Post date:** [September 30, 2021, 5:36am UTC](https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439/1 "2021-09-30T05:36:20Z")

</div>

Recently, in my project, I use glFenceSync to generate a sync object in my renderThread, and use a buffer to storage the sync obj. And in a other thread , I read the sync value and give it to the glClientWaitSync. But it not works, and error is WAIT\_FAILED. I find the error is that the sync obj is not a valid sync obj. However, in rendering thread, I use glIsSync to judege the sync, the return value is true, but the return value is false when I use glIsSync to judge the value I read from the buffer? Do your driver not support the operation of this? can you give me suggestions to resolve this problem ?

Here is my code  
in rendering thread:

m\_mainThreadSyncObj = glFenceSync(GL\_SYNC\_GPU\_COMMANDS\_COMPLETE, 0);  
ImageShotThread::CaptureImageTask shotTask{m\_FBOID, m\_mainThreadSyncObj, m\_Textures[m\_CurrentFBOIndex].GetImageDataBuffer(),m\_Textures[m\_CurrentFBOIndex].GetImageWidth(), m\_Textures[m\_CurrentFBOIndex].GetImageHeight()};  
m\_ImageShotThread.AddImgCaptureTask(shotTask); // add the syncobj into the buffer

in a other thread:  
GLenum ret = GL\_TIMEOUT\_EXPIRED;  
while (GL\_ALREADY\_SIGNALED != ret && GL\_CONDITION\_SATISFIED != ret)  
{  
ret = glClientWaitSync(task.sync, 0, 100000000);  
}

but here, glissync(task.sync) is false，but i read the opengles 3.2 documentation, it support this operation? Do your driver not support?

Respect your answer soon!

---

<div class="post-metadata">

**Author:** ![AlejandroC](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@AlejandroC](https://forums.imgtec.com/u/AlejandroC)\
**Post date:** [September 30, 2021, 5:32pm UTC](https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439/2 "2021-09-30T17:32:43Z")

</div>

Hi zhuyingliang,

Thanks for your message and welcome to the PowerVR Developer Forum, we will analyse the sync issue you’re experiencing and come back to you with more information.

Best regards,  
Alejandro

---

<div class="post-metadata">

**Author:** ![AlejandroC](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@AlejandroC](https://forums.imgtec.com/u/AlejandroC)\
**Post date:** [October 4, 2021, 2:45pm UTC](https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439/3 "2021-10-04T14:45:53Z")

</div>

Hi zhuyingliang,

Apologies for the delay.

That functionality is supported by our drivers. As a first step, I would suggest you verify the following points below:

- Before you call _glFenceSync_ or _glClientWaitSync_, make sure you make current your GL context (i.e., you call _eglMakeCurrent_) in your current execution thread.
- Make sure the GLES context bound is valid, you can query it with _eglGetCurrentContext_.
- Make sure the fence value returned by _glFenceSync_ in your rendering thread is the same as the one used in _glClientWaitSync_, given by task.sync in your example code.
- Query the properties of the fence sync value _m\_mainThreadSyncObj_ after calling _m\_mainThreadSyncObj = glFenceSync(GL\_SYNC\_GPU\_COMMANDS\_COMPLETE, 0);_ and before calling _glClientWaitSync_. You can use _glGetSynciv_ for that.

Let us know if after those tests you are still experiencing issues.

Best regards,  
Alejandro

---

<div class="post-metadata">

**Author:** ![zhuyingliang](https://avatars.discourse-cdn.com/v4/letter/z/e19adc/32.png) [@zhuyingliang](https://forums.imgtec.com/u/zhuyingliang)\
**Post date:** [October 9, 2021, 5:22am UTC](https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439/4 "2021-10-09T05:22:34Z")

</div>

Hi Alejandro Cosin:  
Apologies for the delay.  
I verify the points you suggested, the results and logs here I can provide for you.  
First, I make sure that when I call glFenceSync function, I make the current my GL context in my current renderthread thread. Because I can render birdview in my screen. But i am not sure that in another thread(using glClientWaitSync) should be in the same context as renderthread?  
Secondly, In rendertherad, I print the value of sync in two thread, the value is same, But when using glIsSync to judge, the result is not same.

 ![2021-10-09 11-46-20屏幕截图](https://us1.discourse-cdn.com/flex015/uploads/imagination1/original/1X/e734994565983ea2fb645af98e92eafe670e16ac.png)  
Thirdly, I found that if I use the glFenceSync and glClientWaitSync is a thread, it works well and not throw wait-failed error. But this using method not satisfy my program requirements. (I need synchonization between GPU and CPU, I want to load the data from GPU and transfer the data to other application by CPU)

Finally, here are two main points:  
1、 The glFenceSync and glClientWaitSync should be in one GL context? And in my opinion, this pair of api can cross thread to use and needn’t be in the context. By contrast, if they all under one context, the fence will be no sense, becasue the command queue in the context is the same and it’s all serial.  
2、Can you provide some example to give me some help( that can be tested successful in your GPU using glFenceSync and glWaitClientSync in two thread)?

The api of glFenceSync and glClientWaitSync I use is not working successfully as i expected, I hope you can give me help, because this is very important to me. Thank you!  
Best wishes,  
zhuyingliang

---

<div class="post-metadata">

**Author:** ![AlejandroC](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@AlejandroC](https://forums.imgtec.com/u/AlejandroC)\
**Post date:** [October 13, 2021, 12:12pm UTC](https://forums.imgtec.com/t/may-i-create-a-glfencesync-in-one-thread-and-wait-for-it-in-another-thread-glclientwaitsync-in-ge8430/3439/5 "2021-10-13T12:12:27Z")

</div>

Hi zhuyingliang,

Below I reply to your questions.

**Question:** _But i am not sure that in another thread(using glClientWaitSync) should be in the same context as renderthread?_  
**Answer:** Yes, you need to make current as well in the other thread. We have an example in our SDK ([Native\_SDK/examples/OpenGLES/Multithreading at master · powervr-graphics/Native\_SDK · GitHub](https://github.com/powervr-graphics/Native_SDK/tree/master/examples/OpenGLES/Multithreading)) that does asynchronous resource loading in GLES and we do that in both threads (main and secondary).

**Question:** _The glFenceSync and glClientWaitSync should be in one GL context?_  
**Answer:** Yes, they should be in one GL context, the call to eglMakeCurrent with the same parameters as in your render thread should be enough for that.

**Question:** _Can you provide some example to give me some help( that can be tested successful in your GPU using glFenceSync and glWaitClientSync in two thread)?_  
**Answer:** Unfortunately we do not have that specific example. You can at least review our GLES Multithreading example, it has some related code.

Below I add a schematic sample of how to use glFenceSync between threads, I hope that helps:

```auto
EGLDisplay display;
EGLSurface draw;
EGLSurface read;
EGLContext context;
GLsync synchronisationObject = 0;

// main thread
{
    // Init shared GL context and make it current
    eglMakeCurrent(display, draw, read, context);

    // [...]

    synchronisationObject = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}

// worker thread
{
    eglMakeCurrent(display, draw, read, context);

    // [...]

    GLenum result = glClientWaitSync(synchronisationObject, 0, timeout_in_nanoseconds);

    if((result == GL_CONDITION_SATISFIED) || (result == GL_ALREADY_SIGNALED))
    {
        // Everything went fine
    }
    else
    {
        // Error (GL_TIMEOUT_EXPIRED / GL_WAIT_FAILED)
    }
}

```

Also, I would advice as in my previous message that you query the sync object on both threads with `glIsSync` and `glGetSynciv` to make sure it still exists. You might be passing the `GLsync` value between threads in the CPU but it might happen that the fence object in the GPU is being destroyed / becomes invalid somewhere else.

Please let us know if you still experience issues.

Best regards,  
Alejandro
