Pthreads problem and a few questions - c++

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM_THREADS 8
char *messages[NUM_THREADS];
struct thread_data
{
int thread_id;
int sum;
char *message;
};
struct thread_data thread_data_array[NUM_THREADS];
void *PrintHello(void *threadarg)
{
int taskid, sum;
char *hello_msg;
struct thread_data *my_data;
sleep(1);
my_data = (struct thread_data *) threadarg;
taskid = my_data->thread_id;
sum = my_data->sum;
hello_msg = my_data->message;
printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
//int *taskids[NUM_THREADS];
int rc, t, sum;
sum=0;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvytye, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";
for(t=0;t<NUM_THREADS;t++) {
sum = sum + t;
thread_data_array[t].thread_id = t;
thread_data_array[t].sum = sum;
thread_data_array[t].message = messages[t];
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&thread_data_array[t]);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
The above piece of code gives me a segmentation fault in linux using gcc
As a beginner I have a few questions in mind regarding threads
If 3 threads are created simultaneously and if they have to exit then the thread that is created first is exitted at the end?
Can the o/p differ every time of a thread creation and termination program and if so why??(I have noticed they do so");

If 3 threads are created simultaneously and if they have to exit then the thread that is created first is exited at the end?
Answer: there is no guarantee that the first thread should exit first. Once they are created, they are treated as separate entities by the OS and it's up to the OS to decide which will exit first.
Can the o/p differ every time of a thread creation and termination program and if so why? (I have noticed they do so);
Answer: yes they do differ, the reason being the same as explained above.

How and when each thread is scheduled is completely upto the OS.
There is no way to understand this without digging deep into the OS code.
In most implementations of pthreads (I have not used all, but all I have used).
When the main thread exits all the other threads stop executing and the application quits.
Thus before exiting the main thread should wait for (or kill) all child threads before exiting.
For this we have pthread_join().
for(t=0;t<NUM_THREADS;t++)
{
void* result;
pthread_join(threads[t],&result);
}
// Now all the children are dead.
Note the underlying io system is not thread safe.
So if multiple threads write to IO then you may potentially get some interleaving.

Your code is working perfectely(I am also using gcc)..
output:
Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6
Creating thread 7
Thread 1: French: Bonjour, le monde! Sum=1
Thread 0: English: Hello World! Sum=0
Thread 2: Spanish: Hola al mundo Sum=3
Thread 4: German: Guten Tag, Welt! Sum=10
Thread 3: Klingon: Nuq neH! Sum=6
Thread 5: Russian: Zdravstvytye, mir! Sum=15
Thread 6: Japan: Sekai e konnichiwa! Sum=21
Thread 7: Latin: Orbis, te saluto! Sum=28

Related

Can't understand this basic threads behaviour

I am studying about threads and concurrent programming. Tried this basic example from what was provided in class:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 8
void *printGoGators(void *noArg)
{
printf("Go Gators!\n");
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc, t;
for (t = 0; t < NUM_THREADS; t++)
{
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t],
NULL,
printGoGators,
NULL);
if(rc)
{
printf("ERROR %d", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
This code produces the following output. :
Creating thread 0
Creating thread 1
Go Gators!
Creating thread 2
Go Gators!
Creating thread 3
Go Gators!
Creating thread 4
Go Gators!
Creating thread 5
Go Gators!
Creating thread 6
Go Gators!
Creating thread 7
Go Gators!
Go Gators!
Why is Go Gators! not printed directly after its corresponding Creating thread # for all threads?
Please help!
If your code looked like this, then the output would be in the order you expect:
for (t = 0; t < NUM_THREADS; t++)
{
printf("Creating thread %d\n", t);
printGoGators(NULL);
}
So, you're making an assumption that threads will execute in the same order that they are created. However, that assumption is incorrect - threads may execute in any order.
After you create a thread it is up to the OS to decide what order threads are executed. You can control it using mutexes and conditions to lock a thread let another thread to run and then unlock it.
Unlike accepted answer, this example uses threads and not just print something in a loop.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 8
pthread_mutex_t myMutex; // Declere global mutex
pthread_cond_t myCondition; // Declere global condition
void *printGoGators(void *arg) {
printf("Go Gators! %i\n", *(int*)arg);
delete (int*) arg;
pthread_cond_signal(&myCondition); // Signal that a condition is met
return NULL;
}
int main(int argc, char *argv[]) {
pthread_mutex_init(&myMutex, NULL); // Initialize mutex
pthread_cond_init (&myCondition, NULL); // Initialize condition
pthread_t threads[NUM_THREADS];
int rc, t;
for (t = 0; t < NUM_THREADS; t++) {
int* n = new int;
*n = t;
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t],
NULL,
printGoGators,
n);
if(rc) {
printf("ERROR %d", rc);
exit(-1);
}
pthread_cond_wait(&myCondition, &myMutex); // waite for condition
}
pthread_exit(NULL);
}
result:
Creating thread 0
Go Gators! 0
Creating thread 1
Go Gators! 1
Creating thread 2
Go Gators! 2
Creating thread 3
Go Gators! 3
Creating thread 4
Go Gators! 4
Creating thread 5
Go Gators! 5
Creating thread 6
Go Gators! 6
Creating thread 7
Go Gators! 7
Main thread creation loop: Create a thread and then wait for the condition.
New thread: Print a message then signal that condition is met.
This way you manage the order of execution.

POSIX Threads - synchronize DETACHED threads using conditional variable MEMORY LEAK

Hello I'm trying to synchronize detached threads using conditional variable, but I found a bug that sometimes causes memory leak (depends on scheduler mood). I think the code is self explanatory. I would appreciate any advice.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
using namespace std;
struct TThrArg
{
pthread_t m_ID;
bool m_IsRunning;
};
TThrArg g_Threads[64];
int g_Counter;
pthread_mutex_t g_Mtx;
pthread_cond_t g_Cond;
void * thrFunc ( void * arg )
{
TThrArg * data = (TThrArg *) arg;
// do some stuff
// -----------------------------------
// for ( int i = 0; i < 5000; ++i )
// for ( int j = 0; j < 5000; ++j )
// int x = 0;
// printf("Thread: %lu running...\n", data->m_ID);
// -----------------------------------
pthread_mutex_lock(&g_Mtx);
memset(data, 0, sizeof(TThrArg));
--g_Counter;
pthread_cond_signal(&g_Cond);
pthread_mutex_unlock(&g_Mtx);
sleep(1); // --> this spot causes that main may end before return NULL so resources will not be freed
return NULL;
}
void createThread ( void )
{
pthread_mutex_lock(&g_Mtx);
for ( int i = 0; i < 64; ++i )
{
if ( g_Threads[i].m_IsRunning == 0 )
{
g_Threads[i].m_IsRunning = 1;
++g_Counter;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&g_Threads[i].m_ID, &attr, thrFunc, &g_Threads[i]);
pthread_attr_destroy(&attr);
break;
}
}
pthread_mutex_unlock(&g_Mtx);
}
int main ( int argc, char * argv[] )
{
pthread_mutex_init(&g_Mtx, NULL);
pthread_cond_init(&g_Cond, NULL);
g_Counter = 0;
for ( int i = 0; i < 64; ++i )
createThread();
pthread_mutex_lock(&g_Mtx);
while ( g_Counter != 0 )
{
pthread_cond_wait(&g_Cond, &g_Mtx);
}
pthread_mutex_unlock(&g_Mtx);
pthread_mutex_destroy(&g_Mtx);
pthread_cond_destroy(&g_Cond);
return 0;
}
The leak you see is because the terminating thread decrements the mutex-protected thread counter, and pauses for a second before the thread actually terminates.
The main execution thread will immediately see that the thread counter reached 0, and terminate before the actual detached threads have exited. Each running thread, even a detached thread, consumes and allocates a little bit of internal memory, which does not get released until the thread actually terminates. This is the leak you see, from execution threads that did not terminate before the main execution thread stopped.
This is not the kind of a leak that you need to worry about. It is rather annoying, and makes debugging difficult, true.
In the past, I took one approach in a framework class library that I wrote some time ago. I did not use detached threads at all, but all threads were joinable threads. The framework started one singleton background thread whose only job was to join() the terminated threads. Then, each thread started by the framework will queue up its own thread id for the singleton background thread, just before each thread terminates.
The net effect was equivalent to detached threads. I could start each thread and not worry about joining to it. It's going to be the background thread's job. The framework would signal the background thread to terminate itself, and join it, before exiting. So, if all goes well, there will not be any reported memory leaks that can be accounted to thread support.

Why is a multithreaded C program forced to a single CPU on Mac OS X when system() is used in a thread?

I encountered a strange difference in the behavior of a program using pthreads between Linux and Mac OS X.
Consider the following program that can be compiled with "gcc -pthread -o threadtest threadtest.c":
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
static
void *worker(void *t)
{
int i = *(int *)t;
printf("Thread %d started\n", i);
system("sleep 1");
printf("Thread %d ends\n", i);
return (void *) 0;
}
int main()
{
#define N_WORKERS 4
pthread_t workers[N_WORKERS];
int args[N_WORKERS];
int i;
for (i = 0; i < N_WORKERS; ++i)
{
args[i] = i;
pthread_create(&workers[i], NULL, worker, args + i);
}
for (i = 0; i < N_WORKERS; ++i)
{
pthread_join(workers[i], NULL);
}
return 0;
}
Running the resulting executable on a 4-core Mac OS X machine results in the following behavior:
$ time ./threadtest
Thread 0 started
Thread 2 started
Thread 1 started
Thread 3 started
Thread 0 ends
Thread 1 ends
Thread 2 ends
Thread 3 ends
real 0m4.030s
user 0m0.006s
sys 0m0.008s
Note that the number of actual cores is probably not even relevant, as the time is simply spent in the "sleep 1" shell command without any computation. It is also apparent that the threads are started in parallel as the "Thread ... started" messages appear instantly after the program is started.
Running the same test program on a Linux machine gives the result that I expect:
$ time ./threadtest
Thread 0 started
Thread 3 started
Thread 1 started
Thread 2 started
Thread 1 ends
Thread 2 ends
Thread 0 ends
Thread 3 ends
real 0m1.010s
user 0m0.008s
sys 0m0.013s
Four processes are started in parallel that each sleep for a second, and that takes roughly a second.
If I put actual computations into the worker() function and remove the system() call, I see the expected speedup also in Mac OS X.
So the question is, why does using the system() call in a thread effectively serialize the execution of the threads on Mac OS X, and how can that be prevented?
#BasileStarynkevitch and #null pointed out that a global mutex in system() implementation in the C library of Mac OS X might be responsible for the observed behavior. #null provided a reference to the potential source file of the system() implementation, where these operations are contained:
#if __DARWIN_UNIX03
pthread_mutex_lock(&__systemfn_mutex);
#endif /* __DARWIN_UNIX03 */
#if __DARWIN_UNIX03
pthread_mutex_unlock(&__systemfn_mutex);
#endif /* __DARWIN_UNIX03 */
By disassembling the system() function in lldb I verified that these calls are actually present in the compiled code.
The solution is to replace the use of the system() C library function with a combination of the fork()/execve()/waitpid() system calls. A quick proof of concept for the modification of the worker() function in the original example:
static
void *worker(void *t)
{
static const char shell[] = "/bin/sh";
static const char * const args[] = { shell, "-c", "sleep 1", NULL };
static const char * const env[] = { NULL };
pid_t pid;
int i = *(int *)t;
printf("Thread %d started\n", i);
pid = fork();
if (pid == 0)
{
execve(shell, (char **) args, (char **) env);
}
waitpid(pid, NULL, 0);
printf("Thread %d ends\n", i);
return (void *) 0;
}
With this modification the test program now executes in approximately one second on Mac OS X.

Joining multiple threads without waiting using pthread library and c++

Say the main thread created three worker threads.
Suppose all three threads run a function similar to work()
below.
bool signal[3]={false};
void* work(void* thread_id) //each worker thread is given a thread_id when created
{
int x = *(int*)thread_id;
int data;
/*code*/
signal[x] = true; //this thread finished
pthread_exit(&data);
}
Usually I use following code to join the threads.
int main()
{
pthread_t workerThreads[3];
int master;
/* code for creating threads */
for(int i=0;i<3;i++)
{
void* status;
master = pthread_join(workerThreads[i],&status);
/* code for handling the return data from thread*/
}
}
What I mean is if workerThreads[2] finished before workerThreads[0] , with the code above, main thread still have to wait for workerThreads[0] finish first before handling workerThreads[2]
Is there a way to join the threads without waiting?
Can I use something like this?
while(!signal[0]||!signal[1]||!signal[2]){
if(signal[0]){/*join workerThreads[0]*/}
if(signal[1]){/*join workerThreads[1]*/}
if(signal[2]){/*join workerThreads[2]*/}
}

Illegal Seek in pthread_cancel

I have a program that is trying to use create and cancel through an implemented pool.
The creation is as follows:
int threadsNum=10;
while (created<threadsNum){
pthread_t newThread;
pthread_struct *st; //Open the thread that handle the deleting of the sessions timeout.
st = (pthread_struct*)malloc(sizeof(pthread_struct));
st->id = created;
st->t = newThread;
pthread_mutex_lock( &mutex_threadsPool );
readingThreadsPool[created] = st;
pthread_mutex_unlock( &mutex_threadsPool );
if((threadRes1 = pthread_create( &newThread, NULL, pcapReadingThread, (void*)created)))
{
syslog(LOG_CRIT, "Creating Pcap-Reading Thread %d failed.",created);
printf( "Creating Pcap-Reading Thread %d failed.\n",created);
exit(1);
}
syslog(LOG_INFO, "Created Pcap-Reading Thread %d Successfully.",created);
created++;
}
Later I try to cancel them and restart them :
pthread_t t;
pthread_struct* tstr;
int i;
pthread_mutex_unlock( &mutex_threadsPool );
//first go on array and kill all threads
for(i = 0; i<threadsNum ; i++ ){
tstr = readingThreadsPool[i];
if (tstr!=NULL){
t = tstr->t;
if (pthread_cancel(t)!=0){
perror("ERROR : Could not kill thread");
}
else{
printf("Killed Thread %d \n",i);
}
}
}
So far so good, but the only problem is that the output is
Error : Could not kill thread : Illegal Seek
Killed Thread 1
Killed Thread 2
Killed Thread 3
Killed Thread 4
Killed Thread 5
Killed Thread 6
Killed Thread 7
Killed Thread 8
Killed Thread 9
Why doesn't it kill the thread in the 0 index as well?
And I couldn't find anything about Illegal Seek..
Thanks for your help people
Thanks
The problem is that newThread is being used before it is initialized:
pthread_t newThread;
pthread_struct *st;
st = (pthread_struct*)malloc(sizeof(pthread_struct));
st->id = created;
st->t = newThread;
but newThread does not receive a value until after the successful invocation of pthread_create(). It appears that newThread variable is retaining its previous value on subsequent iterations of the loop, which results in the correct cancelling of all threads except for last thread started as its id is never inserted into the readingThreadsPool array.
You need to populate the st->t member after the call to pthread_create().
As the code currently stands, it is possible for an entry to be inserted into the readingThreadsPool array even though it is not actually a thread yet. Put the insertion logic after the call to pthread_create():
if((threadRes1 =
pthread_create(&(st->t), NULL, pcapReadingThread, (void*)created)))
{
syslog(LOG_CRIT, "Creating Pcap-Reading Thread %d failed.",created);
printf( "Creating Pcap-Reading Thread %d failed.\n",created);
exit(1);
}
pthread_mutex_lock( &mutex_threadsPool );
readingThreadsPool[created] = st;
pthread_mutex_unlock( &mutex_threadsPool );
or if the pcapReadingThread() function accesses readingThreadsPool and expects an entry for itself (which I think might be the case due to created being passed) then enclose the pthread_create() inside the lock of mutex_threadsPool.