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:
parent
d6aa5b6e69
commit
fdf34009bf
@ -11,6 +11,12 @@
|
|||||||
#include <netinet/ip.h>
|
#include <netinet/ip.h>
|
||||||
using namespace std;
|
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)
|
int distrib::readHostFile(const char * filename)
|
||||||
{
|
{
|
||||||
ifstream ifs(filename);
|
ifstream ifs(filename);
|
||||||
@ -129,15 +135,29 @@ void distrib_server(distrib * the_distrib)
|
|||||||
<< ':'
|
<< ':'
|
||||||
<< the_distrib->m_serverport
|
<< the_distrib->m_serverport
|
||||||
<< endl;
|
<< 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()
|
int distrib::startServer()
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&m_listen_mutex);
|
||||||
|
|
||||||
|
/* start the listen thread */
|
||||||
int ret = pthread_create(&m_server_thread,
|
int ret = pthread_create(&m_server_thread,
|
||||||
NULL,
|
NULL,
|
||||||
(void * (*)(void *)) distrib_server,
|
(void * (*)(void *)) distrib_server,
|
||||||
this);
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
class distrib
|
class distrib
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
distrib();
|
||||||
int readHostFile(const char * filename);
|
int readHostFile(const char * filename);
|
||||||
int startServer();
|
int startServer();
|
||||||
int startClient(const char * server, int port);
|
int startClient(const char * server, int port);
|
||||||
@ -25,6 +26,8 @@ class distrib
|
|||||||
int m_serverport;
|
int m_serverport;
|
||||||
int m_listen_socket;
|
int m_listen_socket;
|
||||||
pthread_t m_server_thread;
|
pthread_t m_server_thread;
|
||||||
|
pthread_cond_t m_listen_cond;
|
||||||
|
pthread_mutex_t m_listen_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user