Skip to content

Commit 6201933

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

File tree

6 files changed

+104
-115
lines changed

6 files changed

+104
-115
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ echo-server
2828
example-command-monitoring
2929
example-command-with-opts
3030
example-client
31+
example-pool
3132
example-scram
3233
example-gridfs
3334
example-matcher

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ mongoc_add_example(
533533
example-sdam-monitoring TRUE
534534
${SOURCE_DIR}/examples/example-sdam-monitoring.c)
535535
mongoc_add_example(example-client TRUE ${SOURCE_DIR}/examples/example-client.c)
536+
mongoc_add_example(example-pool TRUE ${SOURCE_DIR}/examples/example-pool.c)
536537
mongoc_add_example(example-command-with-opts TRUE ${SOURCE_DIR}/examples/example-command-with-opts.c)
537538
mongoc_add_example(example-scram TRUE ${SOURCE_DIR}/examples/example-scram.c)
538539
mongoc_add_example(mongoc-dump TRUE ${SOURCE_DIR}/examples/mongoc-dump.c)

doc/mongoc_client_pool_t.page

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,7 @@
2121

2222
<section id="example">
2323
<title>Example</title>
24-
<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;
29-
30-
static void *
31-
worker (void *data)
32-
{
33-
mongoc_client_pool_t *pool = data;
34-
mongoc_client_t *client;
35-
36-
while (true) {
37-
client = mongoc_client_pool_pop (pool);
38-
/*
39-
* Do something with client. If you are writing an HTTP server, you
40-
* probably only want to hold onto the client for the portion of the
41-
* request performing database queries.
42-
*/
43-
mongoc_client_pool_push (pool, client);
44-
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-
}
53-
}
54-
55-
int main (int argc, char *argv[])
56-
{
57-
mongoc_client_pool_t *pool;
58-
mongoc_uri_t *uri;
59-
pthread_t threads[10];
60-
unsigned i;
61-
void *ret;
62-
63-
pthread_mutex_init (&mutex, NULL);
64-
mongoc_init ();
65-
66-
uri = mongoc_uri_new ("mongodb://localhost/?minPoolSize=16");
67-
pool = mongoc_client_pool_new (uri);
68-
69-
for (i = 0; i < 10; i++) {
70-
pthread_create (&threads[i], NULL, worker, pool);
71-
}
72-
73-
sleep (10);
74-
pthread_mutex_lock (&mutex);
75-
in_shutdown = true;
76-
pthread_mutex_unlock (&mutex);
77-
78-
for (i = 0; i < 10; i++) {
79-
pthread_join (threads[i], &ret);
80-
}
81-
82-
mongoc_client_pool_destroy (pool);
83-
mongoc_uri_destroy (uri);
84-
85-
mongoc_cleanup ();
86-
87-
return 0;
88-
}
89-
]]></code></screen>
24+
<screen><code mime="text/x-csrc"><include parse="text" href="../examples/example-pool.c" xmlns="http://www.w3.org/2001/XInclude" /></code></screen>
9025
</section>
9126

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

doc/mongoc_tutorial.page

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -858,55 +858,7 @@ C:\> executing
858858
<title>6. Threading</title>
859859
<p>The MongoDB C Driver is thread-unaware in the vast majority of its operations. This means it is up to the programmer to guarantee thread-safety.</p>
860860
<p>However, <code xref="mongoc_client_pool_t">mongoc_client_pool_t</code> is thread-safe and is used to fetch a <code>mongoc_client_t</code> in a thread-safe manner. After retrieving a client from the pool, the client structure should be considered owned by the calling thread. When the thread is finished, the client should be placed back into the pool.</p>
861-
862-
<screen><code><![CDATA[#include <mongoc.h>
863-
#include <pthread.h>
864-
865-
#define N_THREADS 10
866-
867-
static void *
868-
worker (void *data) {
869-
mongoc_client_pool_t *pool = data;
870-
mongoc_client_t *client;
871-
872-
client = mongoc_client_pool_pop (pool);
873-
874-
/* Do something... */
875-
876-
mongoc_client_pool_push (pool, client);
877-
878-
return NULL;
879-
}
880-
881-
int
882-
main (int argc,
883-
char *argv[])
884-
{
885-
mongoc_client_pool_t *pool;
886-
mongoc_uri_t *uri;
887-
pthread_t threads[N_THREADS];
888-
int i;
889-
890-
mongoc_init ();
891-
892-
uri = mongoc_uri_new ("mongodb://localhost/?appname=pool-example");
893-
pool = mongoc_client_pool_new (uri);
894-
895-
for (i = 0; i < N_THREADS; i++) {
896-
pthread_create (&threads[i], NULL, worker, pool);
897-
}
898-
899-
for (i = 0; i < N_THREADS; i++) {
900-
pthread_join (threads[i], NULL);
901-
}
902-
903-
mongoc_client_pool_destroy (pool);
904-
mongoc_uri_destroy (uri);
905-
mongoc_cleanup ();
906-
907-
return 0;
908-
}
909-
]]></code></screen>
861+
<screen><code mime="text/x-csrc"><include parse="text" href="../examples/example-pool.c" xmlns="http://www.w3.org/2001/XInclude" /></code></screen>
910862
</section>
911863

912864
<section id="next-steps">

examples/Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ example_client_SOURCES = examples/example-client.c
3737
example_client_CFLAGS = $(EXAMPLE_CFLAGS)
3838
example_client_LDADD = $(EXAMPLE_LDADD)
3939

40+
noinst_PROGRAMS += example-pool
41+
example_pool_SOURCES = examples/example-pool.c
42+
example_pool_CFLAGS = $(EXAMPLE_CFLAGS)
43+
example_pool_LDADD = $(EXAMPLE_LDADD)
44+
4045
noinst_PROGRAMS += example-command-with-opts
4146
example_command_with_opts_SOURCES = examples/example-command-with-opts.c
4247
example_command_with_opts_CFLAGS = $(EXAMPLE_CFLAGS)

examples/example-pool.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* gcc example-pool.c -o example-pool $(pkg-config --cflags --libs libmongoc-1.0) */
2+
3+
/* ./example-pool [CONNECTION_STRING] */
4+
5+
#include <mongoc.h>
6+
#include <pthread.h>
7+
#include <stdio.h>
8+
9+
static pthread_mutex_t mutex;
10+
static bool in_shutdown = false;
11+
12+
static void *
13+
worker (void *data)
14+
{
15+
mongoc_client_pool_t *pool = data;
16+
mongoc_client_t *client;
17+
bson_t ping = BSON_INITIALIZER;
18+
bson_error_t error;
19+
bool r;
20+
21+
BSON_APPEND_INT32 (&ping, "ping", 1);
22+
23+
while (true) {
24+
client = mongoc_client_pool_pop (pool);
25+
/* Do something with client. If you are writing an HTTP server, you
26+
* probably only want to hold onto the client for the portion of the
27+
* request performing database queries.
28+
*/
29+
r = mongoc_client_command_simple (client, "admin", &ping, NULL, NULL,
30+
&error);
31+
32+
if (!r) {
33+
fprintf (stderr, "%s\n", error.message);
34+
}
35+
36+
mongoc_client_pool_push (pool, client);
37+
38+
pthread_mutex_lock (&mutex);
39+
if (in_shutdown || !r) {
40+
pthread_mutex_unlock (&mutex);
41+
break;
42+
}
43+
44+
pthread_mutex_unlock (&mutex);
45+
}
46+
47+
bson_destroy (&ping);
48+
return NULL;
49+
}
50+
51+
int main (int argc, char *argv[])
52+
{
53+
const char *uristr = "mongodb://127.0.0.1/?appname=pool-example";
54+
mongoc_uri_t *uri;
55+
mongoc_client_pool_t *pool;
56+
pthread_t threads[10];
57+
unsigned i;
58+
void *ret;
59+
60+
pthread_mutex_init (&mutex, NULL);
61+
mongoc_init ();
62+
63+
if (argc > 1) {
64+
uristr = argv [1];
65+
}
66+
67+
uri = mongoc_uri_new (uristr);
68+
if (!uri) {
69+
fprintf (stderr, "Failed to parse URI: \"%s\".\n", uristr);
70+
return EXIT_FAILURE;
71+
}
72+
73+
pool = mongoc_client_pool_new (uri);
74+
mongoc_client_pool_set_error_api (pool, 2);
75+
76+
for (i = 0; i < 10; i++) {
77+
pthread_create (&threads[i], NULL, worker, pool);
78+
}
79+
80+
sleep (10);
81+
pthread_mutex_lock (&mutex);
82+
in_shutdown = true;
83+
pthread_mutex_unlock (&mutex);
84+
85+
for (i = 0; i < 10; i++) {
86+
pthread_join (threads[i], &ret);
87+
}
88+
89+
mongoc_client_pool_destroy (pool);
90+
mongoc_uri_destroy (uri);
91+
92+
mongoc_cleanup ();
93+
94+
return 0;
95+
}

0 commit comments

Comments
 (0)