using a mutex/condvar instead of a usleep to wait for listen thread to start

git-svn-id: svn://anubis/fart/trunk@225 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-04-06 17:17:04 +00:00
parent d6aa5b6e69
commit fdf34009bf
2 changed files with 24 additions and 1 deletions

View File

@ -11,6 +11,12 @@
#include <netinet/ip.h>
using namespace std;
distrib::distrib()
{
pthread_cond_init(&m_listen_cond, NULL);
pthread_mutex_init(&m_listen_mutex, NULL);
}
int distrib::readHostFile(const char * filename)
{
ifstream ifs(filename);
@ -129,15 +135,29 @@ void distrib_server(distrib * the_distrib)
<< ':'
<< the_distrib->m_serverport
<< endl;
/* signal readiness of the listen thread */
pthread_mutex_lock(&the_distrib->m_listen_mutex);
pthread_cond_signal(&the_distrib->m_listen_cond);
pthread_mutex_unlock(&the_distrib->m_listen_mutex);
}
int distrib::startServer()
{
pthread_mutex_lock(&m_listen_mutex);
/* start the listen thread */
int ret = pthread_create(&m_server_thread,
NULL,
(void * (*)(void *)) distrib_server,
this);
usleep(100000);
if (ret)
return ret;
/* wait for the listen thread to be running */
pthread_cond_wait(&m_listen_cond, &m_listen_mutex);
pthread_mutex_unlock(&m_listen_mutex);
return ret;
}

View File

@ -9,6 +9,7 @@
class distrib
{
public:
distrib();
int readHostFile(const char * filename);
int startServer();
int startClient(const char * server, int port);
@ -25,6 +26,8 @@ class distrib
int m_serverport;
int m_listen_socket;
pthread_t m_server_thread;
pthread_cond_t m_listen_cond;
pthread_mutex_t m_listen_mutex;
};
#endif