![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Shared memory across threads I'm working on a multi-threading project and I've got an issue about shared memory used by multiple threads at the same time. I have two separate threads that have different callback methods. Thread one is to create an ArrayList called "mHistoricalList" and add things to it. Thread two is looping through the same list at the same time. While thread one is adding things to the list, thread two is unable to loop the list. So the program crashed. So I was thinking of using "lock". But it doesn't work - It gives me a runtime unknown error. My code is below (in the callback methond on thread one): void CreateHistoricalList (string ticker, double openPrice, double volume) { lock (mHistoricalList) { SymbolPriceVolume spv = new SymbolPriceVolume(ticker); PriceVolume pv = new PriceVolume(openPrice, volume); spv.PriceVolumeArray.Add(pv); mHistoricalList.Add(spv); } } Is there anything wrong with the way I use the "lock"? Could anyone advise me on how I can get this fixed? Thanks! |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Shared memory across threads Hi, You may use the ArrayList.Synchronized method as described here: http://msdn.microsoft.com/en-us/library/3azh197k.aspx Frédéric Queudret CTO - Mpoware http://www.mpoware.com/ "Curious" <fir5tsight@xxxxxx> wrote in message news:a42f367f-70a2-4ce1-8585-d35c128944fa@xxxxxx Quote: > I'm working on a multi-threading project and I've got an issue about > shared memory used by multiple threads at the same time. > > I have two separate threads that have different callback methods. > Thread one is to create an ArrayList called "mHistoricalList" and add > things to it. Thread two is looping through the same list at the same > time. While thread one is adding things to the list, thread two is > unable to loop the list. So the program crashed. > > So I was thinking of using "lock". But it doesn't work - It gives me a > runtime unknown error. My code is below (in the callback methond on > thread one): > > void CreateHistoricalList (string ticker, double openPrice, double > volume) > { > lock (mHistoricalList) > { > SymbolPriceVolume spv = new SymbolPriceVolume(ticker); > PriceVolume pv = new PriceVolume(openPrice, volume); > spv.PriceVolumeArray.Add(pv); > mHistoricalList.Add(spv); > } > } > > Is there anything wrong with the way I use the "lock"? Could anyone > advise me on how I can get this fixed? Thanks! |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Shared memory across threads Hi Frederic, I tried with the wrapper of ArrayList. It crashed again. I got the same exception: "Unhandled exception at 0x7c812a5b in TSF_Server_1.exe: 0xC0020001: The string binding is invalid." FYI, I changed the code as below: ArrayList myThreadSafeHistoricalList = ArrayList.Synchronized(mHistoricalList); SymbolPriceVolume spv = new SymbolPriceVolume(ticker); PriceVolume pv = new PriceVolume(openPrice, volume); spv.PriceVolumeArray.Add(pv); mHistoricalList.Add(spv); Shall I replace "mHistoricalList" everywhere with "myThreadSafeHistoricalList"? Do I still need to use "lock"? |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Shared memory across threads Hi Frédéric, According to your suggestion, I changed my callback on thread one as below: void CreateHistoricalList (string ticker, double openPrice, double volume) { ArrayList myThreadSafeHistoricalList = ArrayList.Synchronized(mHistoricalList); SymbolPriceVolume spv = new SymbolPriceVolume(ticker); PriceVolume pv = new PriceVolume(openPrice, volume); spv.PriceVolumeArray.Add(pv); myThreadSafeHistoricalList.Add(spv); } However, it crashed again for the same unhandled exception. Did I do anything wrong? Shall I replace "mHistoricalList" with "myThreadSafeHistoricalList" in the callback on thread two as well (and everywhere else)? |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Shared memory across threads Hi, Leave thread one as it is. Thread two should look something like this : lock (mHistoricalList) { foreach (SymbolPriceVolume spv in mHistoricalList) { // Do stuff here } } Are you locking your list for the whole time you iterate over it? If one thread is enumerating a list and another thread alters the list you're going to have one unhappy computer on your hands. On another note I'd encourage you to take a step back and ask why you need 2 threads accessing a list in this way. Enumerating a list can be a long time to lock the whole thing down. - Adam. ============== |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Shared memory across threads Hi, I tried to reproduce your problem and finally it worked fine for me using a lock on the SyncRoot property of the ArrayList instance. The code I used to reproduce is the following one. Let me know if it solvees your problem: public class Class1 { private ArrayList _items; private Thread _t1; private Thread _t2; public Class1() { _items = new ArrayList(); } public void Execute() { _t2 = new Thread(new ThreadStart(ShowItems)); _t1 = new Thread(new ThreadStart(AddItems)); _t1.Start(); _t2.Start(); } public void ShowItems() { while (true) { //ArrayList synchItems = ArrayList.Synchronized(_items); <- doesn't work lock (_items.SyncRoot) { foreach (object item in _items) { Console.WriteLine((string)item); } } } } public void AddItems() { int cpt = 0; while (true) { //ArrayList synchItems = ArrayList.Synchronized(_items); <- does not work lock (_items.SyncRoot) { _items.Add("CPT" + cpt.ToString()); } cpt++; } } } Frédéric "Curious" <fir5tsight@xxxxxx> wrote in message news:7545b895-1a2b-4337-951d-db3efcb817ba@xxxxxx Hi Frédéric, According to your suggestion, I changed my callback on thread one as below: void CreateHistoricalList (string ticker, double openPrice, double volume) { ArrayList myThreadSafeHistoricalList = ArrayList.Synchronized(mHistoricalList); SymbolPriceVolume spv = new SymbolPriceVolume(ticker); PriceVolume pv = new PriceVolume(openPrice, volume); spv.PriceVolumeArray.Add(pv); myThreadSafeHistoricalList.Add(spv); } However, it crashed again for the same unhandled exception. Did I do anything wrong? Shall I replace "mHistoricalList" with "myThreadSafeHistoricalList" in the callback on thread two as well (and everywhere else)? |
My System Specs![]() |
| | #7 (permalink) |
| | RE: Shared memory across threads Jon Skeet has a fairly simple demonstration of a producer-consumer relationship with monitors here: http://www.yoda.arachsys.com/csharp/...eadlocks.shtml It may not be exactly what you're looking for but, I found it of good value when I started exploring synchronization a bit more. "Curious" wrote: Quote: > I'm working on a multi-threading project and I've got an issue about > shared memory used by multiple threads at the same time. > > I have two separate threads that have different callback methods. > Thread one is to create an ArrayList called "mHistoricalList" and add > things to it. Thread two is looping through the same list at the same > time. While thread one is adding things to the list, thread two is > unable to loop the list. So the program crashed. > > So I was thinking of using "lock". But it doesn't work - It gives me a > runtime unknown error. My code is below (in the callback methond on > thread one): > > void CreateHistoricalList (string ticker, double openPrice, double > volume) > { > lock (mHistoricalList) > { > SymbolPriceVolume spv = new SymbolPriceVolume(ticker); > PriceVolume pv = new PriceVolume(openPrice, volume); > spv.PriceVolumeArray.Add(pv); > mHistoricalList.Add(spv); > } > } > > Is there anything wrong with the way I use the "lock"? Could anyone > advise me on how I can get this fixed? Thanks! > |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Shared memory across threads The callback method is huge and long on thread two. If I use "lock" in it, it'll block other thread for a long time. Question: If I use "lock", shall I use it in both the callback methods on both threads? |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Shared memory across threads Hi Frédéric, Thanks for showing me working code. However, your sample code doesn't work for my program, because the ArrayList is altered in both the callback method on thread one, and the callback method on thread two. Thread one is adding new objects to the ArrayList, while thread two is looping through the list and altering some objects already on the list. In your case, thread one is adding new objects but thread two is looping the list without changing the list. In other words, thread one is "write" operation while thread two is "read" operation. In my case, both threads are "write" operations. |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Shared memory across threads Ok, but it does work too on my sample if you alter the ArrayList on the second thread. The sample code is: public void ShowAndAlterItems() { while (true) { lock (_items.SyncRoot) { _items[_items.Count - 1] = _items[_items.Count - 1] + " altered from t2"; foreach (object item in _items) { Console.WriteLine((string)item); } // add a new item _items.Add("Hello From Thread 2"); } } } public void AddItems() { int cpt = 0; while (true) { lock (_items.SyncRoot) { _items.Add("CPT" + cpt.ToString()); } cpt++; } } Do you have any code that show the issue here? Frédéric "Curious" <fir5tsight@xxxxxx> wrote in message news:d017f4a0-7afc-4995-a5c2-88978f917017@xxxxxx Hi Frédéric, Thanks for showing me working code. However, your sample code doesn't work for my program, because the ArrayList is altered in both the callback method on thread one, and the callback method on thread two. Thread one is adding new objects to the ArrayList, while thread two is looping through the list and altering some objects already on the list. In your case, thread one is adding new objects but thread two is looping the list without changing the list. In other words, thread one is "write" operation while thread two is "read" operation. In my case, both threads are "write" operations. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Dedicated Graphical Memory & Shared System Memory | Vista hardware & devices | |||
| Shared Memory | Vista performance & maintenance | |||
| problem with memory (graphics shared memory) | Vista General | |||
| Ati Radeon 9550 using system memory? 511mb shared system memory. | Vista hardware & devices | |||