如何使用它来杀死线程数组?

时间:2014-11-29 04:28:17

标签: c multithreading pthreads

这段代码工作得很好,但如何使用它来杀死剩余线程数组呢?

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

评论:请删除此内容并添加一些有关代码的额外信息。编辑器不允许我编辑代码。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

但是你想要。没有一种正确的方式&#34;这样做。

最简单的方法是将sleep的调用替换为使用pthread_cond_timedwait的函数和导致该线程调用pthread_exit的谓词。

在伪代码中,使用以下逻辑替换对sleep的调用:

  1. 计算睡觉的时间,直到。

  2. 锁定互斥锁。

  3. 检查谓词是否设置为退出,如果是,请致电pthread_exit

  4. 致电pthread_cond_timedwait

  5. 检查谓词是否设置为退出,如果是,请致电pthread_exit

  6. 检查时间是否过期,如果没有,请停止4。

  7. 要终止线程,请执行以下操作:

    1. 锁定互斥锁。

    2. 将谓词设置为退出。

    3. 致电pthread_cond_broadcast

    4. 释放互斥锁。

    5. 在线程上调用pthread_join等待他们终止。