|
From: Victor Gonzalez <victorpy@xxxxxxxxxxx>detaching
To: <c400-l@xxxxxxxxxxxx>
Date: 10/05/2010 09:09 AM
Subject: Re: [C400-L] pthreads and c++ destructor
Sent by: c400-l-bounces+albertaa=us.ibm.com@xxxxxxxxxxxx
here i post a working code, if anyone could compile it and try. In
my previous mail i forgot to put the delete line in the Read function
#define _MULTI_THREADED
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
using namespace std;
class MyClass
{
char **myCharArray;
public:
void ModifyArray()
{
myCharArray = new char*[10];
for(int i = 0; i < 10; i++)
{
myCharArray[i] = new char[20];
sprintf(myCharArray[i], "%s%i", "mod ", i);
}
}
void ReadArray()
{
for(int i = 0; i < 10; i++)
{
cout << "element " << i << " "<<myCharArray[i]<<endl;
delete [] myCharArray[i];
}
}
};
MyClass *g_myclass;
void *ModClass(void *parm)
{
//mutex lock not shown
g_myclass->ModifyArray();
return NULL;
}
void *ReadClass(void *parm)
{
//mutex lock not shown
g_myclass->ReadArray();
return NULL;
}
int main()
{
int i = 0, rc;
pthread_t thread1, thread2;
g_myclass = new MyClass();
while(i < 1000)
{
//thread api not used, just pseudo-code
//1)
//createThread(ModClass);
rc = pthread_create(&thread1, NULL, ModClass, NULL);
//sleep(1)
usleep(500000);
//2)
rc = pthread_create(&thread2, NULL, ReadClass, NULL);
i++;
}
}
From: victorpy@xxxxxxxxxxxexplain the problem
To: c400-l@xxxxxxxxxxxx
Date: Tue, 5 Oct 2010 13:28:07 +0000
Subject: [C400-L] pthreads and c++ destructor
Hello to all,
I think im having a problem with threads and c++ destructor. I'll
thread is created modify some attributes of the class then
Supose i have a Class myclass that is global for all threads. A
terminates. All the threads that modifies the class are detached.
class, but this no longer have the value i put in the first thread.
After some time i try to access the modified attribute of the
i.e. the value is gone or is trash.
after that the thread is detached.
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,
2) After some period of time the array is accessed for read, butin some cases after various iteration, the element in the array is gone.
thread is deleted after the execution of the thread?. I think it
Could it be that the modification of the array with new in the
could be that because i read about thread releasing resources after
Any help will be welcome
Thanks
--
This is the C programming iSeries / AS400 (C400-L) mailing list
To post a message email: C400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/c400-l.
--
This is the C programming iSeries / AS400 (C400-L) mailing list
To post a message email: C400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/c400-l.
As an Amazon Associate we earn from qualifying purchases.
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.