从不同的线程写入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“不同的字符串”(因此,数据已损坏)。 但它适用于小缓冲区大小(例如,如果我们将更改15000
到80
)。所以,问题是:它是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 expect3
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 change15000
to80
, 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 mysend_routine
function (or there is another solution)?
原文:https://stackoverflow.com/questions/11581978
最满意答案
你只需要一个输入到你的函数,因为它只能操作
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 therm(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 with “$” function)[2023-06-23]
对于第一个示例,您可以执行以下操作: 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 ...
-
lapply而不是r中的循环(Lapply instead of for loop in r)[2023-12-24]
我假设您的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 ...