LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-25-2007, 08:50 AM   #1
nalsrayatko
LQ Newbie
 
Registered: Nov 2007
Distribution: Slackware 12
Posts: 8

Rep: Reputation: 0
Problem with passing a class address to a thread


Hi all;

I am newbie to the concept of thread programming, and I have written a simple program trying to pass multiple parameters(includes calling class address) to a thread. But it gives me 'Segmentation fault' error, when I try to change the class attributes. I don't understand what the problem is. However, when I call sleep(1) function after creation of the thread, it runs normally. It seems a synchronization problem between thread and creating program. But I really don't understand what reasons that causes synchronization problem are. Does anybody know about the problem? (Eclipse 3.2.2, Fedora Core 6)


Thanks for everything
Yatko Nalsra


Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>


void*
Loop(void* arg);

class Student
{
public:
    Student();
    void Start();

protected:
    int number;
    friend void* Loop(void*);
};

int main()
{
    Student victor;
   
    victor.Start();
   
    sleep(14);
    printf("The End\n");
}


struct param
{
    int number;
    Student* ptr;
};

Student::Student()
{}

void
Student::Start(void)
{
    pthread_attr_t attr;
    pthread_attr_init( &attr );
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
 
    param temp;
    temp.number = 10;
    temp.ptr = this;
   
    pthread_t handle;
   
    int error;
    error = pthread_create( &handle, &attr,&Loop, &temp );
   
    //sleep(1); when I call this function, it runs perfect.

    if (error)
        return;

}

void*
Loop(void* arg)
{
    param *temp = (param *)arg;
   
    int number = temp->number;
    Student& ptr = *(temp->ptr);
    ptr.number = number;   // When I try to change class attributes, it causes 'segmentation fault' error.
   
    for(int i = 0; i < 10 ;++i){
        printf("number :  %d loop:%d \n", ptr.number,i);
        sleep(2);
   
    }
}
 
Old 11-25-2007, 11:08 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The reason it works with sleep is because that pauses the execution of the calling function while the thread catches up. Without sleep the Start function exits before the thread assigns ptr, making temp no longer exist when the thread tries to use it via the handle (Student& ptr = *(temp->ptr);.)
ta0kira

PS Because the stack unwinds after the return of Start and temp was the second item placed on its stack, it's likely already overwritten by the sleep(14) call before the thread gets to it.

PPS If the Student class creates the thread then it needs to store a pthread_t as a class member. In the destructor, it either needs to pthread_cancel or pthread_wait so that the thread is never running when the object no longer exists. The sleep(14) mitigates this for you, but it's highly unrealistic because that defeats the purpose of a thread.

P^3S Some advice for your future in multi-threading (doesn't apply here, but can save you a headache):
Code:
//instead of:
int *my_int = new int;
delete my_int;
my_int = NULL;
Code:
//do this to prevent a gap (or exception) between 'delete' and '= NULL'
int *my_int = new int;
int *temp = my_int;
my_int = NULL;
delete temp;

Last edited by ta0kira; 11-25-2007 at 11:30 AM.
 
Old 11-25-2007, 11:59 AM   #3
nalsrayatko
LQ Newbie
 
Registered: Nov 2007
Distribution: Slackware 12
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks very much ta0kira.

I see the problem now, clearly and I have changed my code. In the new code, after the creation of thread, the Start() function waits on a boolean (threadRunning) variable until the thread set it as true. Also, thanks for your other advices and I hope to learn more things from this forum.

Yatko
 
  


Reply

Tags
synchronization


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I access class A and class C ip address using one ethernet card? fakhrul Linux - Networking 4 10-21-2007 01:34 AM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
Problem with a thread class in c++ crapodino Programming 1 09-26-2005 08:02 AM
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM


All times are GMT -5. The time now is 08:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration