pthreads

Problem with semaphores and sem_wait()

I have a queue structure that is being used by several pthreads. The threads are supposed to dequeue from the queue if it's not empty and then do their business. I initially had this set up as a while loop where the threads checked whether the queue was empty using a mutex_lock. Unfortunately this slowed my program down to a crawl. I t...

CPU Affinity Masks (Putting Threads on different CPUs)

I have 4 threads, and I am trying to set thread 1 to run on CPU 1, thread 2 on CPU 2, etc. However, when I run my code below, the affinity masks are returning the correct values, but when I do a sched_getcpu() on the threads, they all return that they are running on CPU 4. Anybody know what my problem here is? Thanks in advance! #defi...

pthreads_setaffinity_np: Invalid argument?

I've managed to get my pthreads program sort of working. Basically I am trying to manually set the affinity of 4 threads such that thread 1 runs on CPU 1, thread 2 runs on CPU 2, thread 3 runs on CPU 3, and thread 4 runs on CPU 4. After compiling, my code works for a few threads but not others (seems like thread 1 never works) but runni...

pthread and child process data sharing in C

hi everyone, my question is somewhat conceptual, how is parent process' data shared with child process created by a fork() call or with a thread created by pthread_create() for example, are global variables directly passed into child process and if so, does modification on that variable made by child process effect value of it in pare...

POSIX threads and signals

I've been trying to understand the intricacies of how POSIX threads and POSIX signals interact. In particular, I'm interested in: What's the best way to control which thread a signal is delivered to (assuming it isn't fatal in the first place)? What is the best way to tell another thread (that might actually be busy) that the signal ha...

efficient thread-safe singleton in C++

The usual pattern for a singleton class is something like static Foo &getInst() { static Foo *inst = NULL; if(inst == NULL) inst = new Foo(...); return *inst; } However, it's my understanding that this solution is not thread-safe, since 1) Foo's constructor might be called more than once (which may or may not matter) and...

pthread with unique struct as parameter C

I have this piece of code that is giving me trouble. I know all the threads are reading the same struct. But I have no idea how to fix this. #include <pthread.h> #include <stdio.h> #include <stdlib.h> typedef struct { int a,b; } s_param; void * threadfunc(void *parm) { s_param *param2 = parm; printf("ID:%d and v:%d\n",param2->a...

Thread scheduling C

#include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 4 #define TCOUNT 5 #define COUNT_LIMIT 13 int done = 0; int count = 0; int thread_ids[4] = {0,1,2,3}; int thread_runtime[4] = {0,5,4,1}; pthread_mutex_t count_mutex; pthread_cond_t count_threshold_cv; void *inc_count(void *t) { ...

_REENTRANT Flag in pthreads

Hi, which compiling a multithreaded program we use gcc like below: gcc -lpthread -D_REENTRANT -o someprogram someprogram.c what exactly is the flag -D_REENTRANT doing over here? ...

Method of programmatically getting CPU time for thread, in C, that works on both OpenSolaris and Linux

I have a small daemon that I'm writing in C and I need a way to get the current CPU time on a thread. Linux apparently supplies a number of ways to go about doing this, clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...), pthread_getcpuclockid(), getrusage(RUSAGE_THREAD, ...) but none of these seem to be supported under OpenSolaris 2009.06. Is ...

How to use pthread_atfork() and pthread_once() to reinitialize mutexes in child processes

We have a C++ shared library that uses ZeroC's Ice library for RPC and unless we shut down Ice's runtime, we've observed child processes hanging on random mutexes. The Ice runtime starts threads, has many internal mutexes and keeps open file descriptors to servers. Additionally, we have a few of mutexes of our own to protect our intern...

Can I user kernel-level thread in Linux kernel v2.6 and is there any way to know thread-level of my thread?

Can I create kernel-level thread using pthread_create() function with PTHREAD_SCOPE_PROCESS option in Linux kernel 2.6? And how can I know which thread-level is my thread on? Are there any function for that? ...

in socket programming, can accept() the same listening socket in multi-process(thread)?

i.e. open a listening socket in parent process call epoll_wait(listening_socket) in child1,child2,child3.... call accept in each child if there is connection request ...

find out the number of threads created by a short running program

I have program that runs fast enough. I want to see the number of threads created by the program. ldd test shows use of library pthread. but how to find out number of threads created by the program. I only have command line access to the PC on which the program is run. The platform is linux. ...

gdb : multithreading

Hi, I have a program which uses two threads. I have put the break point in both the threads. While running the program under gdb I want to switch between the threads and make them run. (thread t1 is active and running and thread t2; when paused on the breakpoint. I want to stop T1 running and run the T2). Is there any way that I can sc...

Passing parameter to pthread

Hello, i have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100 struct thread_param { char *f1; char *f2; int x; }; void *thread_function(void *arg){ printf("%d\n", ((struct thread_param*)arg)->x); } int main(int argc, char *argvs[]){ int i, thread_cr_res = 0,...

Suggestions for duplicate file finder algorithm (using C)

Hello, I wanted to write a program that test if two files are duplicates (have exactly the same content). First I test if the files have the same sizes, and if they have i start to compare their contents. My first idea, was to "split" the files into fixed size blocks, then start a thread for every block, fseek to startup character of ev...

nptl SIGCONT and thread scheduling

Hello, I'm trying to port a code that relies on SIGCONT to stop certain threads of an application. With current linux nptl implementation seems one can't rely on that in 2.6.x kernels. I'm trying to devise a method to stop other threads. Currently I can only think on mutexes and condition variables. Any hints is appreciated. ...

Creating an independent draw thread using pthreads (C++)

Hi all, I'm working on a graphical application which looks something like this: while (Simulator.simulating) { Simulator.update(); InputManager.processInput(); VideoManager.draw(); } I do this several times a second, and in the vast majority of cases my computation will be taking up 90 - 99% of my processing time. What I w...

using pthread in c++

I am using pthread.h in a *.cc file. when I try to use pthread_exit(0); or pthread_join(mythrds[yy],NULL); it says : .cc:(.text+0x3e): undefined reference to `pthread_exit' when complied very similar code in a *.c file with gcc it work perfect. How Can I use pthread's in c++.. (I also added -lpthread) .. void *myThreads ( void *ptr ) ...