pthreads

Using pthread condition variable with rwlock

家住魔仙堡 提交于 2020-01-22 09:38:08
问题 I'm looking for a way to use pthread rwlock structure with conditions routines in C++. I have two questions: First: How is it possible and if we can't, why ? Second: Why current POSIX pthread have not implemented this behaviour ? To understand my purpose, I explain what will be my use: I've a producer-consumer model dealing with one shared array. The consumer will cond_wait when the array is empty, but rdlock when reading some elems. The producer will wrlock when adding(+signal) or removing

C++ pthread blocking queue deadlock (I think)

折月煮酒 提交于 2020-01-20 02:54:08
问题 I am having a problem with pthreads where i think i am getting a deadlock. I have created a blocking queue which I thought was working, but after doing some more testing I have found that if i try and cancel multiple threads that are blocking on the blocking_queue, i seem to get a deadlock. The blocking queue is very simple and looks like this: template <class T> class Blocking_Queue { public: Blocking_Queue() { pthread_mutex_init(&_lock, NULL); pthread_cond_init(&_cond, NULL); } ~Blocking

OpenSSL and multi-threads

倾然丶 夕夏残阳落幕 提交于 2020-01-19 03:55:05
问题 I've been reading about the requirement that if OpenSSL is used in a multi-threaded application, you have to register a thread identification function (and also a mutex creation function) with OpenSSL. On Linux, according to the example provided by OpenSSL, a thread is normally identified by registering a function like this: static unsigned long id_function(void){ return (unsigned long)pthread_self(); } pthread_self() returns a pthread_t, and this works on Linux since pthread_t is just a

Issue with Lock Ordering or Scheduling

旧街凉风 提交于 2020-01-16 08:59:19
问题 I have a C application that uses pthreads. There is a lock contention between two threads(say A and B) where A gets the lock first while B is waiting for the lock, Once A is done and releases the lock, B still doesn't get it and after a while A gets the lock again(A does acquire and release in a loop). If I attach my process to gdb and pause thread A after it has given up the lock and manually continue on thread B, it then gets it and does what is needed. This does not look like a dead lock

shared memory mutex with struct pointers

强颜欢笑 提交于 2020-01-16 08:43:33
问题 Im looking for some feedback on if I am doing the following properly. Im working on porting some windows real time code that had heavy use of named mutex's. It took some searching but i came across some stuff saying you can use shared memory as mutexes in linux, using shm open. I cant include all of the code here, but i put together the key areas that i need some feed back on. My questions are if im setting up the shared memory region, and mutex correctly, and also if my pointers will be set

shared memory mutex with struct pointers

社会主义新天地 提交于 2020-01-16 08:43:08
问题 Im looking for some feedback on if I am doing the following properly. Im working on porting some windows real time code that had heavy use of named mutex's. It took some searching but i came across some stuff saying you can use shared memory as mutexes in linux, using shm open. I cant include all of the code here, but i put together the key areas that i need some feed back on. My questions are if im setting up the shared memory region, and mutex correctly, and also if my pointers will be set

What if a being-waited thread detaches itself?

拟墨画扇 提交于 2020-01-15 06:57:26
问题 #include <pthread.h> void thread_routine(void*) { sleep(5); pthread_detach(pthread_self()); sleep(5); } int main() { pthread_t t; pthread_create(&t, 0, thread_routine, 0); pthread_join(t); } Will pthread_join(t); return immediately after pthread_detach(pthread_self()); succeed? 回答1: The behavior is undefined, and thus obviously to be avoided at all costs. (As far as I can tell the behavior is implicitly undefined. There are several kindred instances of explicitly undefined behavior in the

Automatically release mutex on crashes in Unix

末鹿安然 提交于 2020-01-15 04:25:07
问题 I have two programs interacting via a shared memory segment. When using the segment for read or write, they hold a lock. Should either of them crash (get killed, typically - possibly with something untrappable) in a critical section, I would like them to release the lock so that the shmem isn't locked out completely. Other questions point the way to the answer in Windows, Java, etc. but how do you do it in Unix (and specifically Linux)? (I'm not attached to the pthreads mutex functions; SysV

Automatically release mutex on crashes in Unix

a 夏天 提交于 2020-01-15 04:24:26
问题 I have two programs interacting via a shared memory segment. When using the segment for read or write, they hold a lock. Should either of them crash (get killed, typically - possibly with something untrappable) in a critical section, I would like them to release the lock so that the shmem isn't locked out completely. Other questions point the way to the answer in Windows, Java, etc. but how do you do it in Unix (and specifically Linux)? (I'm not attached to the pthreads mutex functions; SysV

Why do you need '-lpthread'?

回眸只為那壹抹淺笑 提交于 2020-01-14 10:38:48
问题 So my questions is: Why do you need '-lpthread' at the end of a compiling command? Why does this command work: gcc -o name name.c -lpthread but this won't: gcc -o name name.c I am using the pthread.h library in my c code. I already looked online for some answers but didn't really find anything that answered it understandably 回答1: pthread.h is not a library it is just a header file which gives you declaration (not the actual body of function) of functions which you will be using for multi