pthreads

concurrent readers and mutually excluding writers in C using pthreads

岁酱吖の 提交于 2020-01-05 07:59:06
问题 I was hoping if someone could forward me or show me a program that has multiple readers yet mutually excluding writers in C. I searched the entire internet for it, and could not find a single example that displays this behavior using coarse-grained locking. I know I can use pthread_rwlock_init, pthread_rwlock_rdlock, etc, I just don know how to use it. I learn by examples, which is why Im here. Suppose I have a region of code(not a shared variable)and I want multiple reads, yet a single

How to configure CMakeList in Clion ide for using POSIX pthread functions?

二次信任 提交于 2020-01-05 05:28:13
问题 I tried to compile a simple POSIX example in CLIon ide, but it doesn`t know about pthread library, I think... Here is the code: void *func1() { int i; for (i=0;i<10;i++) { printf("Thread 1 is running\n"); sleep(1); } } void *func2() { int i; for (i=0;i<10;i++) { printf("Thread 2 is running\n"); sleep(1); } } int result, status1, status2; pthread_t thread1, thread2; int main() { result = pthread_create(&thread1, NULL, func1, NULL); result = pthread_create(&thread2, NULL, func2, NULL); pthread

Proper use of MPI_THREAD_SERIALIZED with pthreads

笑着哭i 提交于 2020-01-04 09:06:52
问题 After reading some MPI specs I'm lead to understand that, when initializing with MPI_THREAD_SERIALIZED, a program must ensure that MPI_Send/Recv calls that occur in separate threads must not overlap. In other words, you need a mutex to protect MPI calls. Consider this situation: Mutex mpi_lock = MUTEX_INITIALIZER; void thread1_function(){ while(true){ /* things happen */ lock(mpi_lock); MPI_Send(/* some message */); unlock(mpi_lock); /* eventually break out of loop */ } } void thread2

Is there a way to call library thread-local init/cleanup on thread creation/destruction?

牧云@^-^@ 提交于 2020-01-04 07:45:10
问题 This question is similar to How to call a function on a thread's creation and exit? but more specific. In another multi-process shared memory project I used a combination of an __attribute__((constructor)) labeled library init routine, lazy initialisation for each thread, and robust futexes to make sure resources weren't leaked in the shared memory even if a sys admin chose to SIGKILL one of the processes using it. However futexes within the APIs are way too heavyweight for my current project

C++: Warning (Anachronism) in Solaris Studio when creating a pthread from main using a static function within a class

妖精的绣舞 提交于 2020-01-04 07:20:31
问题 I've been facing this warning. I have a main C++ program which creates several threads. These threads run a function which is in a specific class. In order to do that, this class has a static function which returns the real function I am using to run in these threads. I'll paste the relevant parts of my code for you to understand it better. These are the relevant parts of Car.h, the headers of the class whose function is run in the thread: [...] class Car { public: [...] void *GoForward(void)

Reason for ENOMEM failure to create threads?

北战南征 提交于 2020-01-04 06:09:10
问题 I have an application that is using pthread_create() and pthread_detach() in the main thread and later pthread_exit() in the child thread. After around 54 pthread_create() calls that have each been paired with a subsequent pthread_detach() and then pthread_exit() the pthread_create() fails. It is ENOMEM failure "Out of memory". What might cause pthread_exit() to not be freeing up the memory of the old threads and causing my application to leak memory and eventually run out? This is running on

Reason for ENOMEM failure to create threads?

核能气质少年 提交于 2020-01-04 06:08:07
问题 I have an application that is using pthread_create() and pthread_detach() in the main thread and later pthread_exit() in the child thread. After around 54 pthread_create() calls that have each been paired with a subsequent pthread_detach() and then pthread_exit() the pthread_create() fails. It is ENOMEM failure "Out of memory". What might cause pthread_exit() to not be freeing up the memory of the old threads and causing my application to leak memory and eventually run out? This is running on

Posix threads:Signal a thread that is running in while loop

為{幸葍}努か 提交于 2020-01-04 05:26:25
问题 I have a thread that is a very simple: it sends keep-alive packets to a server, then sleeps. I would like my main thread to signal the thread to quit, but whenever I call pthread_kill() it seems to crash my process. I tried to call sigpending() followed by sigismember() at the beginning of my while loop, but I think it is crashing because it doesn't know of any handle to take care of the signal (I was using sigaddset() before the while loop), as follow: void* foo(void* arg) { sigset_t set,

graceful thread termination with pthread_cond_signal proving problematic

半世苍凉 提交于 2020-01-04 05:18:22
问题 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

pthread: destroying global static mutex

旧城冷巷雨未停 提交于 2020-01-04 05:18:09
问题 This code was taken from the 3rd edition of Advanced Programming in the UNIX Environment, written by Richard Stevens. This is an example of how to make a reentrant version of getenv() . It is demostrated here only for a learning purpose. /* Copyright (c) W.R.Stevens */ #include <string.h> #include <errno.h> #include <pthread.h> #include <stdlib.h> extern char **environ; pthread_mutex_t env_mutex; static pthread_once_t init_done = PTHREAD_ONCE_INIT; static void thread_init(void) { pthread