Skip to content

Commit f53dbf1

Browse files
committed
CDRIVER-1915 mistakes in thread example
1 parent 57899f0 commit f53dbf1

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

doc/mongoc_client_pool_t.page

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,71 @@
2222
<section id="example">
2323
<title>Example</title>
2424
<screen><code mime="text/x-csrc"><![CDATA[#include <mongoc.h>
25+
#include <pthread.h>
26+
27+
static pthread_mutex_t mutex;
28+
static bool in_shutdown = false;
2529
2630
static void *
2731
worker (void *data)
2832
{
2933
mongoc_client_pool_t *pool = data;
3034
mongoc_client_t *client;
3135
32-
do {
36+
while (true) {
3337
client = mongoc_client_pool_pop (pool);
3438
/*
3539
* Do something with client. If you are writing an HTTP server, you
3640
* probably only want to hold onto the client for the portion of the
3741
* request performing database queries.
3842
*/
3943
mongoc_client_pool_push (pool, client);
40-
} while (!inShutdown);
4144
42-
return NULL;
45+
pthread_mutex_lock (&mutex);
46+
if (in_shutdown) {
47+
pthread_mutex_unlock (&mutex);
48+
return NULL;
49+
}
50+
51+
pthread_mutex_unlock (&mutex);
52+
}
4353
}
4454
4555
int main (int argc, char *argv[])
4656
{
4757
mongoc_client_pool_t *pool;
4858
mongoc_uri_t *uri;
49-
pthread_t thread[10];
59+
pthread_t threads[10];
5060
unsigned i;
5161
void *ret;
5262
63+
pthread_mutex_init (&mutex, NULL);
5364
mongoc_init ();
5465
55-
uri = mongoc_uri_new ("mongodb://mdb1.example.com/?minPoolSize=16");
66+
uri = mongoc_uri_new ("mongodb://localhost/?minPoolSize=16");
5667
pool = mongoc_client_pool_new (uri);
5768
5869
for (i = 0; i < 10; i++) {
59-
pthread_create (&thread, NULL, worker, pool);
70+
pthread_create (&threads[i], NULL, worker, pool);
6071
}
6172
62-
mongoc_uri_destroy (uri);
73+
sleep (10);
74+
pthread_mutex_lock (&mutex);
75+
in_shutdown = true;
76+
pthread_mutex_unlock (&mutex);
6377
6478
for (i = 0; i < 10; i++) {
65-
pthread_join (threads [i], &ret);
79+
pthread_join (threads[i], &ret);
6680
}
6781
82+
mongoc_client_pool_destroy (pool);
83+
mongoc_uri_destroy (uri);
84+
6885
mongoc_cleanup ();
6986
7087
return 0;
71-
}]]></code></screen>
88+
}
89+
]]></code></screen>
7290
</section>
7391

7492
<links type="topic" groups="function" style="2column">

0 commit comments

Comments
 (0)