pthreads

Some questions about pthread_mutex_lock and pthread_mutex_unlock

When a thread has acquired the lock and execute the following code, Could the thread will unlock the lock it has acquired just with the return statement? some code like this. static pthread_mutex_t mutex; int foo() { pthread_mutex_lock(mutex); ......... execute some code here and some errors happen ...

C: pthread performance woes. How can I make this code perform as expected?

I have created this little program to calculate pi using probability and ratios. In order to make it run faster I decided to give multithreading with pthreads a shot. Unfortunately, even after doing much searching around I was unable to solve the problem I have in that when I run the threadFunc function, with one thread, whether that be ...

How does POSIX Threads work in linux?

I thought pthread uses clone to spawn one new thread in linux. But if so, all of the threads should have their seperate pid. Otherwise, if they have the same pid, the global variables in the libc seem to be shared. However, as I ran the following program, I got the same pid but the different address of errno. extern errno; void* f(void...

why ostringstream could not work well in multithreading environment

Maybe something is weird. When I use STL ostringstream class in my multithreading environment I found that the execution time of each thread increased linearly as the thread number increased. I don't know why this happened. I try to check the ostringstream source code but could not find any synchronization code. Are there some synchroniz...

get all the thread_id created with pthread_created within an process

Hi , If there is any "intelligent" way to get all the threadIDs created using pthread_created within an process, supposing those threads are created in the third party library that does not expose those data. ...

Hook for pthread_create

Hi Is there (in glibc-2.5 and newer) a way to define a hook for pthread_create? There is a lot of binary applications and I want to write a dynamic lib to be loaded via LD_PRELOAD I can add hook on entry to main (''attributte constructor''), but how can I force my code to be executed in every thread just before the thread's function w...

pthreads mutex number and performance

how many pthreads mutex are usually available in a typical system? Is having many pthreads mutex degrades performance? ...

concurrent queue in C++

I am trying to design a queue which could be simultaneously accessed by multiple read/write threads. I prefer using 2 mutexes, one apiece for read and write. Doing write is simple enough, lock the write mutex, append data, unlock and you are done. The issue is with read. If there's no data in the in queue I'd like my thread to wait til...

Problems initializing pthreads

Hi, I'm debugging a program using pthreads for a class I have to solve a producer consumer problem. What I'm running into is that I get an error saying that I have a undefined reference to the create a join functions. After that I put the initialization functions in but now I get the error that none of them were declared within the scop...

Sleep performance

Hello, I am developing a program in c++ and I have to implement a cron. This cron should be executed every hour and every 24 hours for different reasons. My first idea was to make an independent pthread and sleep it during 1h every time. Is this correct? I mean, is really efficient to have a thread asleep more than awake? What are the in...

Detached vs. Joinable POSIX threads

I've been using the pthread library for creating & joining threads in C. When should I create a thread as detached, right from the outset? Does it offer any performance advantage vs. a joinable thread? 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 befo...

pthreads - previously created thread uses new value (updated after thread creation)

So here's my scenario. First, I have a structure - struct interval { double lower; double higher; } Now my thread function - void* thread_function(void* i) { interval* in = (interval*)i; double a = in->lower; cout << a; pthread_exit(NULL) } In main, let's say I create these 2 threads - pthread_t one...

Stopping individual threads

Using the pthread library in C, is it possible to send a SIGSTOP signal to an individual thread? I want to ensure that even though I create N threads in a loop, all should start executing only when all of them have been created. I ask because the man page for pthread_kill() mentions: Signal dispositions are process-wide: if ...

Barriers for thread syncing

I'm creating n threads & then starting then execution after a barrier breakdown. In global data space: int bkdown = 0; In main(): pthread_barrier_init(&bar,NULL,n); for(i=0;i<n;i++) { pthread_create(&threadIdArray[i],NULL,runner,NULL); if(i==n-2)printf("breakdown imminent!\n"); if(i==n-1)printf("breakdown already occurred!\n"); } ...

Sieve of Eratosthenes by Data parallel method

I am trying to implement the Sieve of Eratosthenes problem by data parallel method using pthreads, but I am not able to figure out how does thread-1 on finding "2" is a prime number broadcast that to all the other threads and when all the other threads mark multiples of 2 they should wait again for the next prime. How will the multiple w...

For pthread, How to kill child thread from the main thread

I use pthread_create to create several child threads. At a time, the main thread wants to kill all child threads or there will be segment falut. Which function should I use to finish that? I searched the answer from google and got function like pthread_kill. But I did not know which signal should I send to the child thread to kill them. ...

behaviour of pthreads and pthread_join function

Hi, I am new to multi-threaded programming and have a question about pthreads. This is the test code that I run and I don't understand its behaviour. Can someone throw some light on it please. void *t1(void *args){ printf("returning from t1\n"); return; } void *t2(void *args){ printf("returning from t2\n"); ...

A function in a pthread

Can I call another function in a thread runner function, called by a pthread_create()? Are there any restrictions on such functions? ...

Virtual method & this pointer

Hi. I'm just beginning to learn C++ and I'm trying to make Thread class that that has the basic functionality of the Java Thread class. What I'm trying to do is make a class which you subclass, write a Run method (which is pure virtual in the base class) create an object of the subclass call the start method on it and you have thread. T...

Reading/Writing from STL Map in multithreaded environment

Problem: I need to write a function which returns a value for a input key from a map. If function can't found the value in map it will fetch the value from database, write into map for future use and return the same. There can be multiple threads calling this function. I am thinking on this line: string GetData (const int key) { p...