-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add ConcurrentBag<T> Clear method #13975
Conversation
Side note, @AlexRadch, before you submit PRs, can you please make sure your branch is up-to-date with master, so as to avoid all of the extra commits like "Merge pull request #2 from dotnet/master"? |
lock (GlobalListsLock) | ||
{ | ||
_locals = new ThreadLocal<ThreadLocalList>(); | ||
_headList = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a safe change. It would need to synchronize with all of the lists, which means freezing the bag before making these changes. And you shouldn't need to allocate a new thread local; you should be able to freeze the bag, iterate through all of the local lists clearing each, then unfreeze the bag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't right.
Method FreezeBag used by read only methods such like: CopyTo, ToArray, GetEnumerator, Count and IsEmpty.
Methods that change bag use lock (GlobalListsLock)
see GetThreadList
method that used by next methods: Initialize, Add, TryTakeOrPeek.
So my lock is correct.
ThreadLocal class do not have methods to clear. So we must recreate it. After that all local lists will be garbage by GC because we do have any reference to them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. By doing it the way you're doing it, Clear can end up doing only a partial clear, where it can still be clearing from part of the data structure while new elements are being added elsewhere that won't be cleared; you can also end up in situations where you'll end up clearing elements added after the Clear operation completes. This is why you need to take all the locks, not just the global one (which freezing also takes). Plus, doing it that way means you don't have to throw away the thread local and all the non-trivial data structures it references.
@stephentoub I don't understand how to sync my corefx repository to dotnet/corefx without such sync PRs. I clone corefx some time ago and now to sync my copy I read instruction on Stack Exchange but it creates many sync PRs that you see. |
Do you have a remote for your local that points to the dotnet corefx repo on GitHub? If not, create one with a command like:
Just like "origin" points to your GitHub repo (by default), with that command "upstream" will point to the dotnet one. Then you can do a command like:
That'll switch to your local master branch and then pull down the remote master, making your master up-to-date with the remote one. After that you can also update your remote origin master, e.g.
You can also just rebase your commit on top of the remote upstream master, e.g.
That makes sure you have a local copy of everything from both origin and upstream, then switches to your local branch for this PR, and then rebases it on top of the master from upstream (takes your new commits and replays them on top of what's in upstream/master). |
You don't right. |
Now I will add tests. |
I removed strange Commits. |
I created RTest11_Clear() test for new Clear method, but can not compile it with next error
It seem that test project do not see code changes in Bag class. How I can compile tests with new Bag class? |
You need to expose the API in netcoreapp11 ref and then create test project specific for netcoreapp11 -- see for example 67bc5d4 or any PR which added a new .NET Core-only API recently (list of issues). It is not well documented in our how-to-contribute guide yet (I plan to get to it in December - gotta try it myself first :)). |
I added netcoreapp11 ref and moved |
I am trying add new API to ConcurentBag. I am reading Add APIs. Now Determining versions and targetsDetermine what librarySystem.Collections.Concurrent Determine target framework
Determine library version
(We should bump) Making the changes in repoIf changing the library version TRUE
Update ref
Update src
Update pkg
Update tests
<Project Include="System.Collections.Concurrent.Tests.csproj">
<TargetGroup>netstandard1.7</TargetGroup>
<TestTFMs>netcoreapp1.1</TestTFMs>
</Project>
|
I recreated this PRs #14084 |
Implement ConcurrentBag.Clear API. See https://github.com/dotnet/corefx/issues/2338