pthreads

Alsa: snd_pci_readi() and real-time threads

女生的网名这么多〃 提交于 2020-03-26 05:40:12
问题 I've got a dedicated thread that caputures audio from Alsa through snd_pcm_readi(). Periodically I get a short read, meaning snd_pcm_readi() returns a positive integer lower than my buffer size, and there's obviously a 'pop' sound in my audio stream. Then I set the thread priority to real-time and this gives a tangible benefit, far less short reads, but this doesn't solve. Now the question: before going down the bumpy road of a real-time patched Linux kernel, there's something else I can do

How to calculate number of processor cores in PHP script (linux)?

我们两清 提交于 2020-03-18 11:55:11
问题 I'm trying to use pthreads for multithreading. I'm creating pool with constructor. First parameter is number of Workers. $pool = new Pool(8, 'WebWorker'); I want to detect count of processor cores automatically. Something like this: $pool = new Pool(get_processor_cores_number(), 'WebWorker'); How is it possible with PHP? 回答1: You can do something like that, of course your server should be in linux: function get_processor_cores_number() { $command = "cat /proc/cpuinfo | grep processor | wc -l"

Data race with detached pthread detected by valgrind

穿精又带淫゛_ 提交于 2020-03-18 07:26:21
问题 I am creating two detached pthread: pthread_attr_t tha1; void* fun1(void*){ return 0; } void* fun2(void*){ return 0; } int main(){ pthread_t th1, th2; pthread_attr_init(&tha1); pthread_attr_setdetachstate(&tha1, PTHREAD_CREATE_DETACHED); pthread_create( &th1, &tha1, fun1, 0); pthread_create( &th2, &tha1, fun2, 0); pthread_attr_destroy(&tha1); pthread_exit((void*) 0); return 0; } With the above code valgrind (helgrind tool) shows data race: Possible data race during write of size 1 at

Data race with detached pthread detected by valgrind

有些话、适合烂在心里 提交于 2020-03-18 07:26:10
问题 I am creating two detached pthread: pthread_attr_t tha1; void* fun1(void*){ return 0; } void* fun2(void*){ return 0; } int main(){ pthread_t th1, th2; pthread_attr_init(&tha1); pthread_attr_setdetachstate(&tha1, PTHREAD_CREATE_DETACHED); pthread_create( &th1, &tha1, fun1, 0); pthread_create( &th2, &tha1, fun2, 0); pthread_attr_destroy(&tha1); pthread_exit((void*) 0); return 0; } With the above code valgrind (helgrind tool) shows data race: Possible data race during write of size 1 at

Android NDK c创建新的线程

独自空忆成欢 提交于 2020-03-14 20:17:21
在jni的c/c++层创建一个新的线程只需要3步: 1.导入库 #include<pthread.h> 2.写好线程要做的事 void* run_1(void*); void* run_1(void* args){ ... } 3.调用方法 pthread_t thread_1; pthread_create(&thread_1,NULL,run_1,args); /////////////////////////////////////////////////////////////////////////////////// 但是这样的线程,缺少了JNIEnv指针,好像干不了什么事,所以就要做这个基础上,得到JNIEnv指针,并将该线程依附于java虚拟机之上,这样这个线程像java层过来的线程一样,能够干很多事情。 官方文档关于attachCurrentThread()的说明,好像勉强看得懂,就翻译一下试试。。。 The JNI interface pointer (JNIEnv) is valid only in the current thread. Should another thread need to access the Java VM, it must first call AttachCurrentThread() to attach itself to

Is a signal sent with kill to a parent thread guaranteed to be processed before the next statement?

安稳与你 提交于 2020-03-06 05:14:11
问题 Okay, so if I'm running in a child thread on linux (using pthreads if that matters), and I run the following command kill(getpid(), someSignal); it will send the given signal to the parent of the current thread. My question: Is it guaranteed that the parent will then immediately get the CPU and process the signal (killing the app if it's a SIGKILL or doing whatever else if it's some other signal) before the statement following kill() is run? Or is it possible - even probable - that whatever

Why is notify required inside a critical section?

时光毁灭记忆、已成空白 提交于 2020-02-25 04:10:44
问题 I'm reading this book here (official link, it's free) to understand threads and parallel programming. Here's the question. Why does the book say that pthread_cond_signal must be done with a lock held to prevent data race? I wasn't sure, so I referred to this question (and this question too), which basically said "no, it's not required". Why would a race condition occur? What and where is the race condition being described? The code and passage in question is as follows. ... The code to wake a

Why is notify required inside a critical section?

假如想象 提交于 2020-02-25 04:10:31
问题 I'm reading this book here (official link, it's free) to understand threads and parallel programming. Here's the question. Why does the book say that pthread_cond_signal must be done with a lock held to prevent data race? I wasn't sure, so I referred to this question (and this question too), which basically said "no, it's not required". Why would a race condition occur? What and where is the race condition being described? The code and passage in question is as follows. ... The code to wake a

Computing Sums Using Multithreading

爷,独闯天下 提交于 2020-02-24 03:55:22
问题 I am working on a program that is to take the input of 15,000 integers from a file. After reading the values, the thread should then create 10 threads with each thread responsible for computing the sum of their block (1,500 values each). Each thread will then print the sum of its values, and the main thread will compute the sum from all 10 threads. What I have is mind is to read in all values and store them in an int array while using an int to keep count of the number of values read (let's

Crash related to boost::function usage in thread pool

给你一囗甜甜゛ 提交于 2020-01-25 11:00:35
问题 I am trying to implement thread pool in C++ using pthread. I want to encapsulate logic related to threads management in one object which is taking ownership of these threads. That means whenever this object is destroyed, threads must be stopped and cleaned up. I've been testing my code and it turns out that I get segmentation fault when I destroy WorkerThreadManager object while there is boost::function called. See the code and backtrace from GDB. I don't really understand why it happens, as