pthreads

thread_cancel and blocking function as cond_wait

帅比萌擦擦* 提交于 2020-01-06 14:13:39
问题 I have my main process send pthread_cancel to another thread which is waiting for a condition to happen with cond_wait(&condition) . On the pthread_cancel they are saying : Deferred cancel ability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. But often those function are blocking function. Then my question is the thread cancelled only after that thread has been unblock (in my example by a broadcast or a signal) or it would see

My recursive mutex vs pthread_mutex_t (type: recursive) (repost, push)

≯℡__Kan透↙ 提交于 2020-01-06 08:21:21
问题 I was wondering if I could make a recursive mutex type on my own with a PTHREAD_MUTEX_ERRORCHECK mutex, this is the result: typedef struct { pthread_mutex_t mutex; uint32_t deadlocks; } pthread_recursivemutex_t; int pthread_recursivemutex_init(pthread_recursivemutex_t *mutex) { int ret; pthread_mutexattr_t attr; mutex->deadlocks = 0; ret = pthread_mutexattr_init(&attr); if (ret != 0) { return ret; } (void)pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); ret = pthread_mutex_init(

Reader/Writer implementation in C

浪子不回头ぞ 提交于 2020-01-06 07:53:27
问题 I'm currently learning about concurrency at my University. In this context I have to implement the reader/writer problem in C, and I think I'm on the right track. My thought on the problem is, that we need two locks rd_lock and wr_lock . When a writer thread wants to change our global variable, it tries to grab both locks, writes to the global and unlocks. When a reader wants to read the global, it checks if wr_lock is currently locked, and then reads the value, however one of the reader

Reader/Writer implementation in C

試著忘記壹切 提交于 2020-01-06 07:53:12
问题 I'm currently learning about concurrency at my University. In this context I have to implement the reader/writer problem in C, and I think I'm on the right track. My thought on the problem is, that we need two locks rd_lock and wr_lock . When a writer thread wants to change our global variable, it tries to grab both locks, writes to the global and unlocks. When a reader wants to read the global, it checks if wr_lock is currently locked, and then reads the value, however one of the reader

Do we need mutex to perform multithreading file IO

时光怂恿深爱的人放手 提交于 2020-01-06 07:27:12
问题 I'm trying to do random write (Benchmark test) to a file using multiple threads (pthread). Looks like if I comment out mutex lock the created file size is less than actual as if Some writes are getting lost (always in some multiple of chunk size). But if I keep the mutex it's always exact size. Is my code have a problem in other place and mutex is not really required (as suggested by @evan ) or mutex is necessary here void *DiskWorker(void *threadarg) { FILE *theFile = fopen(fileToWrite, "a+"

pthread kill after a certain time duration

跟風遠走 提交于 2020-01-05 19:28:10
问题 I'm working with VS2005 C++ and I'm BRAND NEW to it. I have a loop that creates several threads using the following statement - rc = pthread_create(&thread[i], NULL, &Function, (void *)threadID); I want to terminate all threads after a certain duration (say 5 minutes). How do I have a timer that kills all threads after this duration? 回答1: I believe, something like that: pthread_t tid[thread_num] = {}; for(size_t i = 0 ; i < threads_num; ++i){ pthread_create(&tid[i], NULL, func, arg); } sleep

C++ threads stack address range

﹥>﹥吖頭↗ 提交于 2020-01-05 12:15:37
问题 Does the C++ standard provide a guarantee about the non-overlapping nature of thread stacks (as in started by an std::thread )? In particular is there a guarantee that threads will have have their own, exclusive, allocated range in the process's address space for the thread stack? Where is this described in the standard? For example std::uintptr_t foo() { auto integer = int{0}; return std::bit_cast<std::uintptr_t>(&integer); ... } void bar(std::uint64_t id, std::atomic<std::uint64_t>& atomic)

threads have the same id

两盒软妹~` 提交于 2020-01-05 08:09:28
问题 I learn threads. I have read that thread terminates after it is out of a function (that is passed as parameter to pthread_create function). So I create threads in the loop, they are executed and afterwards they are terminated. (sorry for some long code) But when I call a function pthread_create, new threads get the same ids. Why? struct data { FILE *f; }; void *read_line_of_file(void *gdata) { pthread_mutex_lock(&g_count_mutex); // only one thread can work with file, //doing so we block other

threads have the same id

北战南征 提交于 2020-01-05 08:09:23
问题 I learn threads. I have read that thread terminates after it is out of a function (that is passed as parameter to pthread_create function). So I create threads in the loop, they are executed and afterwards they are terminated. (sorry for some long code) But when I call a function pthread_create, new threads get the same ids. Why? struct data { FILE *f; }; void *read_line_of_file(void *gdata) { pthread_mutex_lock(&g_count_mutex); // only one thread can work with file, //doing so we block other

concurrent readers and mutually excluding writers in C using pthreads

自闭症网瘾萝莉.ら 提交于 2020-01-05 07:59:49
问题 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