Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++11 - gcc conditional_var problem

28 views
Skip to first unread message

David FLEURY

unread,
Mar 25, 2013, 4:47:44 PM3/25/13
to
Bonjour,
j'ai fait un petit exercice en C++11.
But : Cr�er 2 threads, qui prennent la main chacun leur tour

Ca fonctionne sous VS 2012, gcc 4.7.2.
Ca reste bloqu� sous gcc 4.8.0. Le dernier "joueur" ne re�oit pas un
signal. En rempla�ant la lambda, �a bloque aussi sous gcc 4.4


Auriez-vous une id�e de mon erreur ?

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>

// ---------------------------------------------------------------------
const int OnePlayerGameMaxDuration = 3;

// ---------------------------------------------------------------------
std::thread::id LastPlayingPlayer;
std::mutex LastPlayingPlayerMutex;
std::condition_variable LastPlayingPlayerCondVar;

// ---------------------------------------------------------------------
bool IsLastPlayer(const std::thread::id& id)
{
return (LastPlayingPlayer == id);
}

// ---------------------------------------------------------------------
void SetLastPlayer(const std::thread::id& id)
{
LastPlayingPlayer = id;
}

// ---------------------------------------------------------------------
void Player(const std::string& playerName)
{
std::thread::id playerId = std::this_thread::get_id();

for (int i = 0; i < OnePlayerGameMaxDuration; ++i)
{
std::unique_lock<std::mutex> lock(LastPlayingPlayerMutex);
while(IsLastPlayer(playerId))
LastPlayingPlayerCondVar.wait(lock); // Wait someone else

std::cout << playerName << std::endl;

SetLastPlayer(playerId);

// Notify another player, the play is done
LastPlayingPlayerCondVar.notify_one();
}
}

// --------------------------------------------------------------------
int main()
{
// Game started
std::cout << "Ready...Set...Go!" << std::endl;

// Start 2 players : Ping! and Pong!
std::thread player1([] { Player("Ping!"); });
std::thread player2([] { Player("Pong!"); });

// Wait for the players to finish
player1.join();
player2.join();

// Game finished
std::cout << "Done!" << std::endl;
}

didier.c...@gmail.com

unread,
Apr 26, 2013, 6:57:06 PM4/26/13
to
Ca devrait marcher avec:

LastPlayingPlayerCondVar.notify_all();

à la place de notify_one.

Message has been deleted
Message has been deleted

David FLEURY

unread,
Apr 27, 2013, 2:16:39 AM4/27/13
to
Le 27/04/2013 00:57, didier.c...@gmail.com a �crit :
> Ca devrait marcher avec:
>
> LastPlayingPlayerCondVar.notify_all();
>
> � la place de notify_one.
>

En fait, c'�tait plus un probl�me de compile/link de gcc.
Il faut mettre -pthread sinon le comportement ne semble pas stable.
En tout cas, l'utilisation de cette option a r�gl� mes probl�mes, m�me
si je ne vois pas pourquoi � mon niveau, je dois le sp�cifier...


David FLEURY

unread,
May 18, 2013, 11:29:04 AM5/18/13
to
> // ---------------------------------------------------------------------
> void Player(const std::string& playerName)
> {
> std::thread::id playerId = std::this_thread::get_id();
>
> for (int i = 0; i < OnePlayerGameMaxDuration; ++i)
> {
> std::unique_lock<std::mutex> lock(LastPlayingPlayerMutex);
> while(IsLastPlayer(playerId))
> LastPlayingPlayerCondVar.wait(lock); // Wait someone else
>

Ici, la boucle while doit �tre remplac�e par :

LastPlayingPlayerCondVar.wait(lock,[=] { return
!IsLastPlayer(playerId); }); // Wait someone else

pour cause de "spurious wakeup"

0 new messages