c - Linux互斥检查程序是否已经运行?

标签 c linux mutex

谁能告诉我为什么下面的例子不能作为 Linux 下的互斥锁的例子

#include <pthread.h>
int main (){
pthread_mutex_t start;
if (pthread_mutex_init(&start, NULL) != 0){
     printf("err!");
     return(1);
}
if (pthread_mutex_lock(&start) != 0){
     printf("err!");
     return(1);
}

pthread_mutex_unlock(&start);
pthread_mutex_destroy(&start);
}

最佳答案

pthread 互斥量用于在一个程序中同步线程。如果你启动这个程序两次,你会得到两个不同的互斥体。对于多个进程之间的同步,还有其他工具(在这种情况下文件锁可能是最简单的)。

关于c - Linux互斥检查程序是否已经运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4114695/

相关文章:

并发 I/O - 缓冲区损坏、 block 设备驱动程序

c++ - glibc 在 RHEL 5 中损坏了双链表

xml - 使用 sed 在 xml 上 move 文本

c - 如何在相互依赖的C文件中定义pthread和互斥锁?

c - 线程与互斥体同步

c - 从 24bpp 位图中获取每个像素的 RGB 值以在 C 中转换为 GBA 格式

python - 编程语言如何调用用另一种语言编写的代码?

c++ - mutable boost::mutex 是否可以将锁定和等待功能分开?

返回 C.CString 时出现 CGo 段错误

c - 类似于 c 中的 dict