Multiple instances of pthread


 
Thread Tools Search this Thread
Top Forums Programming Multiple instances of pthread
# 1  
Old 05-21-2012
Multiple instances of pthread

Suppose I declare
pthread_t clear_thread;
and then
pthread_create(&clear_thread, &detach, clear_message, this);
the thread is supposed to go away, perform the service it is intended to procide, and then kill itself.

A little while later, I require this service again, so I say
pthread_create(&clear_thread, &detach, clear_message, this);
Now if, whether by accident or design, the first thread is still running, what is supposed to happen? The man pages seem to be silent on this issue (one would have hoped that creating a new thread on an already running identifier would return an error, but it seems not to be so).

So, are there now two threads running? and if I try to do a pthread_kill on it which thread (or both?) gets killed?

And, for that matter, is there any way to tell whether a thread that I have created is still running? Or do I have to declare a variable to keep track of that myself?

I suspect that the answer is that I have made a programming blunder by allowing this situation to arise, but it is an easy mistake to make, especially if you did not foresee the necessity to demand this service frequently (I got into this mess because my progma ran amok and started demanding service continuously, and eventually I think I got into some deadlock). But if that is the answer, then I suspect it is a bug in the design of the pthread system - or is it a feature?

For the record, I am trying to put messages up on an LCD, and this thread is created to take them down again after 30 seconds, or when the user acknowledges them, or whatever, and there are associated threads for putting the backlight on, and turning it off after a while.
# 2  
Old 05-21-2012
Several points. Thread synchronization ( what you are asking about)
Code:
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, some_func, (void *)args);

This creates a detached thread - one that goes away when it hits a pthread_exit() statement or a return statement at the end of some_func();

The default pthread_create() call, by default, in most UNIXes, creates a joinable thread.
Not a detached thread like above.

So you do this for each thread:
Code:
pthread_t tid;
pthread_create(&tid, &attr, some_func, (void *)args);
pthread_join(tid, NULL);


The code blocks on pthread_join() until the child thread hits return or pthread_exit().
One type of thread you wait for (join) the other you do not care about.

You need to decide which one you want to use, there really aren't a lot of other choices.
You can set a global variable:
Code:
volatile int i_am_done=0;

and change the variable to let the parent know a detached thread completed.
Protect the volatile int with a mutex: pthread_mutex_lock & pthread_mutex_unlock calls, whenever you set or look at the variable.

And. pthread_kill is not always a great choice. All it does is to direct the signal to the thread. Example: SIGKILL (kill -9) destroys the entire process because the effect of the signal is to nuke the entire process. So signals do not behave the way you might believe a priori.
# 3  
Old 05-22-2012
Multiple instances of pthread

Indeed, but the pthreads in my example were created as detached (I showed a parameter '&detach' which was intended to give a strong hint to that effect).

And I agree that pthread_kill has to be used with caution, but in my case the threads were all 'sleep'ing, so I killed them with SIGALRM and they woke up and proceeded on their way, which was to kill themselves.

But you have not answered my main question, which was what happens (or is supposed to happen) if you create another thread on the same tid variable before it has died. It may be (indeed probably is) bad practice, but bad practice (aka s**t) happens, and the pthread system offers no help in avoiding it.

And if I need to know whether the thread on some tid is still running, how can I tell? Do I have to create some sufficiently global variable and keep track of it myself (which is what I have done) is is there some way I can ask?

It seems that what I need to do is discover whether a thread is still running on the tid, and then either arrane to kill it, or else to persuade the existing thread to take on new responsibilities. When I first wrote this code, I did not consider this possibility and it worked fine until things started to happen too faatSmilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

2. Shell Programming and Scripting

Grep with multiple instances of same pattern

Hi, This is my text file I'm trying to Grep. Apple Location Greenland Rdsds dsds fdfd ddsads http Received Return Immediately Received End My Grep command: grep only--matching 'Location.*Received' Because the keyword Received appears twice, the Grep command will stop at the last... (3 Replies)
Discussion started by: spywarebox
3 Replies

3. Shell Programming and Scripting

Grep with multiple instances of same pattern

Hi, This is my text file I'm trying to Grep. Apple Location Greenland Rdsds dsds fdfd ddsads http Received Return Immediately Received End My Grep command: grep only--matching 'Location.*Received' e. Because the keyword Received appears twice, the Grep command will stop at the last... (0 Replies)
Discussion started by: spywarebox
0 Replies

4. Shell Programming and Scripting

Checking for multiple instances of a process

Hi I have a scenario where i need to check multiple instances of a running shell script (abc.sh) . How can I find from inside a running shell script whether any other instance of the same script is running or not? If any other instance of same shell script is running I need to exit from... (4 Replies)
Discussion started by: raghu.amilineni
4 Replies

5. Shell Programming and Scripting

Multiple instances of a job.

Could you please let me know how to create/make a multiple instances of a job/process in ksh(shell scripting). i.e., at present the parent script is calling another child/dependent script for only once. What we want is, the parent script itself has to execute multiple times, and in each one it... (1 Reply)
Discussion started by: Gangegowda
1 Replies

6. AIX

multiple instances of same vg on same AIX machine

hi, i am new to AIX and to this forum as well. Can you please help me out with following issue/requirement 1) I have one physical volume (pv1) (a scsi disk). (pv1) on 1st AIX machine. I have a single volume group on it(vg1). 2)I removed it from the 1st AIX machine and exported to the 2nd... (1 Reply)
Discussion started by: navadeep
1 Replies

7. Shell Programming and Scripting

kill multiple instances of the same program

Hi, I know that the answer to this is very simple, since I saw somebody do it some time back..but I forgot how. The problem is, I have multiple instances of the same program running simultaneously and I want to kill them all in a single command. I know that it can be done using awk '{print... (12 Replies)
Discussion started by: ipzig
12 Replies

8. Shell Programming and Scripting

detecting multiple instances

Hi Gurus I have a requirement like this. i use solaris OS.. if there are 2 instances of the same ksh file running in the directory, i need to kill the ksh file that started to run latest. suppose ragha.ksh starts running thru cron in abc/xyz directory now ragha.ksh started running by any... (3 Replies)
Discussion started by: ragha81
3 Replies

9. UNIX for Dummies Questions & Answers

Multiple file instances

I am capturing text based reports with a specific program, which works no problem. However, since I send report warehouse output as they are migrated from the database software, on occasion when two capture process' initiate simultaneously, the capture file locks up. Is there a way to setup (in... (1 Reply)
Discussion started by: gozer13
1 Replies

10. UNIX for Advanced & Expert Users

multiple instances of syslogd - is it possible?

I would like to start up multiple instances of syslog daemon. I am having a little difficulty. Is this at all possible? I have separate syslog.conf1.... syslog.conf5 files. I have linked the daemon to separate files syslogd1 ... syslogd5 I have arranged the rcd.2 start/stop scripts for... (9 Replies)
Discussion started by: Gary Dunn
9 Replies
Login or Register to Ask a Question