pthreads

graceful thread termination with pthread_cond_signal proving problematic

旧街凉风 提交于 2020-01-04 05:18:06
问题 I need to fire of a bunch of threads and would like to bring them down gracefully. I'm trying to use pthread_cond_signal / pthread_cond_wait to achieve this but am running into a problem. Here's my code. firstly the thread_main static void *thrmain( void * arg ) { // acquire references to the cond var, mutex, finished flag and // message queue ..... while( true ) { pthread_mutex_lock( &lock ); if ( msq.empty() ) { // no messages so wait for one. pthread_cond_wait( &cnd, &lock ); } // are we

What happens if no threads are waiting and condition signal was sent?

我怕爱的太早我们不能终老 提交于 2020-01-04 04:23:06
问题 What happens if all threads are busy and main thread has sent thread cond signal ? 1 Main Thread and 3 pthreads in thread pool. 3 pthreads are in status of pthread_mutex_lock(&sync_mutex); pthread_cond_wait(&sync_cond, &sync_mutex); pthread_mutex_unlock(&sync_mutex); Main thread has sent Signal to wake up the threads to process the work. In this situation, What if 3 threads are already busy and next signal has arrived? 回答1: Nothing. The signal disappears. 回答2: If your using one of the

Multithreading in PHP 7

拥有回忆 提交于 2020-01-04 02:33:08
问题 How to do multithreading in PHP7? The first problem I see with pthread is coming directly from PHP manual. https://secure.php.net/manual/en/intro.pthreads.php The pthreads extension cannot be used in a web server environment . Threading in PHP should therefore remain to CLI-based applications only. Is is safe to oversee this warning, and spawn some threads on HTTP requests? 回答1: Is is safe to oversee this warning, and spawn some threads on HTTP requests? The extension itself prohibits loading

Handling segfault signal SIGSEGV need to determine the cause of segfault using siginfo_t

独自空忆成欢 提交于 2020-01-04 02:11:18
问题 I'm making a wrapper for the pthread library that allows each thread to have its own set of non-shared memory. Right now the way c is set up if any thread tries to rwe another threads data, the program segfaults. This is fine, I can catch it with a sighandler and call pthread_exit() and continue on with the program. But not every segfault is going to be the result of a bad rwe. I need to find a way to use the siginfo type to determine if the segfault was bad programming or this error. Any

Why is the multithreaded version of this program slower?

删除回忆录丶 提交于 2020-01-03 22:14:29
问题 I am trying to learn pthreads and I have been experimenting with a program that tries to detect the changes on an array. Function array_modifier() picks a random element and toggles it's value (1 to 0 and vice versa) and then sleeps for some time (big enough so race conditions do not appear, I know this is bad practice). change_detector() scans the array and when an element doesn't match it's prior value and it is equal to 1, the change is detected and diff array is updated with the detection

pthread mutex locking variables used in statements

[亡魂溺海] 提交于 2020-01-03 20:16:53
问题 First of all, I have a gut feeling that says, inside an if statement, if I am using the variable, it counts as reading the variable so I should lock it with mutex there also (if another pthread might be doing stuff with it). Am I right that I should lock it? The example situation in simplified manner is given below. in one thread I am using the below statement: if(event){ // Should I or should I not lock event here to use it // inside if statement? pthread_mutex_lock(&mutex_event); event = 0;

Why does gcc link without the lpthread flag?

有些话、适合烂在心里 提交于 2020-01-03 13:58:48
问题 I was working on a hobby project where mutexes were behaving mysteriously. I boiled it down to this test case that should obviously deadlock. #include <pthread.h> #include <stdio.h> int main() { pthread_mutex_t test; pthread_mutex_init(&test, NULL); pthread_mutex_lock(&test); pthread_mutex_lock(&test); printf("Took lock twice\n"); return 0; } However, when I compile without the -lpthread flag, not only does the program still compile and link, it also runs without deadlocking. Why? gcc pthread

Why does gcc link without the lpthread flag?

微笑、不失礼 提交于 2020-01-03 13:58:11
问题 I was working on a hobby project where mutexes were behaving mysteriously. I boiled it down to this test case that should obviously deadlock. #include <pthread.h> #include <stdio.h> int main() { pthread_mutex_t test; pthread_mutex_init(&test, NULL); pthread_mutex_lock(&test); pthread_mutex_lock(&test); printf("Took lock twice\n"); return 0; } However, when I compile without the -lpthread flag, not only does the program still compile and link, it also runs without deadlocking. Why? gcc pthread

Dining Philosopher Program C

佐手、 提交于 2020-01-03 06:35:30
问题 I am working with the classic dining philosopher problem with 5 philosophers and 5 chopsticks. My homework is to use 1 mutex and 5 conditions. I got it working but I don't know why philosopher 1 never eats, but 4,3,0 eat and 2 eats twice. Here's my code (sorry for the length): //To compile: gcc -pthread -o monitor monitor.c //To execute: ./monitor "an integer as the amount of food (default = 5)" #include <pthread.h> #include <stdio.h> #include <stdlib.h> // There are 5 philosophers (0,1,2,3,4

Using Pthread in Azure web app in a PHP site

穿精又带淫゛_ 提交于 2020-01-03 05:16:31
问题 I have a webapp (PHP site using CodeIgniter) hosted on Azure. I have a situation where I want to use the pthread module. Since pthread is not available in PHP out of the box, when I install it using Xampp server, I suspect that it won't be possible to use threading on Azure. But I am not sure. Azure documentation also doesn't talk about it. Is there a way I can use it pthread on Azure? I am able to use it on my local Windows machine, using pthread.dll . 回答1: To enable extension in Azure Web