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 … )
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
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
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.