posix

Using pthread condition variable with rwlock

不羁岁月 提交于 2020-01-22 09:44:20
问题 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

Using pthread condition variable with rwlock

你说的曾经没有我的故事 提交于 2020-01-22 09:39:10
问题 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

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

What Can I Use Besides usleep in a Modern POSIX Environment?

自作多情 提交于 2020-01-21 12:03:36
问题 I'm fairly new to C but writing a small multithreaded application. I want to introduce a delay to a thread. I'd been using 'usleep' and the behavior is what I desire - but it generates warnings in C99. implicit declaration of function ‘usleep’ It's only a warning, but it bothers me. I've Googled for an answer but all I could find was a while-loop / timer approach that seemed like it would be CPU intensive. EDIT: My includes are: #include <stdio.h> #include <stdlib.h> #include <pthread.h>

Is reading into uninitialized memory space ALWAYS ill advised?

隐身守侯 提交于 2020-01-17 14:03:13
问题 I am recreating the entire standard C library and I'm working on an implementation for strle n that I would like to be the basis of all my other str functions. My current implementation is as follows: int ft_strlen(char const *str) { int length; length = 0; while(str[length] != '\0' || str[length + 1] == '\0') length++; return length; } My question is that when I pass a str like: char str[6] = "hi!"; As expected, the memory reads: ['h']['i']['!']['\0']['\0']['\0']['\0'] If you look at my

Is reading into uninitialized memory space ALWAYS ill advised?

馋奶兔 提交于 2020-01-17 14:03:10
问题 I am recreating the entire standard C library and I'm working on an implementation for strle n that I would like to be the basis of all my other str functions. My current implementation is as follows: int ft_strlen(char const *str) { int length; length = 0; while(str[length] != '\0' || str[length + 1] == '\0') length++; return length; } My question is that when I pass a str like: char str[6] = "hi!"; As expected, the memory reads: ['h']['i']['!']['\0']['\0']['\0']['\0'] If you look at my

reading from named pipes in linux vs OS X

独自空忆成欢 提交于 2020-01-17 04:44:06
问题 The program below works well under OS X but not in linux. It continues to loop through the perror("read error") line, with no bytes in the read buffer, and EWOULDBLOCK isn't the errno (errno=0); In OS X the program works as expected, which is that it reads from three named pipes, and prints any data from any of them to the console. #include <sys/types.h> #include <sys/select.h> #include <sys/time.h> #include <sys/types.h> #include <errno.h> #include <stdlib.h> #include <stdio.h> #include

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

Why do some POSIX functions get a element number and element size parameters?

眉间皱痕 提交于 2020-01-14 14:26:07
问题 See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html The malloc function simply gets a array size to allocate. But: http://pubs.opengroup.org/onlinepubs/009695399/functions/calloc.html And: http://pubs.opengroup.org/onlinepubs/009695399/functions/fread.html Are just 2 examples of functions that receive element size and number of elements. Why's that difference? Doesn't it more simple to get a size and that's it? 回答1: This is more of a note than a reason, but the values of

How can semaphores implemented on multi-core systems?

谁都会走 提交于 2020-01-14 07:11:29
问题 I can not understand how Posix allows any thread to unlock (post) on a semaphore. Let's consider following example: // sem1 and sem2 are a Posix semaphore, // properly initialized for single process use // at this point, sem2 is locked, sem1 is unlocked // x and y are global (non-atomic, non-volatile) integer variables // thread 1 - is executing right now rc = sem_wait(&sem1); // succeeded, semaphore is 0 now x = 42; y = 142; sem_post(&sem2); while (true); // thread 2. waits for sem2 to be