C 警告:格式‘;%d’;应为类型为‘;int’;,但参数2的类型为‘;长整型&x2019;[-Wformat=]

C 警告:格式‘;%d’;应为类型为‘;int’;,但参数2的类型为‘;长整型&x2019;[-Wformat=],c,multithreading,unix,thread-synchronization,C,Multithreading,Unix,Thread Synchronization,这段代码是关于 比赛条件: 调度和编译器行为在进程或线程同步中起着重要作用。演示同步需求的最简单场景来自两个线程/进程之间创建的竞态条件,这些线程/进程试图修改共享变量的值,这通常会导致数据不一致和错误结果。以下示例演示了这种情况: 我是C新手,我对这个警告的内容感到不安。警告是什么意思?我如何修复它。我写的代码在这里: q1.c: In function ‘runner’: q1.c:13:1: warning: format ‘%d’ expects argument of type ‘in

这段代码是关于

比赛条件: 调度和编译器行为在进程或线程同步中起着重要作用。演示同步需求的最简单场景来自两个线程/进程之间创建的竞态条件,这些线程/进程试图修改共享变量的值,这通常会导致数据不一致和错误结果。以下示例演示了这种情况:

我是C新手,我对这个警告的内容感到不安。警告是什么意思?我如何修复它。我写的代码在这里:

q1.c: In function ‘runner’:
q1.c:13:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
 printf("T tid: %d x before: %d\n", syscall(SYS_gettid),x); int i;
 ^
q1.c:19:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
 printf("T tid: %d x after: %d\n", syscall(SYS_gettid),x);
代码如下:

// Race condition
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
int x=0;
void * runner(void *arg)
{
printf("T tid: %d   x before: %d\n", syscall(SYS_gettid),x); int i;
for (i = 0; i < 100000; i++ )
{
x = x + 1;
}
printf("T tid: %d   x after: %d\n", syscall(SYS_gettid),x);
}

int program()
{
pthread_t t1,t2,t3,t4;
printf("Parent pid: %d  x before threads: %d\n", getpid(),x); int i;
if(pthread_create(&t1,NULL, runner, NULL)){ printf("Error creating thread 1\n"); return 1;
}
if(pthread_create(&t2,NULL, runner, NULL)){ printf("Error creating thread 2\n"); return 1;
}
if(pthread_create(&t3,NULL, runner, NULL)){ printf("Error creating thread 1\n"); return 1;
}
if(pthread_create(&t4,NULL, runner, NULL)){ printf("Error creating thread 1\n"); return 1;
}

if(pthread_join(t1,NULL)){ printf("error joining thread 1"); return 1;
}
if(pthread_join(t2,NULL)){ printf("error joining thread 1"); return 1;
}
if(pthread_join(t3,NULL)){ printf("error joining thread 1"); return 1;
}

if(pthread_join(t4,NULL)){ printf("error joining thread 1"); return 1;
}
printf("Parent pid: %d  x after threads: %d\n", getpid(),x); return 0;
}

int main(int argc, char *argv[]) { 
int count=0;
// loop runs the program count times 
while(count<5)
{
// running program program();
count++;
//reset global x for next run of program. x=0;
printf("\n\n");
}
return 0;
}
//竞争条件
#包括
#包括
#包括
#包括
#包括
#包括
int x=0;
空*流道(空*参数)
{
printf(“tTID:%dx之前:%d\n”,syscall(SYS\u gettid),x);int i;
对于(i=0;i<100000;i++)
{
x=x+1;
}
printf(“tTID:%d之后的x:%d\n”,syscall(SYS\u gettid),x);
}
int程序()
{
pthread_t t1、t2、t3、t4;
printf(“线程%d\n之前的父pid:%d x”,getpid(),x);int i;
if(pthread_create(&t1,NULL,runner,NULL)){printf(“错误创建线程1\n”);返回1;
}
if(pthread_create(&t2,NULL,runner,NULL)){printf(“错误创建线程2\n”);返回1;
}
if(pthread_create(&t3,NULL,runner,NULL)){printf(“创建线程1时出错”);返回1;
}
if(pthread_create(&t4,NULL,runner,NULL)){printf(“创建线程1时出错”);返回1;
}
if(pthread_join(t1,NULL)){printf(“错误连接线程1”);返回1;
}
if(pthread_join(t2,NULL)){printf(“错误连接线程1”);返回1;
}
if(pthread_join(t3,NULL)){printf(“错误连接线程1”);返回1;
}
if(pthread_join(t4,NULL)){printf(“错误连接线程1”);返回1;
}
printf(“线程%d\n之后的父pid:%d x”,getpid(),x);返回0;
}
intmain(intargc,char*argv[]){
整数计数=0;
//循环运行程序计数次

(count你必须用
%ld
来更改
%d
%d
表示有符号的
int
,这里的
l
代表
长的
%ld
代表有符号的
长的int

写代码时,应该始终带着人类阅读它的想法。 甚至作者在阅读代码时也遇到了重大问题 如许多“复制和粘贴”错误所示

注意:当在一行上开始“//”注释时,该行的所有其余部分都是注释

除了main之外的任何函数都应该有一个原型。 在调用被调用函数之前声明被调用函数的方法 将适用于普通程序,但不适用于多文件程序或 具有交叉调用路径的程序 即,始终提供原型声明

调用某个函数时,始终了解(即查找)全部细节 关于这个功能,在OPs发布代码的情况下, 这尤其适用于syscall()函数

在编写线程时,需要了解的不仅仅是原型 在OPs代码中,还需要知道如何退出线程

编译时始终启用所有警告。警告是问题 在继续之前应修复的代码中

顺便说一句:使用一个互斥锁可以消除竞争条件问题

// Race condition
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>

void *runner( void * );
int   program( void );

int x=0;


// <-- compiler raises warning about unused parameter
void * runner(void *arg)
{
    // the returned value from the syscall is a long int
    //printf("T tid: %d   x before: %d\n",
    printf("T tid: %ld   x before: %d\n",
            syscall(SYS_gettid),
            x);
    int i;

    for (i = 0; i < 100000; i++ )
    {
        x = x + 1;
    }

    // <-- the returned value from syscall is a long int
    //printf("T tid: %d   x after: %d\n",
    printf("T tid: %ld   x after: %d\n",
            syscall(SYS_gettid),
            x);

    // <-- this line missing, compiler raises warning
    pthread_exit(NULL);
} // end function: runner


int program()
{
    pthread_t t1,t2,t3,t4;

    printf("Parent pid: %d  x before threads: %d\n",
            getpid(),
            x);

    //int i; // <-- this variable not used, results in compiler warning


    if( pthread_create(&t1, NULL, runner, NULL) )
    {
        printf("Error creating thread 1\n");
        return 1;
    }

    if( pthread_create(&t2, NULL, runner, NULL) )
    {
        printf("Error creating thread 2\n");
        return 1;
    }

    if( pthread_create(&t3, NULL, runner, NULL) )
    {
        // <-- copy and paste error
        //printf("Error creating thread 1\n");
        printf("Error creating thread 3\n");
        return 1;
    }

    if( pthread_create(&t4, NULL, runner, NULL) )
    {
        // <-- copy and paste error
        //printf("Error creating thread 1\n");
        printf("Error creating thread 4\n");
        return 1;
    }


    if( pthread_join(t1, NULL) )
    {
        printf("error joining thread 1");
        return 1;
    }

    if( pthread_join(t2, NULL) )
    {
        // <-- copy and paste error
        //printf("error joining thread 1");
        printf("error joining thread 2");
        return 1;
    }

    if( pthread_join(t3, NULL) )
    {
        // <-- copy and paste error
        //printf("error joining thread 1");
        printf("error joining thread 3");
        return 1;
    }

    if( pthread_join(t4, NULL) )
    {
        // <-- copy and paste error
        //printf("error joining thread 1");
        printf("error joining thread 4");
        return 1;
    }

    printf("Parent pid: %d  x after threads: %d\n",
            getpid(),
            x);
    return 0;
} // end function: program


// <-- this line cause compiler to raise two warnings about unused parameters
//int main(int argc, char *argv[])
int main()
{
    int count=0;

    // <-- there is no loop code perhaps you meant to put the 'while' on the next line
    // loop runs the program count times while(count<5)
    while( count < 5 )
    {
        // <-- program is not run, perhaps you meant to put the 'program()' on the next line
        // running program program();
        program();

        count++;
        // <-- x is not being reset, perhaps you menat to put 'x=0;' on the next line
        //reset global x for next run of program. x=0;
        x=0;
        printf("\n\n");
    }

    return 0;
}  // end function: main
//竞争条件
#包括
#包括
#包括
#包括
#包括
#包括
无效*转轮(无效*);
int程序(无效);
int x=0;
//的原型返回一个
long

#include <sys/syscall.h>
long syscall(long number, ...);

适当的缩进将使代码更易于阅读。请注意,复制“n”粘贴会导致您创建(或未能创建)三次“线程1”,并导致加入(或未能加入)“线程1”四次。除了需要使用数组外,您还需要记住在复制代码时完全编辑代码。值得注意的是,虽然这几乎永远都是正确的(--1996年),但文档仅在几个月前更新(),所以错误是可以理解的。错误信息对我来说似乎很清楚,但我的英语和C都很流利。)
                 v---- long --------v 
                              v----------- int ----------v
printf("T tid: %ld x before: %d\n", syscall(SYS_gettid), x);