Hello everyone, again:P
I asked before some days something about pthreads and got some useful information so you might be able to help me again cause I'm stuck :/
I need to create a number of threads. There are 2 types of a thread. Let's say, type1 will be doing some work (e.g. doing a simple dummy loop) and type2 choses one random thread and cancels it.
I'll give a code to describe what I'm trying to do.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define THREAD_NUM 20
pthread_t threads[THREAD_NUM];
pthread_attr_t pta[THREAD_NUM];
struct thread_data{
int thread_id;
int thread_type;
int work; //when the thread is running it work will be set to "1" else "0"
};
struct thread_data tdata[THREAD_NUM];
int main(){
for( i=0; i<THREAD_NUM; i++ ){
tdata[i].thread_id=i;
tdata[i].thread_type = //this will be 1 or 2 chosed by another function
rc=pthread_attr_init(&pta[i]);
rc=pthread_create(&threads[i], &pta[i], thread_fun, (void *)&tdata[i]);
//error check
if(rc){
exit(-1);
}
}
for( j=0; j<THREAD_NUM; j++ ){
pthread_join(threads[j], NULL);
}
return 0;
}
void *thread_fun(void *threadarg){
struct thread_data *uniquedata;
uniquedata = (struct thread_data *) threadarg;
uniquedata->work = 1; //set 1 as long it is working. When finished will set back to 0 again
int taskid = uniquedata->thread_id;
int tasktype = uniquedata->thread_type;
//set cancelstate so it will be able for a thread to be deleted immediately
pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
if(tasktype==1){
//do some work here (e.g. loop for some time more than 1 min to ensure that it can be canceled before it has finished)
}
else if(tasktype==2){
//a random thread chosen to be deleted might have been ended or already canceled so we need some checking first
int done=0;
while(!done){
//pick a random thread. (this is done by calling another function not needed here)
//this random number (lets say dstr) will be between 0-THREAD_NUM ()
//so now I will check if this thread is running
if( tdata[dstr].work == 1 ){
//report tdata and set done=1
tdata[dstr].work=0;
done=1;
pthread_attr_destroy(&pta[dstr]); //destroy the attributes of the random thread chosen
pthread_cancel(threads[dstr]); //cancel the chosen thread
printf("Thread %d has been deleted succesfully!!!!!!\n", dstr);
}
else{
printf("The random chosen thread %d has already been deleted or terminated. Chosing another..\n", dstr);
}
}
}
uniquedata->work = 0;
pthread_exit(NULL); //not sure if I need this here
}
The problem is that when I run this program, some threads (not all!) will not be canceled for some weird reason (it seems like, in the "if( tdata[dstr].work == 1 )" line gets 0 instead of 1 :/
I understand that if a type2 thread wants to cancel some other type2 may not succeed because it has already done its work and exited, but this happens also with type1 threads.
I tried also to use mutexes but had the same problem again..
I hope this code is undestanable though :P
any help would be appreciated, I'm stuck here for 4 days and can't figure out what's happening.
If I said something silly excuse me but it's my first time writing multithreaded code :P
Thanks for your time, n.v