Skip to content

Commit

Permalink
client: マルチスレッド時、スレッド作成中に作成済みのスレッドが動き出さないようにロックする.
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoki INADA committed Apr 19, 2011
1 parent bb2c1d5 commit 354d49f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clients/client.c
Expand Up @@ -32,6 +32,8 @@ int g_noverwrap;
int g_resolve;
long g_restimes[1000001];

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

const char *host;
const char *port;

Expand Down Expand Up @@ -94,6 +96,9 @@ void* do_connect(struct addrinfo *servinfo)
socks = malloc(sizeof(int)*g_noverwrap);
memset(socks, 0, sizeof(int)*g_noverwrap);

pthread_mutex_lock(&g_mutex);
pthread_mutex_unlock(&g_mutex);

for (i = 0; i < g_nloop; ++i) {
// loop through all the results and connect to the first we can
clock_gettime(CLOCK_MONOTONIC, &t1);
Expand Down Expand Up @@ -245,6 +250,7 @@ int main(int argc, char *argv[])
}
}

pthread_mutex_lock(&g_mutex);
{
void* res;
pthread_t *threads = malloc(sizeof(pthread_t)*nthread);
Expand All @@ -256,6 +262,8 @@ int main(int argc, char *argv[])
return 3;
}
}
pthread_mutex_unlock(&g_mutex);

for (i = 0; i < nthread; ++i) {
rv = pthread_join(threads[i], &res);
if (rv == -1) {
Expand Down

0 comments on commit 354d49f

Please sign in to comment.