Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This code worked fine , but how to use it to kill for array of remaining threads?

#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
");
    printf("Coming out of thread1 
");
    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
");
    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 
");
    sleep(2);
    pthread_create(&tid1, NULL, print1,&tid2);
    pthread_create(&tid2, NULL, print2,&lock);
    printf("Running Thread 1
");
    sleep(2);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    return 0;
}

Comments: Please delete this and add some extra information about the code. The editor is not allowing me to edit the code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

here is an edited version of the code
along with some commentary

#include<signal.h>
#include<pthread.h>



void *print1(void *tid)
{
    // should be in global space, so no need to pass
    pthread_t *td= tid; 

    // this is a whole new mutex, 
    //should be in global space so other threads can access it
    pthread_mutex_t lock1=PTHREAD_MUTEX_INITIALIZER; 

    // why bother with the mutex, it does nothing useful
    pthread_mutex_lock(&lock1);
    printf("1");
    printf("2");
    printf("3");
    printf("4
");

    printf("Coming out of thread1 
");
    sleep(2);
    pthread_mutex_unlock(&lock1);

    pthread_kill(*td,SIGKILL);//killing remaining all threads return NULL;

    // this exit is not correct, it should be this call:
    // void pthread_exit(void *rval_ptr);
} // end function: print1


void *print2(void *arg)
{
    // this should be in global memory so all threads using same mutex
    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
");
    fflush(stdout);

    pthread_mutex_unlock(lock);

    // this exit is not correct, it should be this call:
    // void pthread_exit(void *rval_ptr);      
    return NULL;
} // end function: print2


int main()
{
    int s;

    // this should be in global memory
    // so no need to pass to threads
    pthread_t tid1, tid2;

    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

    printf("creating Thread 1and 2 
");

    // why bother to sleep here?
    sleep(2);

    // in these calls, the last parm should be NULL
    // and the related data should be in global memory
    pthread_create(&tid1, NULL, print1,&tid2);
    pthread_create(&tid2, NULL, print2,&lock);

    // we are still in main, so this printf is misleading
    printf("Running Thread 1
");

    // no need to sleep() 
    // as the pthread_join calls will wait for the related thread to exit
    sleep(2);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    return 0;
} // end function: main

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...