Accessing data between threads
-
-------------------------------------------------------------------------------- Hello, I am writing an application which uses two threads. I have a list variable in my Document class. THREAD 1 In this thread, I am filling a list from back i.e pDoc->list.insert(list.begin(),data); THREAD 2 In this thread, I am extracting each element from top of the list, and displaying it on the UserInterface i.e data = pDoc->list.pop_front(); Display(data); THREAD1 cotinously reads and adds data at the bottom of the list while THREAD 2 gets data from the top of the list and displays it. Can I directly access the list memeber available in my document class as above in the two threads. When I tried the above, it worked fine when the list had limited number say 10 to 30 records, but as the size of list was growing to say 10,000 records the program was crashing. Can anyone please help me to solve this ? Thanks Madhavi
-
-------------------------------------------------------------------------------- Hello, I am writing an application which uses two threads. I have a list variable in my Document class. THREAD 1 In this thread, I am filling a list from back i.e pDoc->list.insert(list.begin(),data); THREAD 2 In this thread, I am extracting each element from top of the list, and displaying it on the UserInterface i.e data = pDoc->list.pop_front(); Display(data); THREAD1 cotinously reads and adds data at the bottom of the list while THREAD 2 gets data from the top of the list and displays it. Can I directly access the list memeber available in my document class as above in the two threads. When I tried the above, it worked fine when the list had limited number say 10 to 30 records, but as the size of list was growing to say 10,000 records the program was crashing. Can anyone please help me to solve this ? Thanks Madhavi
Your problem is that, you are using a shared resource (list) between two threads. You need synchronization between threads, so that only one thread at at time can access shared list. To achieve this, you can use
CEvent
,CMutex
, orCSemaphore
, depending on your needs and choice. See any good reference on these classes.:cool: ARSALAN MALIK -
-------------------------------------------------------------------------------- Hello, I am writing an application which uses two threads. I have a list variable in my Document class. THREAD 1 In this thread, I am filling a list from back i.e pDoc->list.insert(list.begin(),data); THREAD 2 In this thread, I am extracting each element from top of the list, and displaying it on the UserInterface i.e data = pDoc->list.pop_front(); Display(data); THREAD1 cotinously reads and adds data at the bottom of the list while THREAD 2 gets data from the top of the list and displays it. Can I directly access the list memeber available in my document class as above in the two threads. When I tried the above, it worked fine when the list had limited number say 10 to 30 records, but as the size of list was growing to say 10,000 records the program was crashing. Can anyone please help me to solve this ? Thanks Madhavi
You can also try to use thread-save lists. I think there are some at www.stlport.org. Hardy.