提问人:Дмитрий Першуков 提问时间:11/16/2023 最后编辑:genpfaultДмитрий Першуков 更新时间:11/16/2023 访问量:115
多线程打印字符串仅一次 [已关闭]
Multiple thread print string only one time [closed]
问:
我想编写一个简单的代码,它将使用 4 个线程打印“Hello world”4 次:
#include <stdio.h>
#include <stdlib.h>
#include "omp.h"
int main(void) {
#pragma omp parallel num_threads(4)
{
#pragma omp critical
{
//i temporalily changed it to print thread number for debug
printf("Thread rank: %d\n", omp_get_thread_num());
}
}
return 0;
}
...但是我的代码只打印一次线程号:
据我了解,应该创建 4 个线程,所以这里可能没有错误。#pragma omp parallel num_threads(4)
#pragma omp critical
这样程序就不会同时打印文本并产生垃圾。
我使用Visual Studio并打开了openmp支持:
据我从vis studio控制台中了解,线程已创建,但由于某种原因不打印任何内容。
输出:
'Programm.exe' (Win32): Loaded 'mypath here\Programm.exe'. Symbols loaded.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\vcomp140d.dll'.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'.
The thread 0x5f9c has exited with code 0 (0x0).
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\kernel.appcore.dll'.
'Programm.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'.
The thread 0x30c8 has exited with code 0 (0x0).
The thread 0x4794 has exited with code 0 (0x0).
The program '[5548] Programm.exe' has exited with code 0 (0x0).
答: 暂无答案
评论
# i temporalily changed it to print thread number for debug
这不会编译。您运行了最后成功编译的程序但没有输出吗?Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30142.1 for x64
active