使用nanomsg库实现多线程之间的通信(一)

概述:

      这次主要是使用nanomsg库实现多线程之间的通信,在我们复杂的多线程编程中可能各个线程需要共用一些信息,平常加互斥锁等等,有时候也是相当麻烦,这里是使用nanomsg库实现多线程的通信,主要这次是一对一线程双向通信和单向通信的demo。

多线程一对一双向通信demo

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/pair.h>
#include <nanomsg/bus.h>
#include <nanomsg/nn.h>

/*
  此程序为nanomsg多线程一对一双向通信demo。
*/


//inproc 标识用于多线程通信
char *url = "inproc://sky_test";

int server_sock_init(int *sock)
{
  *sock = nn_socket (AF_SP, NN_PAIR);
  if (*sock < 0) {
    printf("create server sock failed\r\n");
    return 1;
  }
  if (nn_bind(*sock, url) < 0) {
    printf("bind server sock failed\r\n");
    return 1;
  }
  printf("server socket init success...\r\n");
  return 0;
}

int client_sock_init(int *sock)
{
  *sock = nn_socket (AF_SP, NN_PAIR);
  if (*sock < 0) {
    printf("create client sock failed\r\n");
    return 1;
  }
  if (nn_connect(*sock, url) < 0) {
    printf("connect server sock failed\r\n");
    return 1;
  }
  printf("client socket init success...\r\n");
  return 0;
}

void *thread_test(void *arg)
{
  int c_sock;
  char *tx_msg = "Hello Main Thread";
  if (0 != client_sock_init(&c_sock)) {
    return;
  }
  while (1) {
    //发送信息到主线程客户端
    while (1) {
      size_t len = strlen (tx_msg) + 1;
      if (nn_send(c_sock, tx_msg, len, 0) < 0) {
        printf("Thread Send Msg Failed\r\n");
        usleep(500000);
        continue;
      }
      break;
    }
    while (1) {
      //接收主线程客户端信息
      char *rx_msg = NULL;
      int result = nn_recv(c_sock, &rx_msg, NN_MSG,NN_DONTWAIT);
      if (result > 0) {
        printf("Thread Recieve: %s\r\n", rx_msg);
        nn_freemsg (rx_msg);
        break;
      }
      usleep(200000);
    }
  }
}

int main()
{
  int s_sock;
  pthread_t ps;
  char *tx_msg = "Hi Thread Test";
  if (0 != server_sock_init(&s_sock)) {
    return 1;
  }
  pthread_create(&ps, NULL, thread_test, NULL);
  sleep(1);
  while (1) {
    while (1) {
      //接收子线程客户端信息
      char *rx_msg = NULL;
      int result = nn_recv(s_sock, &rx_msg, NN_MSG,NN_DONTWAIT);
      if (result > 0) {
        printf("Main Recieve: %s\r\n", rx_msg);
        nn_freemsg (rx_msg);
        break;
      }
      usleep(200000);
    }
    //发送信息到子线程客户端
    while (1) {
      size_t len = strlen (tx_msg) + 1;
      if (nn_send(s_sock, tx_msg, len, 0) < 0) {
        printf("Main Send Msg Failed\r\n");
        usleep(500000);
        continue;
      }
      break;
    }
  }
  return 0;
}
编译:
gcc -o nanomsg_pair nanomsg_pair.c -lnanomsg -lpthread
运行结果:
sky@ubuntu:~/Study/nanomsg/code_test/inproc/pair$ ./nanomsg_pair
server socket init success...
client socket init success...
Main Recieve: Hello Main Thread
Thread Recieve: Hi Thread Test
Main Recieve: Hello Main Thread
Thread Recieve: Hi Thread Test
Main Recieve: Hello Main Thread
Thread Recieve: Hi Thread Test
Main Recieve: Hello Main Thread
Thread Recieve: Hi Thread Test
Main Recieve: Hello Main Thread
Thread Recieve: Hi Thread Test
Main Recieve: Hello Main Thread
Thread Recieve: Hi Thread Test
Main Recieve: Hello Main Thread
.....

多线程一对一单向通信(类似管道)demo

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/pipeline.h>
#include <nanomsg/nn.h>

/*
  此程序为nanomsg多线程一对一单向通信demo。
*/

//inproc 标识用于多线程通信
char *url = "inproc://sky_test";

//发送数据的socket初始化
int send_sock_init(int *sock)
{
  *sock = nn_socket (AF_SP, NN_PUSH);
  if (*sock < 0) {
    printf("create send data sock failed\r\n");
    return 1;
  }
  if (nn_bind(*sock, url) < 0) {
    printf("bind send data sock failed\r\n");
    return 1;
  }
  printf("send data socket init success...\r\n");
  return 0;
}

//接收数据的socket初始化
int recieve_sock_init(int *sock)
{
  *sock = nn_socket (AF_SP, NN_PULL);
  if (*sock < 0) {
    printf("create recieve data sock failed\r\n");
    return 1;
  }
  if (nn_connect(*sock, url) < 0) {
    printf("connect recieve data sock failed\r\n");
    return 1;
  }
  printf("recieve data socket init success...\r\n");
  return 0;
}

//线程测试
void *thread_test(void *arg)
{
  int c_sock;
  if (0 != recieve_sock_init(&c_sock)) {
    return;
  }
  while (1) {
    //轮询接收信息
    char *rx_msg = NULL;
    int result = nn_recv(c_sock, &rx_msg, NN_MSG, NN_DONTWAIT);
    if (result > 0) {
      printf("Thread Recieve: %s\r\n", rx_msg);
      nn_freemsg (rx_msg);
    }
    sleep(1);
  }
}

int main()
{
  int s_sock;
  pthread_t ps;
  char *tx_msg = "Hello thread test";
  if (0 != send_sock_init(&s_sock)) {
    return 1;
  }
  pthread_create(&ps, NULL, thread_test, NULL);
  sleep(1);
  //间隔两秒,发送信息到子线程接收数据端
  while (1) {
    size_t len = strlen (tx_msg) + 1;
    if (nn_send(s_sock, tx_msg, len, 0) < 0) {
      printf("Main Send Msg Failed\r\n");
      usleep(500000);
      continue;
    }
    sleep(2);
  }
  return 0;
}
编译:
gcc -o nanomsg_pipe nanomsg_pipe.c -lnanomsg -lpthread

运行结果:
sky@ubuntu:~/Study/nanomsg/code_test/inproc/pipeline$ ./nanomsg_pipe
send data socket init success...
recieve data socket init success...
Thread Recieve: Hello thread test
Thread Recieve: Hello thread test
Thread Recieve: Hello thread test
Thread Recieve: Hello thread test
Thread Recieve: Hello thread test
Thread Recieve: Hello thread test
Thread Recieve: Hello thread test
...

 总结:

      使用nanomsg实现线程间通信,对于多线程编程来说是相当方便了,nanomsg库又很轻量级,对于嵌入式程序编程真的非常好用。

猜你喜欢

转载自blog.csdn.net/Dancer__Sky/article/details/83538565