Tag: pthreads

内存没有被释放但仍然可以访问,它是否泄漏?

通过检查valgrind,我看到在终止程序后没有释放5块内存,但它们仍然可以访问。 我需要被它打扰吗? 它是如何发生的? zhanwu@gelata:~/sandbox$ valgrind ./a.out ==2430== Memcheck, a memory error detector ==2430== Copyright (C) 2002-2010, and GNU GPL’d, by Julian Seward et al. ==2430== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info ==2430== Command: ./a.out ==2430== Hello world! Thread1 returns 1 Thread2 returns 10 Thread3 returns 10 ==2430== ==2430== HEAP SUMMARY: ==2430== […]

为什么线程称为轻量级进程?

线程是“轻量级的”,因为大部分开销已经通过创建其进程来完成。 我在其中一个教程中找到了这个。 有人可以详细说明它究竟意味着什么吗?

处理multithreading程序中的异步信号

Linux Programming Interface提到了一种在multithreading程序中处理异步信号的方法: 所有线程都阻止进程可能接收的所有异步信号。 最简单的方法是在创建任何其他线程之前阻止主线程中的信号。 随后创建的每个线程都将inheritance主线程信号掩码的副本。 使用sigwaitinfo() , sigtimedwait()或sigwait()创建一个接受传入信号的专用线程。 这种方法的优点是同步接收异步生成的信号。 由于它接受传入信号,专用线程可以安全地修改共享变量(在互斥控制下)并调用非异步安全function。 它还可以发信号通知条件变量,并且可以实现其他线程和进程通信和同步机制。 现在的问题是: 当内核想要传递信号时,它会选择任意一个进程内的线程。 从哪里可以知道将信号传递给专用线程? pthread API是非同步安全的函数。 那么我们如何在信号处理程序中使用它们?

当获取它的线程退出时,Mutex会发生什么?

假设有两个线程,主线程和线程B(由main创建)。 如果B获得了一个互斥锁(比如pthread_mutex)并且在没有解锁锁的情况下调用了pthread_exit。 那么互斥体会发生什么? 它变得免费吗?

在multithreading程序中捕获SIGINT

我正在编写一个multithreading程序,我想从用户处理一个可能的Ctrl-C命令来终止执行。 据我所知,无法保证能够取消每个工作线程的主线程将捕获信号。 因此,是否有必要对工作线程的代码使用不同的信号处理程序,以便任何人在信号到达时捕获信号,或者是否有另一种方法通过仅在主线程的代码中使用信号处理程序来执行此操作?

如何使用条件变量

Linux编程接口书有一段代码(生产者/消费者)来展示条件变量的工作原理: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int avail = 0; while (TRUE) { s = pthread_mutex_lock(&mtx); while (avail == 0) { /* Wait for something to consume */ s = pthread_cond_wait(&cond, &mtx); } while (avail > 0) { /* Consume all available units */ avail–; } s = pthread_mutex_unlock(&mtx); […]

生产者消费者计划使用信号量和pthreads

我已经为生产者 – 消费者问题写了一个代码。但是我没有得到输出。没有编译错误,但在我的程序中发出警告。我很困惑。非常努力。但是无法得到它。请告诉我什么在我的程序中是错误的。什么是正确的程序。我感到沮丧。请帮助伙计们。 这是代码 – #include #include #include #include #define BUFF_SIZE 5 /* total number of slots */ #define NP 3 /* total number of producers */ #define NC 3 /* total number of consumers */ #define NITERS 4 /* number of items produced/consumed */ typedef struct { int buf[BUFF_SIZE]; /* shared var */ int […]

pthreads – 加入一组线程,等待一个退出

在POSIX线程接口中,可以使用pthread_join(thread)来阻塞,直到指定的线程退出。 是否有类似的函数允许执行阻止,直到任何子线程退出? 这与wait() UNIX系统调用类似,但适用于子线程,而不适用于进程

pthread互斥锁无法正常工作

我目前正在从麻省理工学院开放课件中学习C语言,在C语言中称为实践编程。在讨论multithreading中的竞争条件时,讲义包含一个具有竞争条件的程序示例以及如何使用互斥锁解决它。 代码在Linux系统上按预期工作,但在OS X上没有。 #include #include #include pthread_mutex_t mutex; // Added to fix race condition unsigned int cnt = 0; void *count(void *arg) { int i; for (i = 0; i < 100000000; i++) { pthread_mutex_lock(&mutex); // Added to fix race condition cnt++; pthread_mutex_unlock(&mutex); // Added to fix race condition } return NULL; } int main() […]

Timespec:struct type错误c2011

使用Visual Studio 2015在C中执行Pthread程序时出现以下错误 错误C2011’timespec’:’struct’类型重新定义 以下是我的代码: #include #include #include void *calculator(void *parameter); int main(/*int *argc,char *argv[]*/) { pthread_t thread_obj; pthread_attr_t thread_attr; char *First_string = “abc”/*argv[1]*/; pthread_attr_init(&thread_attr); pthread_create(&thread_obj,&thread_attr,calculator,First_string); } void *calculator(void *parameter) { int x=atoi((char*)parameter); printf(“x=%d”, x); }