× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




Hello to all,

I think im having a problem with threads and c++ destructor. I'll explain the problem

Supose i have a Class myclass that is global for all threads. A thread is created modify some attributes of the class then terminates. All the threads that modifies the class are detached.

After some time i try to access the modified attribute of the class, but this no longer have the value i put in the first thread. i.e. the value is gone or is trash.

Let me put a practical example

Class MyClass
{

char **myCharArray;

void ModifyArray()

{
myCharArray = new char*[10];

for(int i = 0; i < 10; i++)
myCharArray[i] = new char[20];
}

char ReadArray()
{
for(int i = 0; i < 10; i++)
cout << "element " << i << " "<<myCharArray[i]<<endl;

}

};

void *ModClass(void *parm)
{
//mutex lock not shown
g_myclass->ModifyArray();
}

void *ReadClass(void *parm)

{

//mutex lock not shown

g_myclass->ReadArray();

}

MyClass *g_myclass

int main()
{

int i = 0

g_myclass = new MyClass();

while(i < 10)
{

//thread api not used, just pseudo-code
//1)
createThread(ModClass);

sleep(10)
//2)
createThread(ReadClass);

i++;

}

}

1) In the first thread the array is modified using new operator, after that the thread is detached.
2) After some period of time the array is accessed for read, but in some cases after various iteration, the element in the array is gone.

Could it be that the modification of the array with new in the thread is deleted after the execution of the thread?. I think it could be that because i read about thread releasing resources after detaching

Any help will be welcome

Thanks

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.