-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
container/garray: MarshalJSON func unnecessary with RLock because it use value receiver #4133
Comments
不加锁 不会出现数据竞争吗? |
Will there be no data competition without locking? |
MarshalJSON用的值接收器,数据会被复制一份,不会出现数据竞争,通过我的测试用例可以看出。不知道我的理解是否正确 |
For the value receiver used by MarshalJSON, the data will be copied and there will be no data competition, as can be seen from my test cases. I don't know if my understanding is correct |
另一个问题是,我不确定为什么MarshalJSON要用值接收器,而不使用指针接收器,如果有人知道可以告知一下 |
Another question is, I'm not sure why MarshalJSON uses a value receiver instead of a pointer receiver. If anyone knows, can you tell me? |
切片的底层 是数组的指针,指针被复制,数组还是同一个数组。 把time.Sleep(1 * time.Second) 去掉 检测-race 检测下 |
The underlying layer of the slice is a pointer to the array. The pointer is copied, but the array is still the same array. Remove time.Sleep(1 * time.Second) and check with -race |
加上-race的确提示data race问题,但很奇怪,既然数组是同一个数组,为什么before跟after打印的结果都是123,after的结果不应该是1234吗 |
Adding -race does prompt a data race problem, but it is very strange. Since the array is the same array, why are the results printed by before and after both 123? Shouldn't the result of after be 1234? |
不能确定哪个goroutine 先执行 |
Unable to determine which goroutine executes first |
Is your feature request related to a problem?
Option Yes
Describe the solution you'd like
The MarshalJSON method of the container/garray package uses a value receiver, so the data of the array will be copied. Therefore, there is no need to add an RLock.
Describe alternatives you've considered
before:
I suggest:
lets test:
result: The data of the MarshalJSON method will not be affected by other concurrent goroutines.
![Image](https://private-user-images.githubusercontent.com/17793750/405464324-81b9aa2f-ba93-4b5f-acf0-24471e690bcd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2NDgyNDcsIm5iZiI6MTczOTY0Nzk0NywicGF0aCI6Ii8xNzc5Mzc1MC80MDU0NjQzMjQtODFiOWFhMmYtYmE5My00YjVmLWFjZjAtMjQ0NzFlNjkwYmNkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDE5MzIyN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTU3NTg1YWNhZjMxMzM3NDRlMzc5YTVhODhhZmIzNTI4N2NiMzlmMTE4ZGQzOTBlNDJlZDI2MTFjMGZmODM2MmYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.pnh173I9xse55LpVl_m2_S5FqecejgHBQRrQ-gMgujg)
Additional
My pull request: #4132
The text was updated successfully, but these errors were encountered: