Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-25-2007, 08:50 AM   #1
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

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

Loop(void* arg);

class Student
    void Start();

    int number;
    friend void* Loop(void*);

int main()
    Student victor;
    printf("The End\n");

struct param
    int number;
    Student* ptr;


    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)


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);
Old 11-25-2007, 11:08 AM   #2
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);.)

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):
//instead of:
int *my_int = new int;
delete my_int;
my_int = NULL;
//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
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.




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:50 PM.

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