Thread - Argument passing

Thread - Argument passing

Post by <net.. » Thu, 24 Jun 1999 04:00:00



Hi all.

I am testing linux pthread on redhat linux.

I would like to pass local variable when a thread is created.

How can i do this?

See following example i wrote.

------------------------------------------------
int gsd; // global variable declaration

CreateThread(int sd)
{
        gsd = sd;
        printf("Address of SD = %x\n",&sd);
        pthread_create(0,0,Handler,&sd); // case 1
        pthread_create(0,0,Handler,&gsd); // case 2

Quote:}

void *Handler(void *arg)
{
        int sd;

        memcpy(&sd,arg,sizeof(sd));

        printf("Arg = %x\n",arg);
        printf("is SD correct ? %d\n",sd);

        // do something...

Quote:}

In case 2, this program works well, the argumnet is passed correctly.
In case 1, wrong argument is passing, but arg still points to the address of sd.
        I think it is because sd is local variable ( kept in stack space. )
        And thread is created but not executed immediately.
        So, the stack area allocated to sd is reused by other function call.

Is there any other way to pass local variable when creating thread?
( If i have to use global variable, i have to write code for
critical section problem. It is very complex to me)

Thank you in advance!!


POSTECH, Republic Of Korea

 
 
 

Thread - Argument passing

Post by mlw » Thu, 24 Jun 1999 04:00:00



> Hi all.

> I am testing linux pthread on redhat linux.

> I would like to pass local variable when a thread is created.

> How can i do this?

> See following example i wrote.

> ------------------------------------------------
> int gsd; // global variable declaration

> CreateThread(int sd)
> {
>         gsd = sd;
>         printf("Address of SD = %x\n",&sd);
>         pthread_create(0,0,Handler,&sd); // case 1
>         pthread_create(0,0,Handler,&gsd); // case 2
> }

> void *Handler(void *arg)
> {
>         int sd;

>         memcpy(&sd,arg,sizeof(sd));

>         printf("Arg = %x\n",arg);
>         printf("is SD correct ? %d\n",sd);

>         // do something...
> }

> In case 2, this program works well, the argumnet is passed correctly.
> In case 1, wrong argument is passing, but arg still points to the address of sd.
>         I think it is because sd is local variable ( kept in stack space. )
>         And thread is created but not executed immediately.
>         So, the stack area allocated to sd is reused by other function call.

> Is there any other way to pass local variable when creating thread?
> ( If i have to use global variable, i have to write code for
> critical section problem. It is very complex to me)

This is threading 101.

In neither case should the variables be reliable. In both cases you are
using automatic variables to pass information to a thread. If the thread
is not started right away, i.e. scheduled to start, the variables will
be gone.

The only reasion case 2 works is because it is lower down the stack and
has not been overwritten by the time the thread has been run.

I usually wrap threads in C++ classes:

class MyThread;

void ThreadThunk(void *thread)
{
        ((MyThread *)thread)->ThreadProc();

        delete (MyThread *)thread;

Quote:}

class MyThread
{
        protected:
        pthread_t       m_pthread;
        public:
                        MyThread();
        virtual         ~MyThread();
        virtual int     ThreadProc(void);
        virtual int     Run();

Quote:};

MyThread::MyThread()
{
        //Init as needed

Quote:}

MyThread::~MyThread()
{
        // Cleanup as needed
Quote:}

MyThreadProcThreadProc(void)
{
        while(1)
                ;

Quote:}

int MyThread::Run()
{
        return pthread_create(&m_pthread,0,ThreadThunk, (void *)this);

Quote:}

Now all one has to do is inherit from MyThread and to Add there
functionality:

class YourThread : public MyThread
{
        protected:
        m_sd;

        public:
        int ThreadProc()
        {
                // Check sd;
                while(1)
                        ; // Do something
        }
        YourThread(int sd)
        {
                m_sd = sd;
        }      

Quote:};

int Function(int sd)
{
        YourThread *yt = new YourThread(sd);
        return yt->Run();

Quote:}

--
Mohawk Software
Windows 95, Windows NT, UNIX, Linux. Applications, drivers, support.
Visit http://www.mohawksoft.com

 
 
 

Thread - Argument passing

Post by Duncan Stodar » Fri, 25 Jun 1999 04:00:00


Allocate memory for the parameters, pass pointer to memory as argument, and
free the memory in the thread after extracting parameters.  Only way other
than global variables.


>Hi all.

>I am testing linux pthread on redhat linux.

>I would like to pass local variable when a thread is created.

>How can i do this?

>See following example i wrote.

>------------------------------------------------
>int gsd; // global variable declaration

>CreateThread(int sd)
>{
> gsd = sd;
> printf("Address of SD = %x\n",&sd);
> pthread_create(0,0,Handler,&sd); // case 1
> pthread_create(0,0,Handler,&gsd); // case 2
>}

>void *Handler(void *arg)
>{
> int sd;

> memcpy(&sd,arg,sizeof(sd));

> printf("Arg = %x\n",arg);
> printf("is SD correct ? %d\n",sd);

> // do something...
>}

>In case 2, this program works well, the argumnet is passed correctly.
>In case 1, wrong argument is passing, but arg still points to the address
of sd.
> I think it is because sd is local variable ( kept in stack space. )
> And thread is created but not executed immediately.
> So, the stack area allocated to sd is reused by other function call.

>Is there any other way to pass local variable when creating thread?
>( If i have to use global variable, i have to write code for
>critical section problem. It is very complex to me)

>Thank you in advance!!


>POSTECH, Republic Of Korea

 
 
 

1. script questions - passing argument as argument to another program

Hello.

I have a script that I want to take an argument and pass it to the
passwd program.

1.  How in any situation do I take an argument in a script and pass it
to a program?  I can type the command in like: passwd $1 , but I do
not know how to simulate the enter key being pressed at that point.

2.  How do I allow other users to run this script?  I want the script
itself to execute as root when run since there are commands only root
may run, and I will only give other people in a certain group the
ability to execute the script.

3.  Is there a way to associate a file with multiple groups?

Thank you for your time.

Andrew

please remove 'REMOVE' characters to e-mail me

2. LinuxPPC boot issue on PPC7500

3. How to pass arguments passed in ''s

4. Netscape and Plugins

5. Passing Arguments

6. Tyan Thunder K7 & 2 1.2Ghz Athlon MPs

7. Passing arguments from the form via BASH script

8. dmsdosfs

9. passing awk variables as arguments

10. passing arguments to an alias

11. script argument passing

12. passing arguments with spaces in Bourne shell script

13. passing argument to handler