小编Zey*_*eel的帖子

警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'long int'[-Wformat =]

这段代码是关于.

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

我是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);
Run Code Online (Sandbox Code Playgroud)

这是代码:

// 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) …
Run Code Online (Sandbox Code Playgroud)

c unix multithreading thread-synchronization

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

multithreading ×1

thread-synchronization ×1

unix ×1