| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 597 人关注过本帖
标题:今天学习多线程有个地方不理解
只看楼主 加入收藏
后卿
Rank: 4
来 自:网络
等 级:业余侠客
威 望:1
帖 子:302
专家分:295
注 册:2016-10-22
结帖率:81.71%
收藏
已结贴  问题点数:20 回复次数:3 
今天学习多线程有个地方不理解
程序代码:
#include <windows.h>
#include <process.h>
#include<iostream>
#include<stdio.h>
unsigned WINAPI threadInc(void* arg);
unsigned WINAPI threadDes(void* arg);
HANDLE hEvent;
int main(int argc, char* argv[])
{
    HANDLE Handles[2]{};
    int i;
    hEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
       
    Handles[0] = (HANDLE)_beginthreadex(NULL, 0, threadA, NULL, 0,
        NULL);
         
    Handles[1] = (HANDLE)_beginthreadex(NULL, 0, threadB, NULL, 0,
                NULL);
     
    WaitForMultipleObjects(2, Handles, TRUE, INFINITE);
    CloseHandle(hEvent);
    system("pause");
    return 0;
}
unsigned WINAPI threadA(void* arg)
{
    for (int j=0;j<2;j++)
    {
        int i;
        std::cout << "我是猪" << std::endl;
        WaitForSingleObject(hEvent, INFINITE);
        for (int i = 0; i < 2; i++)
        {
            std::cout << "我是猪22" << std::endl;
        }
        SetEvent(hEvent);
    }    
    return 0;
}
unsigned WINAPI threadB(void* arg)
{
    for (int j=0;j<2;j++)
    {
        int i;
        std::cout << "woshizhu" << std::endl;
        WaitForSingleObject(hEvent, INFINITE);
        for (int i = 0; i < 2; i++)
        {
            std::cout << "woshizhu33" << std::endl;
        }
         SetEvent(hEvent);
    }
    return 0;
}

遇到了4个问题,
1.将hEvent 设置为有信号状态,随机进入线程A,打印“我是x”,接下来有没有可能连续打印两次中文“我是x22”呢?
2.再连续打印两次中文“我是X22"后,SetEvent,然后,因为自己发出的SetEvent不作用于自己,所以会进入线程B,那这个时候会跳到waitfor那一行呢?还是会跳到线程B里的打印英文”woshix“那一行(后面没有33)
3.按照2的逻辑,线程A结束后不会再次执行线程A,但为什么会有这样的结果,连续打印了两次线程A
woshizhu
woshizhu33
woshizhu33
woshizhu

woshizhu33
woshizhu33
图片附件: 游客没有浏览图片的权限,请 登录注册


4.为什么打印结果有一行空着呢?我只写了个换行,是打印结果后换行,而不是让它换整个空行


[此贴子已经被作者于2023-5-25 10:02编辑过]

搜索更多相关主题的帖子: 线程 打印 std NULL int 
2023-05-25 10:01
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:14 
1. 听不懂,带猜。能
2. 完全听不懂
3. 完全听不懂
4. 你看输出的第一行,是不是少了个换行。
2023-05-25 10:23
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
BTW,换成 C++ 代码的话,当是

程序代码:
#include <iostream>
#include <string>
#include <thread>
#include <semaphore>

int main( void )
{
    std::binary_semaphore smph {1};

    auto thread_common = [&smph]( const char* id ) {
        for( int i=0; i!=2; ++i )
        {
            std::clog << (std::string(id)+'\n');

            smph.acquire();
            std::clog << (std::string(id)+" xxx \n");
            smph.release();
        }
    };

    std::thread ta( thread_common, "Thread A" );
    std::thread tb( thread_common, "Thread B" );
    tb.join();
    ta.join();
}
2023-05-25 11:07
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
想了想,但这代码毫无意义呀,什么都不能说明。因此改为

程序代码:
#include <iostream>
#include <string>
#include <thread>
#include <semaphore>

int main( void )
{
    std::binary_semaphore smph {1};
    auto thread_common = [&smph]( const char* id )
    {
        using namespace std::chrono_literals;
        for( int i=0; i!=2; ++i )
        {
            smph.acquire();
            std::clog << '[';
            std::this_thread::sleep_for( 50ms );
            std::clog << id << " xxx";
            std::this_thread::sleep_for( 50ms );
            std::clog << "]\n";
            smph.release();
        }
    };

    std::jthread ta( thread_common, "Thread A" );
    std::jthread tb( thread_common, "Thread B" );
}


如果发生错乱,则说明 binary_semaphore 没有起作用。
2023-05-25 12:21
快速回复:今天学习多线程有个地方不理解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025015 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved