【问题标题】:how one thread can be killed in another thread如何在另一个线程中杀死一个线程
【发布时间】:2011-06-28 04:58:13
【问题描述】:

实际上,主要场景是:从主线程有两个线程在运行。通过使用条件变量,两个线程将运行并休眠,然后返回主线程。我的意思是我不想要不同的输出模式。只有一个模式:从 main->thread1->thread2->main。 我为 C 线程编写了一个代码。它有时会显示我想要的结果,有时会显示我想要的结果。例如,输出是:

I am in thread 1 
before conditional wait
I am in thread 2
before conditional release
i am again in thread 2
i am again in thread 1
main exits here

问题是有时“这里的主要出口”不执行。请帮助我。需要注意的是我不能使用 pthread_join()。我的代码如下所示

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t gLock;
pthread_cond_t gCondition;

pthread_mutex_t mLock;
pthread_cond_t mCondition;

void initialize()
{
      pthread_mutex_init(&gLock, NULL);
      pthread_cond_init (&gCondition, NULL);
      pthread_mutex_init(&mLock, NULL);
      pthread_cond_init (&mCondition, NULL);

      return;
}

void * threadOne(void * msg)
{
    printf("%s \n",(char*) msg);
    printf("before conditional wait\n");

    pthread_mutex_lock(&gLock);
    pthread_cond_wait(&gCondition,&gLock);
    pthread_mutex_unlock(&gLock);

    printf("i am again in thread 1\n");

    pthread_mutex_lock(&mLock);
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);

}

void * threadTwo(void * msg)
{
    printf("%s\n",(char*)msg);
    printf("before conditional release\n");
    pthread_mutex_lock(&gLock);
    pthread_cond_signal(&gCondition);
    pthread_mutex_unlock(&gLock);
    printf("i am again in thread 2\n");

}

int main()
{
        pthread_t thread1;
        pthread_t thread2;

        char * msg1="I am in thread 1";
        char * msg2="I am in thread 2";
        initialize();

        pthread_create(&thread1,NULL,threadOne,(void*) msg1);
        pthread_create(&thread2,NULL,threadTwo,(void*) msg2);

        pthread_mutex_lock(&mLock);
        pthread_cond_wait(&mCondition,&mLock);
        pthread_mutex_unlock(&mLock);

        printf("main exits here");

        return 0;
}

【问题讨论】:

  • 为什么不能使用pthread_join?这是父线程等待子线程完成的常用方式。
  • 你应该在'main exits here'的末尾添加一个换行符。名义上,这可能是您的问题,但实际上不太可能是问题。
  • 你的线程函数可能应该返回一个值——即使它只是 0。
  • 换行实际上没用。我还是很无奈。

标签: pthreads threadcontext


【解决方案1】:

问题是您错误地使用了条件变量。条件变量只是一种通知机制,而不是标志。除了当前等待的线程列表之外,它没有任何内部状态。因此,如果在其他线程调用pthread_cond_signal()main() 还没有实际执行到pthread_cond_wait() 调用,那么信号就会丢失,main() 将永远等待。

您需要使用与条件变量关联的单独标志。 main() 然后可以检查这个标志,只有在标志没有设置时才等待。此外,它必须在循环中检查此标志,以确保处理“虚假唤醒”,其中pthread_cond_wait() 返回没有相应的信号。 threadOnethreadTwo 之间的通知也是如此。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t gLock;
pthread_cond_t gCondition;
int gFlag=0;

pthread_mutex_t mLock;
pthread_cond_t mCondition;
int mFlag=0;

void initialize()
{
    pthread_mutex_init(&gLock, NULL);
    pthread_cond_init (&gCondition, NULL);
    pthread_mutex_init(&mLock, NULL);
    pthread_cond_init (&mCondition, NULL);
}

void * threadOne(void * msg)
{
    printf("%s \n",(char*) msg);
    printf("before conditional wait\n");

    pthread_mutex_lock(&gLock);
    while(!gFlag)
    {
        pthread_cond_wait(&gCondition,&gLock);
    }
    pthread_mutex_unlock(&gLock);

    printf("i am again in thread 1\n");

    pthread_mutex_lock(&mLock);
    mFlag=1;
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);

}

void * threadTwo(void * msg)
{
    printf("%s\n",(char*)msg);
    printf("before conditional release\n");
    pthread_mutex_lock(&gLock);
    gFlag=1;
    pthread_cond_signal(&gCondition);
    pthread_mutex_unlock(&gLock);
    printf("i am again in thread 2\n");

}

int main()
{
    pthread_t thread1;
    pthread_t thread2;

    char * msg1="I am in thread 1";
    char * msg2="I am in thread 2";
    initialize();

    pthread_create(&thread1,NULL,threadOne,(void*) msg1);
    pthread_create(&thread2,NULL,threadTwo,(void*) msg2);

    pthread_mutex_lock(&mLock);
    while(!mFlag)
    {
        pthread_cond_wait(&mCondition,&mLock);
    }
    pthread_mutex_unlock(&mLock);

    printf("main exits here");

    return 0;
}

【讨论】:

    猜你喜欢
    • 2010-12-13
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多