# Yet another multithreading problem

**URL:** <https://discourse.glfw.org/t/yet-another-multithreading-problem/432>\
**Category:** support\
**Created:** [July 22, 2007, 9:37am UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432 "2007-07-22T09:37:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 22, 2007, 9:37am UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/1 "2007-07-22T09:37:54Z")

</div>

**[calvin1602](https://sourceforge.net/u/calvin1602/)** wrote on [Sunday, July 22, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#a97e):

Hello

I’m using glfw multithreading functions in a program and I’m experiencing some trouble.  
First, some precisions, I don’t know if it’ll help, but anyway:  
I do call glfwInit, but not glfwWindow ( I am in console mode since it’s a test & learning program )  
I’m running XP SP2  
I link glfw statically

so, here it is. I tried to follow the user’s guide as much as possible.

In the main thread  
[code]  
// all the init  
for (int a=0;a!=10;a++){  
glfwLockMutex( Mutex );  
there\_are\_data\_to\_send();  
glfwUnlockMutex( Mutex );  
// THERE WHEN I ADD A Sleep(0), yes, ZERO, IT WORKS. if I don’t, the glfwWaitCond is only triggered ONCE 😊  
glfwSignalCond( WaitingForPackets);// Allô le thread ? ya du boulot pour toi …  
}[/code]

in the thread :  
[code]  
glfwLockMutex( Mutex );  
while (1){ // loop forever, wait for packets  
glfwWaitCond( WaitingForPackets, Mutex, GLFW\_INFINITY );  
send\_data();  
}  
glfwUnlockMutex( Mutex );  
[/code]

So, here it is … I try to send 10 UDP packets. If I add a Sleep(0) ( nonsense, huh ? ), it works. If I don’t, the glfwUnlockMutex( Mutex ); function in the thread loops forever ( I tested with some printf() tests … )

Any idea ? thanks.  
Arnaud

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 22, 2007, 10:05am UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/2 "2007-07-22T10:05:10Z")

</div>

**[peterpp](https://sourceforge.net/u/peterpp/)** wrote on [Sunday, July 22, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#5a68):

The problem is, that signal sent in main thread is lost if there is no other thread waiting for it. So if you send 10 signals at once, first signal will be used and other nine will be lost while thread is sending data.

From GLFW user guide:  
"An important property of condition variables, which separates them from other signaling objects such  
as events, is that only currently waiting threads are affected by a condition. A condition is “forgotten”  
as soon as it has been signaled or broadcasted."

You have two choices:  
- second thread will sent all data that are ready to send, or  
- you will use semaphore - signaling object that does not forget signals

PS: My implementation of semaphore that use GLFW library:  
[http://dark-oberon.svn.sourceforge.net/viewvc/dark-oberon/trunk/game/sources/glfwmodule/glfwsemaphore.h?revision=160&view=markup](http://dark-oberon.svn.sourceforge.net/viewvc/dark-oberon/trunk/game/sources/glfwmodule/glfwsemaphore.h?revision=160&view=markup)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 24, 2007, 12:16pm UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/3 "2007-07-24T12:16:48Z")

</div>

**[calvin1602](https://sourceforge.net/u/calvin1602/)** wrote on [Tuesday, July 24, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#6c3e):

Ohhhhh

All right, thanks 🙂 I think your explanation is far better than the manual’s . Could it be added ? I have read the user’s guide several times, but I hadn’t understand this part.

BUT

I see that you count the times where you send a signal to be sure glfw won’t forget any. this way:  
this-\>singnals++;  
but I do :  
this-\>WaitingPackets.push\_back( blah ); ( I dont have the code there 😛 )

so I can’t really see the difference, since you write :  
111 if (!this-\>singnals)  
112 glfwWaitCond(this-\>cond, this-\>mutex, timeout);  
113  
114 if (this-\>singnals)  
115 this-\>singnals–;

and I write:  
while(! WaitingPackets.empty()){ blah }

I’m sorry if I misunderstood something, it’s the first time I deal with that theads stuff.  
Thanks again, anyway 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 24, 2007, 6:14pm UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/4 "2007-07-24T18:14:36Z")

</div>

**[peterpp](https://sourceforge.net/u/peterpp/)** wrote on [Tuesday, July 24, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#5be0):

You’r welcome. 🙂

There is no difference. If function send\_data() from your first message sends ALL packets that are in buffer:

while(! WaitingPackets.empty()){  
send\_packet( this-\>WaitingPackets.front() );  
this-\>WaitingPackets.pop\_front();  
}

all should works fine… I hope. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 25, 2007, 8:16am UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/5 "2007-07-25T08:16:54Z")

</div>

**nobody** wrote on [Wednesday, July 25, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#3f8b):

Well, this really is what I’ve done. And once more, it works only ONCE if I don’t write that stupid Sleep(0);  
I think I’m gonna test under Ubunutu today and see what he tells me.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 25, 2007, 7:10pm UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/6 "2007-07-25T19:10:28Z")

</div>

**[peterpp](https://sourceforge.net/u/peterpp/)** wrote on [Wednesday, July 25, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#1cf3):

I understand that it works only once. But I can’t see any problem, all packets should by sent.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 25, 2007, 8:20pm UTC](https://discourse.glfw.org/t/yet-another-multithreading-problem/432/7 "2007-07-25T20:20:31Z")

</div>

**[calvin1602](https://sourceforge.net/u/calvin1602/)** wrote on [Wednesday, July 25, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/3d8918b5/#a97e/5734):

Okay. Thanks for answering, though 🙂
