Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C
#include<stdio.h>
#include<signal.h>
#include<pthread.h>

void *print1(void *tid) 
{
    pthread_t *td= tid;
    pthread_mutex_t lock1=PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_lock(&lock1);
    printf("1");
    printf("2");
    printf("3");
    printf("4\n");
    printf("Coming out of thread1 \n");
    sleep(2);
    pthread_mutex_unlock(&lock1);
    pthread_kill(*td,SIGKILL);//killing remaining all threads 
    return NULL;
}
void *print2(void *arg) 
{
    pthread_mutex_t *lock = arg;
    pthread_mutex_lock(lock);
    sleep(5);        
    printf("5");
    sleep(5);
    printf("6");
    sleep(5);
    printf("7");
    sleep(5);
    printf("8\n");
    fflush(stdout);
    pthread_mutex_unlock(lock);
    return NULL;
}
int main() 
{
    int s;
    pthread_t tid1, tid2;
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    printf("creating Thread 1and 2 \n");
    sleep(2);
    pthread_create(&tid1, NULL, print1,&tid2);
    pthread_create(&tid2, NULL, print2,&lock);
    printf("Running Thread 1\n");
    sleep(2);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    return 0;
}
Posted
Updated 28-Nov-14 21:20pm
v2
Comments
Tomas Takac 29-Nov-14 3:22am    
This code doesn't kill any threads. It starts two threads and the waits for them to finish. What is your problem exactly? Can you be more specific?
[no name] 29-Nov-14 3:55am    
dont u see pthread_kill(*td,SIGKILL)
do u know what it means?
if yes answer me
if not i will explain
Tomas Takac 29-Nov-14 4:18am    
You are right I missed that. I'd say you need store the array in a global variable, the loop trough it at the end of each thread. Add some locking to the equation to make is safe. I'm afraid this is beyond my C knowledge.
[no name] 29-Nov-14 4:00am    
im explaining here "if a thread is in exection and when it completes its execution if we are coming out of this thread then i wanna kill remanining threads" Got??
Tomas Takac 29-Nov-14 4:19am    
You should update your question with that. This makes is much clearer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900