- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是对 this 的跟进问题。
在该代码中,当我不使用 fflush(stdout)
时,当我保持 sleep(1)
时,输出不会刷新到屏幕上。
#define S sleep(0)
void* xThread_fn(void* arg)
{
while(1)
{
S;
pthread_mutex_lock(&read_c_mutex);
if(!read_c)
{
pthread_mutex_unlock(&read_c_mutex);
printf(" X");
}
else
{
pthread_mutex_unlock(&read_c_mutex);
pthread_exit(NULL);
}
fflush(stdout); <---THIS ONE HERE
}
}
但是当我保持 sleep(0)
时,就不需要 fflush(stdout)
,输出会在 stdout
上正确更新。为什么会这样?
Q1.为什么 sleep(0)
的存在会导致刷新输出的方式发生任何变化?
如果我修改代码如下(跟踪执行),
#define S sleep(1)
int read_c = 0;
pthread_mutex_t read_c_mutex = PTHREAD_MUTEX_INITIALIZER;
void* inputThread_fn(void* arg)
{
printf("%p is Input\n",pthread_self());
char inputChar;
int i = 0;
while(1)
{
S;
printf("\nChecking input");
scanf("%c",&inputChar);
if(inputChar=='C' || inputChar == 'c')
{
pthread_mutex_trylock(&read_c_mutex);
printf("%p has lock %d\n",pthread_self(),i);
read_c = 1;
pthread_mutex_unlock(&read_c_mutex);
printf("%p has UNlockED %d\n",pthread_self(),i++);
printf("%p is Gone!\n",pthread_self());
fflush(stdout);
pthread_exit(NULL);
}
}
}
void* xThread_fn(void* arg)
{
int i = 0;
printf("%p is X\n",pthread_self());
while(1)
{
S;
printf("X trying for a lock\n");
pthread_mutex_trylock(&read_c_mutex);
printf("%p has lock %d\n",pthread_self(),i);
if(!read_c)
{
pthread_mutex_unlock(&read_c_mutex);
printf("%p has UNlockED %d\n",pthread_self(),i++);
printf("X\n");
fflush(stdout);
}
else
{
printf("%p is Gone!\n",pthread_self());
pthread_mutex_unlock(&read_c_mutex);
fflush(stdout);
pthread_exit(NULL);
}
}
}
void* yThread_fn(void* arg)
{
printf("%p is Y\n",pthread_self());
int i = 0;
while(1)
{
S;
printf("Y trying for a lock\n");
pthread_mutex_trylock(&read_c_mutex);
printf("%p has lock %d\n",pthread_self(),i);
if(!read_c)
{
pthread_mutex_unlock(&read_c_mutex);
printf("%p has UNlockED %d\n",pthread_self(),i++);
printf("Z\n");
fflush(stdout);
}
else
{
printf("%p is Gone!\n",pthread_self());
pthread_mutex_unlock(&read_c_mutex);
fflush(stdout);
pthread_exit(NULL);
}
}
}
示例输出是
0xb6700b70 is Input
0xb6f01b70 is Y
0xb7702b70 is X
Checking inputY trying for a lock
0xb6f01b70 has lock 0
0xb6f01b70 has UNlockED 0
Z
X trying for a lock
0xb7702b70 has lock 0
0xb7702b70 has UNlockED 0
X
Y trying for a lock
0xb6f01b70 has lock 1
0xb6f01b70 has UNlockED 1
Z
X trying for a lock
0xb7702b70 has lock 1
0xb7702b70 has UNlockED 1
X
Y trying for a lock
0xb6f01b70 has lock 2
0xb6f01b70 has UNlockED 2
Z
X trying for a lock
0xb7702b70 has lock 2
0xb7702b70 has UNlockED 2
X
Y trying for a lock
0xb6f01b70 has lock 3
0xb6f01b70 has UNlockED 3
Z
X trying for a lock
0xb7702b70 has lock 3
0xb7702b70 has UNlockED 3
X
Y trying for a lock
0xb6f01b70 has lock 4
0xb6f01b70 has UNlockED 4
Z
X trying for a lock
0xb7702b70 has lock 4
0xb7702b70 has UNlockED 4
X
c
Y trying for a lock
0xb6f01b70 has lock 5
0xb6f01b70 has UNlockED 5
Z
X trying for a lock
0xb7702b70 has lock 5
0xb7702b70 has UNlockED 5
X
0xb6700b70 has lock 0
0xb6700b70 has UNlockED 0
0xb6700b70 is Gone!
Y trying for a lock
0xb6f01b70 has lock 6
0xb6f01b70 is Gone!
X trying for a lock
0xb7702b70 has lock 6
0xb7702b70 is Gone!
Q2. 我使用了 pthread_mutex_trylock()
因为我希望代码在 while 循环中继续执行,直到它获取一个锁来检查 read_c
。 pthread_mutex_lock();
似乎也能达到同样的效果。这让我更加困惑。使用pthread_mutex_trylock();
也是,输出总是这样?一个 X
后跟一个 Z
。会不会出现像X
X
Z
X
这样的情况(前提是线程切换了由操作系统和 ythread 尝试锁定但失败了)?
最佳答案
回答“trylock”问题。
pthread_mutex_trylock
仅尝试锁定互斥体。如果其他人锁定了互斥锁,它只会返回一个错误并继续运行。由于您没有检查返回值,因此您可能会在不持有互斥锁的情况下触及受互斥锁保护的数据。
您的代码相当于根本没有任何互斥量。
pthread_mutex_trylock
只应在您出于某些非常特殊的原因不能等待锁定并且在获取互斥量失败时回退到不同行为的情况下使用。在不检查返回值的情况下调用它总是一个错误。
为了完全正确,您还应该检查 pthread_mutex_lock
的返回值。但是你通常可以不这样做而逃脱。如果不检查 trylock 的返回值,您将永远无法逃脱。
关于c - `pthread_mutex_trylock` 和 `pthread_mutex_lock` 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15832533/
我花了一周的时间来修复一个程序,一开始我得到了SIGBUS,但经过多次尝试后程序仍然得到了SIGSEGV段错误,在下面我发布了段错误日志+源代码。如果专家帮助我修复此段错误错误,我将非常感激。任何建议
我正在使用 pthread_mutex_trylock 将互斥锁锁定在一个结构上,以便它只能在给定时间由单个线程访问/修改。如果互斥体已经被锁定,我只是从例程返回而不是排队/阻塞。 这是我的代码的基本
在我的代码中,我使用 pthread_mutx_trylock() 来检查线程 1 是否已完成他的任务工作并释放互斥锁吗?请让我知道它是否有效? 在线程 1 中: pthread_mutex
/////////////////////////////////* pthread_mutex_t stop = PTHREAD_MUTEX_INITIALIZER; int a = 1; void
我正在学习在 Linux 编程中使用互斥量。我遇到了 trylock 函数,它首先检查互斥量是否可用,否则将其锁定,然后返回。 现在我的问题是: 调用trylock时,是否在未执行临界区的情况下到达函
最近看了memcached的源码,在文件 memcached.h 中。我找到了这段代码: static inline int mutex_lock(pthread_mutex_t *mutex) {
我有以下测试程序。 #include #include using namespace std; pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIA
pthread_mutex_trylock 检测死锁,不阻塞,那你为什么“需要” pthread_mutex_lock? 也许当你故意想让线程阻塞?但那样的话可能会导致死锁? 最佳答案 pthread
一旦线程被取消,就需要解锁互斥体,以避免死锁。所以我设计了以下方法: // file_a.c pthread_attr_t attr; ... rc2 = pthread_attr_init(&att
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
这是对 this 的跟进问题。 在该代码中,当我不使用 fflush(stdout) 时,当我保持 sleep(1) 时,输出不会刷新到屏幕上。 #define S sleep(0) void* xT
我是一名优秀的程序员,十分优秀!