libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_thread_safe.cpp
Go to the documentation of this file.
1 /*:
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 // Client threads loop on send/recv until told to exit
33 void client_thread (void *client)
34 {
35  char data = 0;
36  for (int count = 0; count < 100000; count++) {
37  int rc = zmq_send (client, &data, 1, 0);
38  assert (rc == 1);
39  }
40  data = 1;
41  int rc = zmq_send (client, &data, 1, 0);
42  assert (rc == 1);
43 }
44 
45 int main (void)
46 {
48  void *ctx = zmq_ctx_new ();
49  assert (ctx);
50 
51  void *server = zmq_socket (ctx, ZMQ_SERVER);
52  int rc = zmq_bind (server, "tcp://127.0.0.1:5560");
53  assert (rc == 0);
54 
55  void *client = zmq_socket (ctx, ZMQ_CLIENT);
56  int thread_safe;
57  size_t size = sizeof (int);
58  zmq_getsockopt (client, ZMQ_THREAD_SAFE, &thread_safe, &size);
59  assert (thread_safe == 1);
60  rc = zmq_connect (client, "tcp://127.0.0.1:5560");
61  assert (rc == 0);
62 
63  void *t1 = zmq_threadstart (client_thread, client);
64  void *t2 = zmq_threadstart (client_thread, client);
65 
66  char data;
67  int threads_completed = 0;
68  while (threads_completed < 2) {
69  zmq_recv (server, &data, 1, 0);
70  if (data == 1)
71  threads_completed++; // Thread ended
72  }
73  zmq_threadclose (t1);
74  zmq_threadclose (t2);
75 
76  rc = zmq_close (server);
77  assert (rc == 0);
78 
79  rc = zmq_close (client);
80  assert (rc == 0);
81 
82  rc = zmq_ctx_term (ctx);
83  assert (rc == 0);
84 
85  return 0 ;
86 }
#define size
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
void client_thread(void *client)
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
int main(void)
ZMQ_EXPORT void zmq_threadclose(void *thread)
Definition: zmq_utils.cpp:85
ZMQ_EXPORT int zmq_getsockopt(void *s, int option, void *optval, size_t *optvallen)
Definition: zmq.cpp:277
#define ZMQ_THREAD_SAFE
Definition: zmq.h:332
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
#define ZMQ_SERVER
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func, void *arg)
Definition: zmq_utils.cpp:78
#define ZMQ_CLIENT