EzDevInfo.com

pthreads interview questions

Top pthreads frequently asked interview questions

Does guarding a variable with a pthread mutex guarantee it's also not cached?

Consider a simple (global in my case) variable:

int i;

Somewhere this variable is accessed

pthread_mutex_lock(i_mutex);
if(i == other value) {
  do_something();
}
pthread_mutex_unlock(i_mutex);

Another thread updates i while it holds i_mutex . Could the compiler cache the value of i so I don't get the recent value ? Must i be volatile ?


Source: (StackOverflow)

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 pthread_attr_setdetachstate but this only tells you if your thread is:

  • PTHREAD_CREATE_DETACHED
  • PTHREAD_CREATE_JOINABLE

Both of those have nothing to do with whether the thread is still running or not.

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


Source: (StackOverflow)

Advertisements

sem_init on OS X

I am working on some code which uses the pthread and semaphore libraries. The sem_init function works fine on my ubuntu machine, but on OS X the sem_init function has absolutely no effect. Is there something wrong with the library or is there a different way of doing it? This is the code I am using to test.

sem_t sem1;
sem_t sem2;
sem_t sem3;
sem_t sem4;
sem_t sem5;
sem_t sem6;

sem_init(&sem1, 1, 1);
sem_init(&sem2, 1, 2);
sem_init(&sem3, 1, 3);
sem_init(&sem4, 1, 4);
sem_init(&sem5, 1, 5);
sem_init(&sem6, 1, 6);

The values appear to be random numbers, and they do not change after the sem_init call.


Source: (StackOverflow)

Detached vs. Joinable POSIX threads

I've been using the pthread library for creating & joining threads in C.

  1. When should I create a thread as detached, right from the outset? Does it offer any performance advantage vs. a joinable thread?

  2. Is it legal to not do a pthread_join() on a joinable (by default) thread? Or should such a thread always use the detach() function before pthread_exit()ing?


Source: (StackOverflow)

Parallelization: pthreads or OpenMP?

Most people in scientific computing use OpenMP as a quasi-standard when it comes to shared memory parallelization.

Is there any reason (other than readability) to use OpenMP over pthreads? The latter seems more basic and I suspect it could be faster and easier to optimize.


Source: (StackOverflow)

PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)

Is there any difference between

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Or

pthread_mutex_t lock;
pthread_mutex_init ( &lock, NULL);

Am I safe enough if I use only the first method ?

NOTE: My question mostly refers to very small programs where at the most what I'll do is connect several clients to a server and resolve their inquiries with worker threads.


Source: (StackOverflow)

Difference between -pthread and -lpthread while compiling

What is the difference between gcc -pthread and gcc -lpthread which is used while compiling multithreaded programs?


Source: (StackOverflow)

Is it possible to determine the thread holding a mutex?

Firstly, I use pthread library to write multithreading c program. Threads always hung by their waited mutexs. When I use the strace utility to find a thread is in FUTEX_WAIT status, I want to know which thread hold that mutex at the time. But I don't know how could I make it. Are there any utilities could do that? Someone told me java virtual machine support this, So I want to know whether linux support this feature.


Source: (StackOverflow)

Can I get C's pthread.h to compile in Windows?

If I try to compile a program with

#include <pthread.h>

in it, I get the error: pthread.h: No such file or directory

Is it possible to get this to compile in a Windows environment?

I am using Vista with the latest mingw

I do not want to use the Microsoft Windows Services for UNIX Version 3.5 as I will have to move this to a Unix environment.


Source: (StackOverflow)

Pthread and wait conditions

I'm learning pthread and wait conditions. As far as I can tell a typical waiting thread is like this:

pthread_mutex_lock(&m);
while(!condition)
     pthread_cond_wait(&cond, &m);
// Thread stuff here
pthread_mutex_unlock(&m);

What I can't understand is why the line while(!condition) is necessary even if I use pthread_cond_signal() to wake up the thread.

I can understand that if I use pthread_cond_broadcast() I need to test condition, because I wake up all waiting threads and one of them can make the condition false again before unlocking the mutex (and thus transferring execution to another waked up thread which should not execute at that point). But if I use pthread_cond_signal() I wake up just one thread so the condition must be true. So the code could look like this:

pthread_mutex_lock(&m);
pthread_cond_wait(&cond, &m);
// Thread stuff here
pthread_mutex_unlock(&m);

I read something about spurious signals that may happen. Is this (and only this) the reason? Why should I have spurious singnals? Or there is something else I don't get?

I assume the signal code is like this:

pthread_mutex_lock(&m);
condition = true;
pthread_cond_signal(&cond); // Should wake up *one* thread
pthread_mutex_unlock(&m);

Source: (StackOverflow)

How to increase thread priority in pthreads?

I am using pthread in Linux. I would like to increase the thread priority by setting the parameters sched_param.priority. However, I could not find much info from the net regarding the range of the thread priority I could set, or about the description of the thread priority.

Also, I would like to know about the relative thread priority as I would not want to set the thread priority to be too high and resulting the OS to halt. Could someone help me with this?


Source: (StackOverflow)

gcc: Do I need -D_REENTRANT with pthreads?

On Linux (kernel 2.6.5) our build system calls gcc with -D_REENTRANT.

Is this still required when using pthreads?

How is it related to gcc -pthread option? I understand that I should use -pthread with pthreads, do I still need -D_REENTRANT?

On a side note, is there any difference that you know off between the usage of REENTRANT between gcc 3.3.3 and gcc 4.x.x ?

When I use -pthread gcc option I can see that _REENTRANT gets defined. Will omitting -D_REENTRANT from command line make any difference, for example could some objects be compiled without multithreaded support and then linked into binary that uses pthreads and will cause problems?

I assume it should be ok just to use: g++ -pthread

> echo | g++          -E -dM -c - > singlethreaded
> echo | g++ -pthread -E -dM -c - > multithreaded
> diff singlethreaded multithreaded
39a40
> #define _REENTRANT 1

We're compiling multiple static libraries and applications that link with the static libraries, both libraries and application use pthreads.

I believe it was required at some stage in the past but want to know if it is still required. Googling hasn't returned any recent information mentioning -D_REENTRANT with pthreads. Could you point me to links or references discussing the use in recent version of kernel/gcc/pthread?

Clarification: At the moment we're using -D_REENTRANT and -lpthread, I assume I can replace them with just g++ -pthread, looking at man gcc it sets the flags for both preprocessor and linker. Any thoughts?


Source: (StackOverflow)

mingw-w64 threads: posix vs win32

I'm installing mingw-w64 on Windows and there are two options: win32 threads and posix threads. I know what is the difference between win32 threads and pthreads but I don't understand what is the difference between these two options. I doubt that if I will choose posix threads it will prevent me from calling WinAPI functions like CreateThread.

It seems that this option specify which threading API will be used by some program or library, but by what? By GCC, libstdc++ or by something else?

I found this: Whats the difference between thread_posixs and thread_win32 in gcc port of windows?

In short, for this version of mingw, the threads-posix release will use the posix API and allow the use of std::thread, and the threads-win32 will use the win32 API, and disable the std::thread part of the standard.

Ok, if I will select win32 threads then std::thread will be unavailable but win32 threads will still be used. But used by what?


Source: (StackOverflow)

How to print pthread_t

Searched, but don't come across a satisfying answer.

I know there's no a portable way to print a pthread_t.

How do you do it in your app?

Update:

Actually I don't need pthread_t, but some small numeric id, identifying in debug message different threads.

On my system (64 bit RHEL 5.3) it's defined as unsigned long int, so it's big number and just printing it eats a valuable place in debug line. How does gdb assign short tids?


Source: (StackOverflow)

Multiple arguments to function called by pthread_create()?

I need to pass multiple arguments to a function that I would like to call on a separate thread. I've read that the typical way to do this is to define a struct, pass the function a pointer to that, and dereference it for the arguments. However, I am unable to get this to work:

#include <stdio.h>
#include <pthread.h>

struct arg_struct {
    int arg1;
    int arg2;
};

void *print_the_arguments(void *arguments)
{
    struct arg_struct *args = (struct arg_struct *)args;
    printf("%d\n", args -> arg1);
    printf("%d\n", args -> arg2);
    pthread_exit(NULL);
    return NULL;
}

int main()
{
    pthread_t some_thread;
    struct arg_struct args;
    args.arg1 = 5;
    args.arg2 = 7;

    if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)&args) != 0) {
    	printf("Uh-oh!\n");
    	return -1;
    }

    return pthread_join(some_thread, NULL); /* Wait until thread is finished */
}

The output for this should be:

5
7

But when I run it I actually get:

141921115
-1947974263

Anyone know what I'm doing wrong?


Source: (StackOverflow)