to myself 的分类学习日志

做自己想做的事
posts - 232, comments - 6, trackbacks - 0, articles - 0

导航

<2010年5月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(278)

随笔分类

随笔档案

Internet Industry News

  • 《程序员》官网
  • 中国最具影响力、最权威IT专业技术期刊:《程序员》杂志
  • CSDN.NET
  • 全球最大中文IT社区,为IT专业技术人员提供最全面的信息传播和服务平台
  • InfoQ
  • 提供中立的、由技术实践者主导的会议、内容与在线社区
  • 爱范儿
  • 爱范儿(ifanr)全景关注移动互联网、集中报道创业团队,最潮的智能手持及最酷的互联网应用,对业界生态、智能产品及移动应用有着深刻的理解,致力于“独立,前瞻,深入”的原创报道和分析评论,将大量第一手新酷理念和信息传达到读者。
  • 博客 - 伯乐在线
  • 专注于分享职业相关的博客文章、业界资讯和职业相关的优秀工具和资源。
  • 博客园 - 新闻区
  • 面向软件开发者的高品质IT技术社区。
  • 酷壳 – CoolShell.cn
  • 分享技术见闻,知识,趋势的网站。
  • 外刊IT评论网
  • 以翻译外国IT方面的评论、访谈为主,没有确定的对象,偏重于软件方面,通过那些独特的海外IT视野,关注IT世界,关切IT民生,锐评IT世事。
  • 月光博客
  • 关注互联网和搜索引擎的IT科技博客

Internet Technology

Open Source

搜索

  •  

最新评论

阅读排行榜

评论排行榜

线程间的通讯——Socket

Posted on 2014-11-25 11:06 kongkongzi 阅读(298) 评论(0)  编辑 收藏 引用 所属分类: c++ programming
Linux:
调用socketpair,create a pair of connected sockets。
 1 int g_notificationPipeFDs[2];
 2 
 3 int getNotificationSendFD()
 4 {
 5     return g_notificationPipeFDs[0];
 6 }
 7 
 8 int getNotificationRecvFD()
 9 {
10     return g_notificationPipeFDs[1];
11 }
12 
13 void* LogicThreadFunc(void* args)
14 {
15     struct timeval timeout;
16     timeout.tv_sec = 3;
17     timeout.tv_usec = 0;
18 
19     int value;
20 
21     while (1)
22     {
23         int nfds = getNotificationRecvFD() + 1;
24         fd_set readfds;
25         FD_ZERO(&readfds);
26         FD_SET(getNotificationRecvFD(), &readfds);
27 
28         int ret = select(nfds, &readfds, NULL, NULL, &timeout);
29         if (ret < 0)
30         {
31             perror("select");
32             break;
33         }
34         else if (0 == ret)
35         {
36                 // timeout and do nothing.
37         }
38         else
39         {
40             while (1)
41             {
42                 int len = recv(getNotificationRecvFD(), &value, sizeof(value), 0);
43                 if (len > 0)
44                 {
45                     printf("Recv: %d\n", value);
46                 }
47                 else
48                 {
49                     break;
50                 }
51             }
52         }
53     }
54 
55     return NULL;
56 }
57 
58 
59 
60 int main(int c, char **v)
61 {
62     if (socketpair(AF_LOCAL, SOCK_STREAM, 0, g_notificationPipeFDs) != 0)
63     {
64         perror("socketpair");
65         return -1;
66     }
67 
68     pthread_t logicThreadId;
69     int ret = pthread_create(&logicThreadId, NULL, LogicThreadFunc, NULL);
70     if (ret != 0)
71     {
72         perror("fail to create thread 1.\n");
73         return -1;
74     }
75 
76     int value;
77     while (1)
78     {
79         std::cin >> value;
80         printf("Input: %d\n", value);
81         int len = send(getNotificationSendFD(), &value, sizeof(value), 0);
82         if (len != sizeof(value))
83         {
84             printf("just send %d bytes.\n", len);
85         }
86     }
87 
88     return 0;
89 }


参考 libevent的evutil_socketpair的实现。
/** Create two new sockets that are connected to each other.

    On Unix, this simply calls socketpair().  On Windows, it uses the
    loopback network interface on 127.0.0.1, and only
    AF_INET,SOCK_STREAM are supported.

    (This may fail on some Windows hosts where firewall software has cleverly
    decided to keep 127.0.0.1 from talking to itself.)

    Parameters and return values are as for socketpair()
*/
int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);

使用Socket实现进程间的通讯,与使用 “Mutex lock + Condition variable + 共享缓存区” 实现进程间的通讯的区别:
  • Socket方式:为了实现异步非阻塞地触发处理事件,一般需要把Recv端的socket加入到一个Reactor或Proactor。数据需要被拷贝。
  • Condition variable方式:在wait端需要死等,没有超时设置(?)。需要加锁。





只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理