Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-局部线程变量是共享的_C_Multithreading - Fatal编程技术网

C-局部线程变量是共享的

C-局部线程变量是共享的,c,multithreading,C,Multithreading,我一直在尝试将一个变量传递给线程,但是当创建第二个线程时,值会发生变化,尽管它是作为常量变量创建的 //for the first user if (flag == 0) { //store the socket cl_sc[0] = client_sock_desc; //set id whichOne[0] = 0; puts("Client accepted"); //second user ,same procedure } else if

我一直在尝试将一个变量传递给线程,但是当创建第二个线程时,值会发生变化,尽管它是作为常量变量创建的

//for the first user
if (flag == 0) {
    //store the socket
    cl_sc[0] = client_sock_desc;
    //set id
    whichOne[0] = 0;
    puts("Client accepted");
    //second user ,same procedure
}
else if (flag == 1) {
    cl_sc[1] = client_sock_desc;
    whichOne[0] = 1;
    puts("Client accepted");
}

//create thread and pass cl_sc as arguement
pthread_t sTcThread;
pthread_create(&sTcThread, NULL, server_to_client, (void*)whichOne);
这是线程的实现

void* server_to_client(void* socket_desc)
{
    //make the arguement readable
    const int* whichOne = (int*)socket_desc;
    //one int for retrieved data and one for his socket
    int retrieve, socket = cl_sc[whichOne[0]];
    //chat buddy socket
    int palsSocket;

    //the actual data
    char data[DATA_LENGTH];
    //free the string
    memset(data, 0, DATA_LENGTH);
    for (;;) {
        //set accordingly
        if (whichOne[0] == 0) {
            palsSocket = cl_sc[1];
        }
        else if (whichOne[0] == 1) {
            palsSocket = cl_sc[0];
        }
        printf("Im %d to join my socket is %d and my pals socket is %d\n", whichOne[0], socket, palsSocket);
    }
}
执行结果

客户端接受Im 0以加入我的套接字为4,我的好友套接字为0
客户接受Im 1加入我的套接字是5,我的好友套接字是4

但是一旦线程0被激活,这种情况就会发生

我1加入我的插座是4,我的朋友插座是4

将whichOne常量更改为1

感谢您的回答和评论,问题得以解决

    int *whichOneImported = (int *) socket_desc;
    int whichOne = whichOneImported[0];

const int*whichOne=(int*)套接字描述
保证子例程的指令不会更改
whichOne
,但不会更改另一个线程,因为它指向共享内存区域(创建线程时将地址传递给
whichOne
数组,然后在
标志==1
时修改其值)

设置
whichOne[0]=1在另一个线程中,我不确定逻辑,但可能您的意思是:

whichOne[flag] = flag;
然后

pthread_create(&sTcThread, NULL, server_to_client, (void*) (whichOne+flag));
分离数据


或者在运行线程之前创建一个分配的
whichOne
副本。

我认为对“为什么”的解释可能会有用,但这是正确的答案。因此指针是一个常量,但不是实际值。有什么方法可以使值保持不变吗?我想您为此定义了
whichOne[3]
插槽,但没有使用它们。其他插槽在其他地方使用。我希望whichOne[0]在其线程中的值是分开的,但这似乎不是一个有效的解决方案。然后将线程插槽加倍/向例程传递一个“标志”索引指针。另外:如果要修复值,可以复制数组。