Questions tagged [pthreads]

Pthreads (POSIX Threads) is a standardised C-based API for creating and manipulating threads. It is currently defined by POSIX.1-2008 (IEEE Std 1003.1, 2013 Edition / The Open Group Base Specifications Issue 7).

pthreads
Filter by
Sorted by
Tagged with
-3 votes
1 answer
96 views

send/receive thread seems blocked when access serial port

I wrote a UART read/write program in C, here is the demo test code, typedef struct devpara { int fd; int num } devpara_t; int g_step = 1; // init step int main() { int ret = 0; devpara_t ...
2 votes
1 answer
1k views

Using mutexes in signal handers

I have a multithreaded application and need to stop it gracefully on interrupt, termination, etc. signals. Here goes a snippet to illustrate the relevant parts of the logic. std::atomic_bool running ...
0 votes
0 answers
23 views

Creating + joining multiple pthreads in a loop (C) [duplicate]

I am using a loop to create threads, where the user specifies the number of threads through an argument. The general code is as follows: void* thread_fn(void* arg) { int num = *(int*)arg; ...
0 votes
0 answers
100 views

I'm new to pthreads and using them alongside a mutex. Can anyone point me in the right direction on how to use them?

EDIT(because people are asking some questions that i expected to be asked because no assignment specifics were given). I HAVE to use pthreads and the bridge mutex for this assignment, none of the ...
0 votes
0 answers
101 views

how to quit a blocking read thread when timeout happen?

Here I create a thread to read data from uart in linux, but sometimes, there is no data from uart, so that thread is blocking for a very long time. The question is how to set a timeout branch to let ...
0 votes
0 answers
76 views

How qnx schedule threads

The applications are using pthread lib, in case process A has 100 threads ready, after that process B has one thread ready to run, will scheduler 1) pick and execute thread(s) from process A maxmumlly ...
0 votes
1 answer
1k views

enabling pthread on vscode using code runner extension on linux wsl?

I have encountered an error on Linux while using Posix threads with Code runner on VS Code- an error of undefined reference to pthread_create();
0 votes
1 answer
78 views

How can pthread_cond_wait() be decoupled with mutex?

I've been thinking about the logic behind condition variable for a while, and getting comfortable with most common questions associated with it. Basically, if We do something like: mutex.lock() while(!...
0 votes
0 answers
27 views

What will happen to other detached thread if main routine terminate in C/C++?

I am wondering if the program exit after main routine terminate while other threads are running. I want to find a way to keep the program running til other threads terminate. I try detach the threads ...
2 votes
1 answer
126 views

Stop all c++ threads

I have a thread that is calling std::getline(std::cin,input) and another thread that wakes up every x mins and checks a status. If the status is True my whole c++ application needs to terminate/...
0 votes
1 answer
51 views

Why do my passengers thread behave the way they do?

I'm writing a multithread program in C using pthreads. I have 4 bus stops threads. At the start of the program 10 passengers(every one is a different thread) come to bus stops(random ones). They also ...
0 votes
2 answers
1k views

What is the purpose of pthread and the select () function?

Our professor gave us this code in class: st = select(max+1, &rs, NULL, NULL, &timeinterval); if(st){ for(int i=0; i<workers; i++) { if(FD_ISSET(channels[...
0 votes
1 answer
50 views

C - Multiprocessing Threads Implementation - Pointers problem

i just need to write a code to calculate a local max of an half of an array.. but there's some problem with ponters (?) i see segmentation fault problem #include <stdio.h> #include <pthread.h&...
0 votes
2 answers
201 views

Can VS code compile and run a C program in a VirtualBox VM?

The problem : I have to make program which uses pthreads. To do that , I have an Ubuntu VM on VirtualBox so someone would say im all set. But I was wondering if I can use POSIX libraries while still ...
61 votes
7 answers
130k views

How do you query a pthread to see if it is still running?

In my destructor I want to destroy a thread cleanly. My goal is to wait for a thread to finish executing and THEN destroy the thread. The only thing I found about querying the state of a pthread is ...
0 votes
2 answers
114 views

Why do i get a segmentation fault because of my mutex_lock?

I am making a program that solves the dining philosophers problem and i get a segmentation fault when i try to lock my mutex. I am not allowed to use global variables so i have to kinda move my ...
71 votes
12 answers
74k views

How much overhead is there when creating a thread?

I just reviewed some really terrible code - code that sends messages on a serial port by creating a new thread to package and assemble the message in a new thread for every single message sent. Yes, ...
2 votes
2 answers
79 views

C: Indexing pthreads with numbers 0 through n -1

I've got a set of n threads that need to be numbered 0 to n - 1. This does not work right. #include <pthread.h> #include <stdio.h> #define MY_THREAD_COUNT 6 void *runner(void *param) { ...
0 votes
2 answers
71 views

C - Integrating pthread_join and pthread_create in for loop, thread_returnValue pointers problem

im new to multi-thread programming in C. I implemented a thread_create.c file for two thread with a race of chars in linux. But if i wanna do it with a #define n for generical multi-thread file there'...
4 votes
3 answers
3k views

Why does one CTRL-Z when running in gdb keep giving SIGTSTP signal each time I use 'cont' command?

I'm working on an project on Ubuntu Linux, when I debug the application using GDB and break by CTRL + Z, I got SIGTSTP and the GDB interrupt as expected. But when I using cont after that, I still ...
0 votes
1 answer
486 views

Joining multiple threads to one thread in C

Basically the main thread creates threads 1 2 3 4. And 1 2 3 have to wait for 4 to end and main waits for threads 1 2 3. The code is as follows: #include<stdio.h> #include<pthread.h> #...
2 votes
5 answers
812 views

Why does MinGW-w64 floating-point precision depend on winpthreads version?

I use the MinGW-w64 g++ compilers 10.2 and 10.3. I have built both myself using https://github.com/niXman/mingw-builds. There is an oddity with g++ on Windows: the main thread of an application will ...
2 votes
1 answer
2k views

Why does pthread_join() not create a deadlock?

Below is the program for which I am expecting the program to go into a deadlock because pthread_join() is a blocking wait on a thread (it is waiting to terminate). But I see that pthread_join() does ...
1 vote
1 answer
72 views

why pthread_join didn't block its calling thread [duplicate]

I am learning how to use <pthread.h>, and the textbook says that: The pthread_join function blocks the calling thread ... But in my test, it didn't: void *a_thread(void *pt) { puts("...
7 votes
5 answers
6k views

Linux: detect at runtime that a process have multiple threads

I'm asking about linux with recent glibc. Is there a way to detect that process consist of 1 thread or of several threads? Threads can be created by pthread, or bare clone(), so I need something ...
8 votes
2 answers
17k views

pthread_create with no arguments?

I want to create a thread with no function arguments but I keep getting errors that are seriously bugging me because I cant get something super simple to work right Heres my code: #include<stdio....
0 votes
0 answers
82 views

Segmentation fault at random times when using pthreads, what causes this? [duplicate]

I am working with pthreads in c++ doing the sleeping barber problem. Was just trying to initialize threads and test it out but am getting a segmentation fault at a random point in execution. Sometimes ...
1 vote
1 answer
139 views

I am getting different errors dependent on the compiler I am using. However, I used valgrind and I was given a __pthread_tpp_change_priority error

I am attempting to learn how to create a multi-threaded program for a school project. I am having issues trying to debug my project. It might look like a hot pile of garbage, so I apologize in advance....
0 votes
2 answers
62 views

Error: Seg. Fault. Want to copy multiple files

I wanted to copy multiple files using multi-threading. This ends in error: segmentation fault. Cannot figure out where. What I tried: print("test"); on different lines, but gives me the same ...
0 votes
1 answer
48 views

Threads pool, how to properly terminate all threads when there is no more tasks needed to be done?

So far, I set a global variable taskCompleted to track how many task has been completed; the executeTask() terminates when this reaches to certain number. But the program seems still hang after it ...
2 votes
1 answer
902 views

Segmentation fault in accept system call

I have the following code serving as main loop for a server that accepts incoming socket connections. At the moment the macro OperationMode is defined as 1 so it will execute the pthread logic. for (...
1 vote
0 answers
111 views

GMP library usage cause memory exhaustion

I am using GMP library for large number of calculation. I have checked through valgrind that there is no memory leaks in program for small numbers of calculations. But when I try to calculate large ...
2 votes
1 answer
50 views

Why is the second function not running and or producing an output? (C in Linux)

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> //needed for Mac Linux pthread_mutex_t mutex = ...
2 votes
1 answer
131 views

What is a safe way to interrupt a process created by fork()/system() from a different thread than the one which called fork()/system()?

Context: I am working on a system intended to update a device running linux over ethernet. The device hosts an update server, and when a client connects, the client sends an update package which is ...
0 votes
1 answer
43 views

Multithreaded program calculating sum speed-issues

recently I have writen a programming about Multithreading (calculating sum from 1 to n) and compare the speed of Multithreaded program and normal one. However, the multithreaded program run much more ...
3 votes
1 answer
50 views

Cpp threads is not working as expected when loops are involved

C++ sleep_for method is not working as expected. I'm trying to write a program that prints from 1 to 10 but with some time gap between each print. 1 (after 1 sec) 2 (after 1 sec) 3 . . . and so on. ...
4 votes
1 answer
385 views

Spawning a pthread has drastic change to core dump behavior

Below is a small program, which uses mmap to "allocate" 20GB of virtual address space, and then segfaults. There are two behaviors that can be controlled independently (via cmd line arguments): the ...
0 votes
1 answer
33 views

The code crashes on line 45 "pthread_cond_signal"

#include<pthread.h> #include<iostream> #include<windows.h> #define THREAD_NUM 2 using namespace std; typedef struct { pthread_cond_t cond[2]; pthread_mutex_t mutex[2]; ...
2 votes
1 answer
50 views

Multithreaded program calculating sum running faster on laptop than desktop PC

I am practicing multi-threaded programs as part of my Operating Systems course and wrote a very basic program calculating the sum from 1 to n. However, when I ran it on my Intel i5 1035G7 laptop, it ...
1 vote
2 answers
21k views

corrupted size vs. prev_size in C

I get "corrupted size vs prev_size" error whenever I allocate a dynamic memory in a thread. Whenever I allocate the memory in main() it works fine. But allocating dynamic memory in thread produces the ...
0 votes
0 answers
137 views

ctypes won't load .so file that uses pthread

Ctypes is able to load .so files until I use one that has pthread_create() in it's source code. Then it gives the error: """ FileNotFoundError: Could not find module '\...
0 votes
1 answer
46 views

Getting logical thread ID number

I would like to split a simple 1D array into chunks where each thread operates on a chunk. The code looks like void* thread_func(void* arg) { unsigned start_index = *((int*) arg); unsigned ...
0 votes
1 answer
45 views

Getting thread ID in numeric form

I would like to get the thread ID in a numeric form with the following code: #include <string.h> ... pthread_t ptid = pthread_self(); unsigned long long threadId = 0; memcpy(&threadId, &...
0 votes
0 answers
161 views

pthread and array, how to rewrite same position?

I am having problems with an array[MAX] of p_thread. When my array is full, I return on the first position and make a pthread_join(array[0], NULL). Later, when I try to have a pthread_create(array[0] ...
1 vote
1 answer
286 views

-pedantic warning and pthread_create

I'm newbie in the ansi and iso world, i compiled my program with : -asni -pedantic -std=c++11 -std=c++98 Now, i get the following warning: warning: converting from 'void (NetworkSocket::*)()' to '...
2 votes
1 answer
110 views

Does the thread supported by the kernel run in the kernel state?

I'm a little confused about kernel space, userspace, kernel thread, user thread. Whether you can think that kernel threads are supported by the kernel is running in kernel space? Java, `Thread t = ...
1 vote
1 answer
38 views

Extra characters appearing when pthread_create is called with a struct as an argument to the thread

I observed that when I pass a struct as an argument to the thread, the string member of the struct is few characters longer inside the thread. So basically, in main, s-dev is 12 characters long. And ...
0 votes
3 answers
93 views

In C, is storing data in local variables in threads akin to creating a local copy? AKA Does this Threadpool synchronization make sense?

As I explain in this question, I am having some issues with my Threadpool implementation. Since the previous question is a bit too broad, I thought I'd also ask a very specific question. The way I ...
0 votes
2 answers
91 views

Long to void* casting in pThreads

From the code shown below: I don't understand how the pthread_create function can cast thread which is of type long to type void*. I know that it is the format of the function however, long is an ...
0 votes
1 answer
154 views

Weird error of Python C Extension using Pthreads that RARELY happens

I am writing a Python C Extension for Graph-related computations. One routine computes the edge list of a certain class of Graphs and since this is "embarassingly" parallelizable, I rewrote ...

1
4 5
6
7 8
179