Is it possible to determine the thread holding a mutex?

You can use knowledge of the mutex internals to do this. Ordinarily this wouldn’t be a very good idea, but it’s fine for debugging. Under Linux with the NPTL implementation of pthreads (which is any modern glibc), you can examine the __data.__owner member of the pthread_mutex_t structure to find out the thread that currently has … Read more

pthread: one printf statement get printed twice in child thread

Threading generally occurs by time-division multiplexing. It is generally in-efficient for the processor to switch evenly between two threads, as this requires more effort and higher context switching. Typically what you’ll find is a thread will execute several times before switching (as is the case with examples 3 and 4. The child thread executes more … Read more

POSIX API call to list all the pthreads running in a process

There doesn’t seem to be any portable way to enumerate the threads in a process. Linux has pthread_kill_other_threads_np, which looks like a leftover from the original purely-userland pthreads implementation that may or may not work as documented today. It doesn’t tell you how many threads there were. You can get a lot of information about … Read more

POSIX threads and signals

What’s the best way to control which thread a signal is delivered to? As @zoli2k indicated, explicitly nominating a single thread to handle all signals you want handled (or a set of threads each with specific signal responsibilities), is a good technique. What is the best way to tell another thread (that might actually be … Read more

How to set the name of a thread in Linux pthreads?

As of glibc v2.12, you can use pthread_setname_np and pthread_getname_np to set/get the thread name. These interfaces are available on a few other POSIX systems (BSD, QNX, Mac) in various slightly different forms. Setting the name will be something like this: #include <pthread.h> // or maybe <pthread_np.h> for some OSes // Linux int pthread_setname_np(pthread_t thread, … Read more

Kill Thread in Pthread Library

First store the thread id pthread_create(&thr, …) then later call pthread_cancel(thr) However, this not a recommended programming practice! It’s better to use an inter-thread communication mechanism like semaphores or messages to communicate to the thread that it should stop execution. Note that pthread_kill(…) does not actually terminate the receiving thread, but instead delivers a signal … Read more

How to return a value from pthread threads in C?

Here is a correct solution. In this case tdata is allocated in the main thread, and there is a space for the thread to place its result. #include <pthread.h> #include <stdio.h> typedef struct thread_data { int a; int b; int result; } thread_data; void *myThread(void *arg) { thread_data *tdata=(thread_data *)arg; int a=tdata->a; int b=tdata->b; int … Read more

when g++ static link pthread, cause Segmentation fault, why?

First, the solution. This here will work: Update: Since Ubuntu 18.04, you need to link also against librt (add -lrt): g++ -o one one.cpp -Wall -std=c++11 -O3 -static -lrt -pthread \ -Wl,–whole-archive -lpthread -Wl,–no-whole-archive (continue with original answer) g++ -o one one.cpp -Wall -std=c++11 -O3 -static -pthread \ -Wl,–whole-archive -lpthread -Wl,–no-whole-archive When you use -pthread, … Read more

techhipbettruvabetnorabahisbahis forumu