首页 \ 问答 \ 从不同的线程写入boost :: asio socket(write to boost::asio socket from different threads)

从不同的线程写入boost :: asio socket(write to boost::asio socket from different threads)

在我们的应用程序中,我们使用Boost库(和ASIO进行网络通信)。

最近,我们发现如果我们通过相同的套接字从不同的线程发送数据,我们的客户端应用程序就会接收数据。

小测试突出问题:

#include <stdio.h>
#include <boost/thread.hpp>
#include <boost/asio.hpp>

void send_routine(boost::shared_ptr<boost::asio::ip::tcp::socket> s, char c)
{
  std::vector<char> data(15000, c);
  data.push_back('\n');

  for (int i=0; i<1000; i++)
    boost::asio::write(*s, boost::asio::buffer(&data[0], data.size()));
}


int main()
{
  using namespace boost::asio;
  using namespace boost::asio::ip;

  try {
    io_service io_service;
    io_service::work work(io_service);

    const char* host = "localhost";
    const char* service_name = "18000";

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), host, service_name);
    tcp::resolver::iterator iterator = resolver.resolve(query);

    auto socket = boost::shared_ptr<tcp::socket>(new tcp::socket(io_service));
    socket->connect(*iterator);

    boost::thread t1(send_routine, socket, 'A');
    boost::thread t2(send_routine, socket, 'B');
    boost::thread t3(send_routine, socket, 'C');

    t1.join();
    t2.join();
    t3.join();
  }
  catch (std::exception& e) {
    printf("FAIL: %s\n", e.what());
  }
    return 0;
}

所以,我们在这里创建套接字,连接到localhost:18000并启动3个线程,这些线程将写入套接字。

在不同的终端窗口中,我运行nc -l -p 18000 | tee out.txt | sort | uniq | wc -l nc -l -p 18000 | tee out.txt | sort | uniq | wc -l nc -l -p 18000 | tee out.txt | sort | uniq | wc -l 。 我期望3作为输出,但它在网络流中返回超过100“不同的字符串”(因此,数据已损坏)。 但它适用于小缓冲区大小(例如,如果我们将更改1500080 )。

所以,问题是:它是ASIO库的正确行为吗? 另一个:如何解决它? 我应该在send_routine函数中使用mutex send_routine (还是有其他解决方案)?


In our application we use Boost libraries (and ASIO for network communications).

Recently, we discovered that if we're sending our data from different threads via same socket, our client application is receiving garbaged data.

Small test to highlight the issue:

#include <stdio.h>
#include <boost/thread.hpp>
#include <boost/asio.hpp>

void send_routine(boost::shared_ptr<boost::asio::ip::tcp::socket> s, char c)
{
  std::vector<char> data(15000, c);
  data.push_back('\n');

  for (int i=0; i<1000; i++)
    boost::asio::write(*s, boost::asio::buffer(&data[0], data.size()));
}


int main()
{
  using namespace boost::asio;
  using namespace boost::asio::ip;

  try {
    io_service io_service;
    io_service::work work(io_service);

    const char* host = "localhost";
    const char* service_name = "18000";

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), host, service_name);
    tcp::resolver::iterator iterator = resolver.resolve(query);

    auto socket = boost::shared_ptr<tcp::socket>(new tcp::socket(io_service));
    socket->connect(*iterator);

    boost::thread t1(send_routine, socket, 'A');
    boost::thread t2(send_routine, socket, 'B');
    boost::thread t3(send_routine, socket, 'C');

    t1.join();
    t2.join();
    t3.join();
  }
  catch (std::exception& e) {
    printf("FAIL: %s\n", e.what());
  }
    return 0;
}

So, we create socket here, connect to localhost:18000 and start 3 threads which will write to the socket.

In different terminal window, I run nc -l -p 18000 | tee out.txt | sort | uniq | wc -l. I expect 3 as output, but it returns more then 100 "different strings" in the network stream (so, data is corrupted). But it works with small buffer sizes (if we'll change 15000 to 80, for example).

So, the question is: is it a correct behavior of ASIO library? And another: how to fix it? Should I use mutex inside my send_routine function (or there is another solution)?


原文:https://stackoverflow.com/questions/11581978
更新时间:2023-07-12 15:07

最满意答案

你只需要一个输入到你的函数,因为它只能操作table_df1 。 你也不需要rm(cols)作为cols只存在于函数环境中,所以一旦函数调用终止,就不会存在。

f1 = function(table_df1) {
  table_df1 = as.data.table(table_df1)
  cols = colnames(table_df1)
  cols = cols[-1]
  table_df2 = table_df1[,(paste0(cols, "_pctChange")) := lapply(.SD, function(col){ 
    (col-shift(col,1,type = "lag"))/shift(col,1,type = "lag")
  }), .SDcols=cols]     
}

x2 <- f1(x)
print(x2)
         date a  b    c a_pctChange b_pctChange c_pctChange
1: 2018-01-01 1 89 1872          NA          NA          NA
2: 2018-02-01 3 56 7222   2.0000000  -0.3707865   2.8579060
3: 2018-03-01 2 47 2930  -0.3333333  -0.1607143  -0.5942952

You only need one input to your function as it only manipulates table_df1. Also you don't need the rm(cols) as cols only exists in the function environment and so will not exist once the function call has terminated.

f1 = function(table_df1) {
  table_df1 = as.data.table(table_df1)
  cols = colnames(table_df1)
  cols = cols[-1]
  table_df2 = table_df1[,(paste0(cols, "_pctChange")) := lapply(.SD, function(col){ 
    (col-shift(col,1,type = "lag"))/shift(col,1,type = "lag")
  }), .SDcols=cols]     
}

x2 <- f1(x)
print(x2)
         date a  b    c a_pctChange b_pctChange c_pctChange
1: 2018-01-01 1 89 1872          NA          NA          NA
2: 2018-02-01 3 56 7222   2.0000000  -0.3707865   2.8579060
3: 2018-03-01 2 47 2930  -0.3333333  -0.1607143  -0.5942952

相关问答

更多
  • 对于第一个示例,您可以执行以下操作: lapply(dflist, `$.data.frame`, "a") 对于第二个,使用slot()访问器函数 lapply(mylist, "slot", "tab") 我不确定为什么方法调度在第一种情况下不起作用,但是?lapply的Note部分确实解决了像$这样的原始函数的borked方法调度问题: Note: [...] For historical reasons, the calls created by ‘lapply’ are uneva ...
  • 对于你的第一个问题,lapply增加复杂性,因为它返回你没有使用的列表,for循环更简单但更慢,开销可能会或可能不会很大,这取决于被调用函数的速度。 对于第二部分,我会采用这种方式(示例的虚拟输入,所以我保留了内部循环,如果不在内部循环中调用标量输入函数,如readPNG ,则可以避免): cores <- list("A", "B", "C") pics <- rep(list(vector("character",1)),length(cores)) for(i in 1:length(cores)) ...
  • 如果您正在使用lapply,那么您只是在没有其名称的情况下移交数据框。 也许如果你改变方法: set.seed(123) x_data <- data.frame(A = rnorm(20), B = rnorm(20)) y_data <- data.frame(A = rnorm(20), B = rnorm(20)) add_x_or_y <- function(df, df_name) { df$x_or_y ...
  • 这可以在一行中完成: lst <- lapply(lst, grep, pattern="http", value=TRUE, invert=TRUE) #lst #[[1]] # [1] "I" "my" "mum." "I" "love" "my" "dad." "I" "love" "my" "brothers." # #[[2]] # [1] "I" ...
  • 如果你想使用lapply,你必须创建一个列表,其列表中的元素是包含自变量观察值的向量。 例如,您有一个包含变量Sstatus,indepen1,indepen2,indepent3的数据集。 # make a list list.of.indepent <- vector("list", 3) list.of.indepent[[1]] <- indepen1 etc 然后 lapply(list.of.indepent, FUN=Lmodel) 应该管用。 您可能需要编辑您的Lmodel函数,如下 ...
  • 你只需要一个输入到你的函数,因为它只能操作table_df1 。 你也不需要rm(cols)作为cols只存在于函数环境中,所以一旦函数调用终止,就不会存在。 f1 = function(table_df1) { table_df1 = as.data.table(table_df1) cols = colnames(table_df1) cols = cols[-1] table_df2 = table_df1[,(paste0(cols, "_pctChange")) := lapply ...
  • 我假设您的d数据已经排序: new_data <- do.call(rbind, lapply(unique(d$State), function(state){ data.frame(State = state, Hospital.Name = d$Hospital.Nam ...
  • 在第一个示例中, x是传递给函数的对象。 在第二个例子中,它是一个字符串。 在这两种情况下,它都不是表示传递给dcast的data的变量的符号。 您需要使用list元素的名称构造公式,如下所示: # define the function myfunc <- function(varname) { # 'varname' is a character string naming the column to use myformula <- as.formula(paste("idx~",varnam ...
  • 这归结为位置匹配和参数命名问题。 x是papply正式参数,因此,当你在其中命名时 papply(1:5,function(i,x){return(data.frame(a=i,b=i+x))},x=1) 这意味着1:5匹配为fun - >因此错误。 在函数的当前形式中, R不可能知道当你表示x=1 ,你想要在...组件中考虑它。 请参阅MoreArgs参数以将mapply视为可能有用的方法。 This comes down to a positional matching and argument na ...
  • 以下应该有效: directories <- list.dirs(path=".", full.names = T) # you need to make sure this contains the relevant directories # otherwise you need to remove irrelevant directories foo <- function(x) { old <- setwd(x) # this stores the old directory and chan ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)