pthreads

C++ - How to chunk a file for simultaneous/async processing?

∥☆過路亽.° 提交于 2021-02-19 04:41:19
问题 How does one read and split/chunk a file by the number of lines? I would like to partition a file into separate buffers, while ensuring that a line is not split up between two or more buffers. I plan on passing these buffers into their own pthreads so they can perform some type of simultaneous/asynchronous processing. I've read the answer below reading and writing in chunks on linux using c but I don't think it exactly answers the question about making sure that a line is not split up into

自旋锁和互斥锁的区别

旧巷老猫 提交于 2021-02-17 23:52:35
POSIX threads(简称Pthreads)是在多核平台上进行并行编程的一套API。线程同步是并行编程中非常重要的通讯手段,其中最典型的应用就是用 Pthreads提供的锁机制(lock)来对多个线程之间的共享临界区(Critical Section)进行保护(另一种常用的同步机制是barrier)。 Pthreads提供了多种锁机制: Mutex(互斥量):pthread_mutex_t Spin lock(自旋锁): pthread_spin_t Condition Variable(条件变量): pthread_cond_t Read/Write lock(读写锁):pthread_rwlock_t Pthreads提供的Mutex锁操作相关的API主要有: pthread_mutex_lock(pthread_mutex_t *mutex); pthread_mutex_trylock(pthread_mutex_t *mutex); pthread_mutex_unlock(pthread_mutex_t *mutex); Pthreads提供的Spin Lock锁操作相关的API主要有: pthread_spin_lock(pthread_spinlock_t *lock); pthread_spin_trylock(pthread_spinlock_t

thread_fork working on kernel

只谈情不闲聊 提交于 2021-02-17 06:09:02
问题 I am working on OS161 where C pthread library is not primarily supported. My current objective is to understand the sys calls and make some simple programs run. my simple function has following code: int id = 1; long id2 = 1; int ret = thread_fork("myThread", (void *)id, id2, void (*function)((void *)id, id2), NULL); kprintf("\nHello World\n"); return; ` where call to thread_fork is int thread_fork(const char *name, void *data1, unsigned long data2, void (*func)(void *, unsigned long), struct

thread_fork working on kernel

会有一股神秘感。 提交于 2021-02-17 06:07:56
问题 I am working on OS161 where C pthread library is not primarily supported. My current objective is to understand the sys calls and make some simple programs run. my simple function has following code: int id = 1; long id2 = 1; int ret = thread_fork("myThread", (void *)id, id2, void (*function)((void *)id, id2), NULL); kprintf("\nHello World\n"); return; ` where call to thread_fork is int thread_fork(const char *name, void *data1, unsigned long data2, void (*func)(void *, unsigned long), struct

thread_fork working on kernel

a 夏天 提交于 2021-02-17 06:05:13
问题 I am working on OS161 where C pthread library is not primarily supported. My current objective is to understand the sys calls and make some simple programs run. my simple function has following code: int id = 1; long id2 = 1; int ret = thread_fork("myThread", (void *)id, id2, void (*function)((void *)id, id2), NULL); kprintf("\nHello World\n"); return; ` where call to thread_fork is int thread_fork(const char *name, void *data1, unsigned long data2, void (*func)(void *, unsigned long), struct

How to wake the sleeping threads and exit the main thread?

百般思念 提交于 2021-02-10 22:23:14
问题 I am creating 10 threads. Each thread will do some task. There are 7 tasks to be done. Since number of tasks are less than the number of threads, there would always be 3 threads that sleep and do nothing. My main thread must wait for the tasks to get completed and exit only when all the tasks are done (i.e when the thread exits). I am waiting in a for loop and calling pthread_join but with the 3 threads that are sleeping, how can I wake them up and make them exit? Here is what I am doing now.

Does using locals as arguments in pthread_create() work?

自古美人都是妖i 提交于 2021-02-10 20:14:05
问题 This is mainly a question about scope and threads. Let's say we have the following struct. struct Test { int number; std::string name; }; An instance of this struct will be used as an argument in the pthread_create function. Here is an example of what this might look like. pthread_t tid[5]; for(int i = 0; i < 5; ++i) { Test test; test.number = 5; test.name = "test"; pthread_create(&tid[i], NULL, func, (void *)&test); } Is this acceptable? Since test is declared in the for's scope, that means

C: blocking read should return, if filedescriptor is deleted

蹲街弑〆低调 提交于 2021-02-10 05:51:48
问题 I am reading in a blocked way from a device/filedescriptor. It might happen, that in a different thread the device is closed and filedescriptor is deleted. Unfortunatly the read doesn't return or take notice and keeps blocking. As a workaround I could do a while loop with select as a timeout. If a timeout happens, I can check the filedescriptor and in case it is gone not calling read but return. I am wondering, if there is a better way in Linux-C ? 回答1: The code you are describing has an

Implementation of a lock free vector

*爱你&永不变心* 提交于 2021-02-08 12:05:42
问题 After several searches, I cannot find a lock-free vector implementation. There is a document that speaks about it but nothing concrete (in any case I have not found it). http://pirkelbauer.com/papers/opodis06.pdf There are currently 2 threads dealing with arrays, there may be more in a while. One thread that updates different vectors and another thread that accesses the vector to do calculations, etc. Each thread accesses the different array a large number of times per second. I implemented a

How to enable SIGINT signal for system() call in a pthread in C?

我的梦境 提交于 2021-02-08 11:55:25
问题 The below manual on system() says it blocks SIGINT and SIGQUIT signal for any binary program run through system() call. https://man7.org/linux/man-pages/man3/system.3.html#:~:text=The%20system()%20library%20function,the%20command%20has%20been%20completed. Psedo Code: thread_1() { ... system("binary application"); } main() { ... pid = pthread_create(thread_1); pthread_cancel(pid); } pthread_cancel issues SIGINT to thread 1 which kill the thread 1, but not the binary application. How to make