简体   繁体   English

我可以使用bufferedwriter在同一个套接字中进行写并具有线程安全性吗?

[英]Can I write in the same socket using bufferedwriter and having threadsafe?

I have a client use a bufferedwriter for write in a socket. 我有一个客户端使用bufferedwriter在套接字中写入。 But client create a thread that can use the same bufferedwriter. 但是客户端创建一个可以使用相同的bufferedwriter的线程。 I don't use lock for it and my program doesn't have problem, but have I a problem if the main thread of client and the thread of the client write in the bufferedwriter in the same time? 我不使用锁,我的程序也没有问题,但是如果客户端的主线程和客户端的线程同时在bufferedwriter中写入,是否会有问题?

BufferedWriter 's documentation does not describe its thread safety; BufferedWriter的文档没有描述其线程安全性。 the package-level documentation says nothing either. 包级文档也什么也没说。

Even if the individual methods were implemented atomically, calling multiple methods on the writer would definitely not be atomic. 即使单个方法是原子实现的,在writer上调用多个方法也绝对不是原子的。

You should err on the side of caution and assume it is not thread-safe, and externally synchronize the instance. 您应该谨慎行事,并假设它不是线程安全的,并在外部同步实例。 The fact that no bug has manifested yet does not imply the bug is absent. 尚未发现任何错误的事实并不表示该错误不存在。

Writting to socket is not an atomic operation. 写入套接字不是原子操作。

Is better to use a syncrhonized method to writting 最好使用同步方法进行书写


Update 更新

https://stackoverflow.com/a/1457347/5065312 https://stackoverflow.com/a/1457347/5065312

All versions of Unix and Windows attempt to keep the write atomic, but apparently very few provide a guarantee. Unix和Windows的所有版本都试图保持原子写操作,但是显然很少提供保证。

And if you are using a Realtime Kernel the writes are not atomic. 而且,如果您使用的是实时内核,则写入不是原子的。


Update 2: 更新2:

IEEE Std 1003.1-2017 If the number bytes written to socket is not specified or if the number of bytes written to socket is more than {PIPE_BUF}, then, it does not guarantee that this writting is atomic. IEEE Std 1003.1-2017如果未指定写入套接字的字节数,或者写入套接字的字节数大于{PIPE_BUF},则不能保证此写入是原子的。

For more information see: man 7 pipe 有关更多信息,请参见: man 7 pipe

PostData: It is bad practice to write concurrently without controlling the flow. PostData:不好的做法是在不控制流程的情况下并发写入。 99% can work because 99% of the time the socket output will be free 99%可以工作,因为99%的时间插座输出是空闲的

You will have to synchronize in the BufferedWriter when writing from any thread. 从任何线程写入时,都必须在BufferedWriter进行同步。 Even then you are going to have massive problems at the receiving end understanding the stream, unless your protocol consists merely of lines. 即使这样,除非协议仅由行组成,否则在接收端您将在理解流方面遇到很多问题。

Here is the answer of your question: How to get string between two characters using regular expression? 这是您的问题的答案:如何使用正则表达式在两个字符之间获取字符串?

Use this: 用这个:

$input = "hi my name [is] Mary [Poppins]";
$arr = explode(' ', $input);
$from = "[";
$to = "]";
function getStringBetween($input, $from, $to) {

    $sub = substr($input, strpos($input, $from) + strlen($from), strlen($input));

    return substr($sub, 0, strpos($sub, $to));
}


foreach ($arr as $data) {

    echo getStringBetween($data, $from, $to) . " ";  // is Poppins
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM