Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to thread in C#

all I have searched this question, and I found so many answers to it was not difficult to find a solution for my question. BUT, I have strange experience and I don't know the reason that's why I ask people to give me some advice. Here are my codes:

    void SetThread()
    {
        for (int i = 0; i < _intArrayLength; i++)
        {
            Console.Write(string.Format("SetThread->i: {0}\r\n", i));
            _th[i] = new Thread(new ThreadStart(() => RunThread(i)));
            _th[i].Start();
        }
    }

    void RunThread(int num)
    {
        Console.Write(string.Format("RunThread->num: {0}\r\n", num));
    }

Yes, they are ordinary thread codes. I expect all the thread array should be calling RunThread method 10 times. It should be like

SetThread->i: 0
SetThread->i: 1
SetThread->i: 2
SetThread->i: 3
SetThread->i: 4
SetThread->i: 5
SetThread->i: 6
SetThread->i: 7
SetThread->i: 8
SetThread->i: 9
RunThread->num: 0
RunThread->num: 1
RunThread->num: 2
RunThread->num: 3
RunThread->num: 4
RunThread->num: 5
RunThread->num: 6
RunThread->num: 7
RunThread->num: 8
RunThread->num: 9

This is what I expect to be. The order is not important. But I get the result like below.

SetThread->i: 0
SetThread->i: 1
SetThread->i: 2
The thread '<No Name>' (0x18e4) has exited with code 0 (0x0).
The thread '<No Name>' (0x11ac) has exited with code 0 (0x0).
The thread '<No Name>' (0x1190) has exited with code 0 (0x0).
The thread '<No Name>' (0x1708) has exited with code 0 (0x0).
The thread '<No Name>' (0xc94) has exited with code 0 (0x0).
The thread '<No Name>' (0xdac) has exited with code 0 (0x0).
The thread '<No Name>' (0x12d8) has exited with code 0 (0x0).
The thread '<No Name>' (0x1574) has exited with code 0 (0x0).
The thread '<No Name>' (0x1138) has exited with code 0 (0x0).
The thread '<No Name>' (0xef0) has exited with code 0 (0x0).
SetThread->i: 3
RunThread->num: 3
RunThread->num: 3
RunThread->num: 3
SetThread->i: 4
RunThread->num: 4
SetThread->i: 5
SetThread->i: 6
RunThread->num: 6
RunThread->num: 6
SetThread->i: 7
RunThread->num: 7
SetThread->i: 8
RunThread->num: 8
SetThread->i: 9
RunThread->num: 9
RunThread->num: 10

What I expect is that RunThread function should carry the argument(num) from 0 to 9. And I cannot figure out what that error message is. "The thread '' ~~ and so on. Could anyone give me some clue on this?

like image 490
Joshua Son Avatar asked Aug 12 '13 00:08

Joshua Son


People also ask

Can you pass arguments to a thread?

You can only pass a single argument to the function that you are calling in the new thread. Create a struct to hold both of the values and send the address of the struct.

How do I pass multiple arguments to a thread function?

For cases where multiple arguments must be passed, this limitation is easily overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure in the pthread_create() routine. All arguments must be passed by reference and cast to (void *).

How do you pass a structure to a thread?

Your thread can access this struct by passing a pointer through the fourth argument of pthread_create() . Create a pointer to your struct as illustrated in the last exercise and do so. struct struct_name* arg_ptr = (struct struct_name*) args; printf("thread arg1: %d\n", arg_ptr->arg1);

How can you pass arguments to a function in C?

To pass an argument by reference, you prefix the variable name with & , unless it is already a pointer, as in the case when an array is being passed. As part of the build process, the compiler may convert arguments from one data type to another.


1 Answers

You are creating a closure over the loop variable - an easy fix is to just create a local copy, so your thread uses the desired value:

void SetThread()
    {
        for (int i = 0; i < _intArrayLength; i++)
        {
           int currentValue = i;
            Console.Write(string.Format("SetThread->i: {0}\r\n", i));
            _th[i] = new Thread(() => RunThread(currentValue));
            _th[i].Start();
        }
    }
like image 77
BrokenGlass Avatar answered Oct 06 '22 12:10

BrokenGlass