这段代码运行良好,但是如何使用它杀死其余线程数组?

#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;
}


注释:请删除此代码,并添加有关代码的其他信息。编辑器不允许我编辑代码。

最佳答案

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\n");

    printf("Coming out of thread1 \n");
    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\n");
    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 \n");

    // 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\n");

    // 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

关于c - 如何使用它杀死线程数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27198864/

10-13 09:08