Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

boost::asio 连接管理7

$
0
0

newLISP提供了简单的方法让我创建多个进程。下面的程序创建10个进程,每个进程发送几个'a', 最后发送一个'q'.

(define (quit-for-error)
  ((println (net-error)) (exit)))
(define (send-test)
  (set 'socket (net-connect "localhost" 8888))
  (if (net-send socket "a") (println "send a successfully") (quit-for-error))
  (if (net-send socket "a") (println "send a successfully") (quit-for-error))
  (if (net-send socket "a") (println "send a successfully") (quit-for-error))
  (if (net-send socket "a") (println "send a successfully") (quit-for-error))
  (if socket (println (net-send socket "q")) (quit-for-error))
  (exit))

(spawn 'r1 (send-test))
(spawn 'r2 (send-test))
(spawn 'r3 (send-test))
(spawn 'r4 (send-test))
(spawn 'r5 (send-test))
(spawn 'r6 (send-test))
(spawn 'r7 (send-test))
(spawn 'r8 (send-test))
(spawn 'r9 (send-test))
(spawn 'r10 (send-test))

(until (sync 1000))
(exit)

spawn 创建了子进程,用来执行函数send-test.

一共创建了10个进程。

until是一个等待,直到所有子进程都退出才返回。


现在运行测试程序:

 newlisp ./parallel_text.lsp
send a successfullysend a successfully
send a successfullysend a successfully

send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
1

send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1

服务程序显示如下:

 ./cppapplication_4 
the number of connections is: 1
the new connection object is starting now.
correct data received
the number of connections is: 2
the new connection object is starting now.
correct data received
the number of connections is: 3
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
the number of connections is: 2
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
correct data received
the number of connections is: 2
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection

仔细检查,创建了10个连接,又关闭了10个连接。程序正常。好,下面考虑将服务器从单线程改成多线程模型。



作者:sheismylife 发表于2013-1-15 20:58:35 原文链接
阅读:54 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>