C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pthread return error

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
tikviva
Guest





PostPosted: Fri Jul 30, 2004 3:23 pm    Post subject: pthread return error Reply with quote



Hi, I am doing another experiment on pthread, it seems to me the after
the thread_funcion has done computing, the pointer it returns don't
match what I really want.

I have a thread_funciton which computes and holds the square roots
from 0 to 99, (in double), and it returns a double array porinter to
the main thread. In the main thread, I simply extract the data and
print them onto the screen.

here comes the coding ...

extern "C" {
#include <pthread.h>
}
#include <iostream>
using namespace std;

const int ARY_SIZE = 100;

void* thread_function(void *arg)
{
double *ary[ARY_SIZE] = {0};
for (int i=0; i ary[i] = (double *)malloc(sizeof(double));

for (int j=0; j *ary[j] = (double)(j*j);
return (void *)ary;
}

int main(void)
{
pthread_t thread;
void *exit_status;
double *thread_result[ARY_SIZE];

pthread_create(&thread, NULL, thread_function, NULL);
pthread_join(thread, &exit_status);
cerr << "Print results ... " << endl;
memcpy(thread_result, (double *)exit_status,
ARY_SIZE);//sizeof(double)*(ARY_SIZE-1));

for (int i=0; i {
if( thread_result[i] == NULL )
cerr << "thread_result[" << i << "] is null"
<< endl;
else
cerr << "thread_result[" << i << "] is null"
<< endl;
else
cerr << *thread_result[i] << endl;
}
cerr << "Done with printing." << endl;
free(exit_status);
return 0;
}

-----
Compile command:

g++ -Wall -lpthread -o 2mythread 2mythread.cc
-----

I have a segmentation fault once the printing of 14*14 is done. Can
anyone please show me what's wrong I have been doing in my code?

Thank you so much guys. ;^)

-tikviva

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Sat Jul 31, 2004 3:07 am    Post subject: Re: pthread return error Reply with quote



tikviva wrote:
Quote:

void* thread_function(void *arg)

extern "C" void* thread_fn(void* arg)

Quote:
{
double *ary[ARY_SIZE] = {0};

local array of pointers to double, all initialized to null.

Quote:
for (int i=0; i ary[i] = (double *)malloc(sizeof(double));

Fill every element of the array with an allocated double. Since we are in
clc++m, you should have used new instead. Doesn't matter though.

Quote:
for (int j=0; j *ary[j] = (double)(j*j);

Compute values. Note that first casting to double and then multiplying will
allow a wider range of numbers.

Quote:
return (void *)ary;

Now, this is really dirty: do never cast unless you have to, it just helps
hiding errors. Here, it would have been done implicitly anyway. However,
here lies the problem: ary is a local array, which is returned here.

Instead: what you want is to allocate an array of doubles which you return
instead of allocating single doubles that hold the result.

Quote:
int main(void)

The void is redundant here, in C as in C++.

Quote:
memcpy(thread_result, (double *)exit_status, ARY_SIZE);
//sizeof(double)*(ARY_SIZE-1));

Wrong, both of them. Firstly, with the method described above, you don't
need to copy the array. Secondly, if you need to copy an array via memcpy,
you need to copy num_elements*size_of_element bytes. In this case, that
would have been ARY_SIZE*sizeof(double*).

Quote:
free(exit_status);

Again, no: you allocated the elements in a loop and now you need to release
them in a loop.

Quote:
g++ -Wall -lpthread -o 2mythread 2mythread.cc

GCC-specific and a bit off-topic here: use '-pthread' or '-pthreads', not
'-lptread'.

One last thing: I didn't take a look at how you use pthreads at all, I'm
just commenting on the C++ side of things.

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dazza
Guest





PostPosted: Sat Jul 31, 2004 6:49 pm    Post subject: Re: pthread return error Reply with quote



tikviva wrote:
Quote:
Hi, I am doing another experiment on pthread, it seems to me the after
the thread_funcion has done computing, the pointer it returns don't
match what I really want.

I have a thread_funciton which computes and holds the square roots
from 0 to 99, (in double), and it returns a double array porinter to
the main thread. In the main thread, I simply extract the data and
print them onto the screen.

here comes the coding ...

extern "C" {
#include <pthread.h
}
#include using namespace std;

const int ARY_SIZE = 100;

void* thread_function(void *arg)
{
double *ary[ARY_SIZE] = {0};
for (int i=0; i ary[i] = (double *)malloc(sizeof(double));
// This seems inefficient to allocated individual elements.

for (int j=0; j *ary[j] = (double)(j*j);
return (void *)ary;
ERROR here: Returning a pointer to a local variable

that goes out of scope.
Quote:
}

int main(void)
{
pthread_t thread;
void *exit_status;
double *thread_result[ARY_SIZE];

pthread_create(&thread, NULL, thread_function, NULL);
pthread_join(thread, &exit_status);
cerr << "Print results ... " << endl;
ERROR here:exit_status points to an array that has gone out of scope
memcpy(thread_result, (double *)exit_status, ARY_SIZE);//sizeof(double)*(ARY_SIZE-1));

for (int i=0; i {
if( thread_result[i] == NULL )
cerr << "thread_result[" << i << "] is null" << endl;
else

cerr << *thread_result[i] << endl;
}
cerr << "Done with printing." << endl;
free(exit_status);
return 0;
}

-----
Compile command:

g++ -Wall -lpthread -o 2mythread 2mythread.cc
-----

I have a segmentation fault once the printing of 14*14 is done. Can
anyone please show me what's wrong I have been doing in my code?

Thank you so much guys. ;^)

-tikviva

Tikviva,
I assume that this is a learning exercise. The thing you are
calculating does not seem to be worth the effort of a separate thread on
most processors, nowadays.
Second this is my first posting and my syntax might be wrong so be
patient with me.
I think you were doing a few things wrong. That being:
- Using variables that have gone out of scope is a big nono.
- Not freeing the memory allocated correctly.

Then the code could have been a bit tighter. There were several
inefficiencies that I noticed. Those being:
- Multiple loops,
- allocating pointers to individual objects instead of arrays/vectors
of the objects.

All that withstanding try the following: I have not tried it as I do
not have access to a compiler at the moment.

Try this.

extern "C" {
#include }
#include <iostream>
using namespace std;

const int ARY_SIZE = 100;

extern "C" void* thread_fn(void* arg)
{
double *ary = new double[ARY_SIZE];
for (int j=0; j < ARY_SIZE; j++)
{
double d = j;
ary[j] = d*d;
}
return ary;
}

int main(void)
{
pthread_t thread;
void *exit_status;
double *thread_result;

pthread_create(&thread, NULL, thread_fn, NULL);
pthread_join(thread, &exit_status);
if (exit_status != NULL)
{
thread_result = (double *)exit_status;
cerr << "Print results ... " << endl;
thread_result = (double *)exit_status;

for (int i=0; i {
cerr << thread_result[i] << endl;
}
cerr << "Done with printing." << endl;
delete [] thread_result;
}
else
cerr << "Thread failed ... " << endl;

return 0;
}

Darrel McQuienn
Principle Systems Engineer,
I do not want replies, please followup to the group.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.