c,c++编程求助,急!!!感激不尽

gstatham 2017-09-04 06:55:13
A.h
struct ContextX
{
int running;
};

extern struct ContextX CX;


A.cpp

线程B:
事件A发生时,CX.running = 0;
事件B发生时,CX.running = 1;

==========================================
main.cpp

struct ContextX CX;

线程A:
void *threadA(void *param)
{
struct ContextX *CX = (struct ContextX *)param;
while(CX.running)
{
//do something
}
}
现在问题是,线程B中的事件A发生时,线程A不会进入while循环,事件B发生时,线程A无法进入循环,这是为什么?
...全文
200 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gstatham 2017-09-05
  • 打赏
  • 举报
回复
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

pthread_t IDA;
pthread_t IDB;

struct _status
{
    int running;
    int value;
};

_status status;

void *testA(void *param)
{
    while(1)
    {
        sleep(10);
        printf("shut down thread B..\n");
        status.running = 0;

        sleep(10);
        status.running = 1;
        printf("start thread B..\n");
    }
}

void *testB(void *param)
{
    while(status.running)
    {
        printf("thread B is running...\n");
        sleep(1);
    }
}

int main(int argc, char *argv[])
{
    status.running = 1;
    pthread_create(&IDA, NULL, testA, NULL);
    pthread_create(&IDB, NULL, testB, NULL);

    pthread_join(IDA, NULL);
    pthread_join(IDB, NULL);

    return 0;
}


程序运行结果如下, thread B is running... thread B is running... thread B is running... thread B is running... shut down thread B.. start thread B.. shut down thread B.. start thread B.. 这是上面问题的总结 为什么线程B中的内容没有被再次运行呢?
gstatham 2017-09-05
  • 打赏
  • 举报
回复
现在已经清楚的定位到,当事件B发生时,CX.running = 1; 线程A中的while内容无法执行,没有搞懂为什么会这样。。
821586284 2017-09-05
  • 打赏
  • 举报
回复
http://blog.csdn.net/yeyuangen/article/details/37593533 参考这篇文章,建议用信号量的方法
gstatham 2017-09-05
  • 打赏
  • 举报
回复
感谢各位的回答,自己给出一套解决方案
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

pthread_t IDA;
pthread_t IDB;

pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

#define RUN 1
#define STOP 0

int status = RUN;

void * thread_function(void *param)
{
    static int i = 0;
    while (1)
    {
        pthread_mutex_lock(&mut);
        while (!status)
        {
            pthread_cond_wait(&cond, &mut);
        }
        pthread_mutex_unlock(&mut);

        //do actual something
        printf("thread is running...\n");
        sleep(1);
    }
}

void thread_resume()
{
    if (status == STOP)
    {
        pthread_mutex_lock(&mut);
        status = RUN;
        pthread_cond_signal(&cond);
        printf("pthread run!\n");
        pthread_mutex_unlock(&mut);
    }
}

void thread_pause()
{
    if (status == RUN)
    {
        pthread_mutex_lock(&mut);
        status = STOP;
        printf("thread stop!\n");
        pthread_mutex_unlock(&mut);
    }
}

void *test(void *param)
{
    while(1)
    {
        sleep(5);
        thread_pause();
        sleep(10);
        thread_resume();
        sleep(5);
    }
}


int main(int argc, char *argv[])
{
    pthread_create(&IDA, NULL, test, NULL);
    pthread_create(&IDB, NULL, thread_function, NULL);

    pthread_join(IDA, NULL);
    pthread_join(IDB, NULL);

    return 0;
}
赵4老师 2017-09-05
  • 打赏
  • 举报
回复
《Windows核心编程》
paschen 版主 2017-09-04
  • 打赏
  • 举报
回复
把程序暂停下来,分别看各线程断在哪,然后再分析原因
gstatham 2017-09-04
  • 打赏
  • 举报
回复
只有一个线程对值进行修改,不需要用信号量
Remove_ 2017-09-04
  • 打赏
  • 举报
回复
这个不是绝对的吧,你让他跑事件B发生时跑久点。会进入的。这个是时间片的关系。多线程最后用互斥,信号量处理下,比较好.
qq_37896222 2017-09-04
  • 打赏
  • 举报
回复
没看懂你的程序,感觉你的程序有子程序和主程序,但是还是有点看不明白,其中A.cpp应该指的是子程序吧,而main.cpp应该是主程序吧。可能不懂。不过就你的问题,可以给你个思路就是单独在A中成立是看B的运行的情况,和单独在B成立时A的成立情况。
qq_37896222 2017-09-04
  • 打赏
  • 举报
回复
没看懂你的程序,感觉你的程序有子程序和主程序,但是还是有点看不明白,其中A.cpp应该指的是子程序吧,而main.cpp应该是主程序吧。可能不懂。不过就你的问题,可以给你个思路就是单独在A中成立是看B的运行的情况,和单独在B成立时A的成立情况。

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧