Linux threading question

bevardis

Junior Member
Apr 9, 2002
2
0
0
I have some problems with making threads in Linux
This is the situation:

#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>

class myclass
{
private:
char stack[ 1024*10 ];
public:
void myclone( void );
void thread_func( void );
int volatile somevar;

};

static void LaunchThread( myclass *A )
{
A->thread_func();
};

void myclass::myclone( void )
{
clone( (int(*)(void*))&LaunchThread, &stack, CLONE_VM|CLONE_FS|CLONE_FILES, this );
}

void myclass::thread_func( void )
{
somevar = 0;
while( somevar == 0 )
{
//do something
}
// write something
}

myclass A, B;

int main( void )
{
A->myclone();
B->myclone();
A->somevar = 1;
B->somevar = 1;

return 0;
}

and there the mess begins A->clone() does launch A->thread_func() , B->clone() does not; A->somevar = 1 as well as the same with B does not terminate the cycle in thread_func(); i've searched the internet for a solution but did not find one.

Could some one tell me the correct way ( or there is my mistake here ) to do multithreading with classes in Linux, pleaaaasee :)
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You never check the return value of clone(), which from a copy/paste/compile on my machine is -1 because &stack is NULL.
 

bevardis

Junior Member
Apr 9, 2002
2
0
0
actualy i check the value of clone in the original code ( the posted one was just an example ) and clone never returns -1,
also &stack is never NULL. ( this applies to this example too ).