如果多个线程在没有同步的情况下调用System.out.println(String),输出是否可以交错?或者每行写入原子?该API只字不提同步的,所以这似乎是可能的,或者是由缓冲和/或虚拟机存储器模型等防止交织输出?
编辑:
例如,如果每个线程包含:
System.out.println("ABC");
Run Code Online (Sandbox Code Playgroud)
输出保证是:
ABC
ABC
Run Code Online (Sandbox Code Playgroud)
或者它可能是:
AABC
BC
Run Code Online (Sandbox Code Playgroud) 在UNIX系统中,我们知道malloc()是一种不可重入的函数(系统调用).这是为什么?
同样,printf()据说也是不可重入的; 为什么?
我知道re-entrancy的定义,但我想知道为什么它适用于这些函数.是什么阻止他们保证可以重入?
可能重复:
Linux上的C中的stdout线程安全吗?
假设thread1和thread2相似,并且在它们的作业结束时它们都是printf.它是线程安全还是必须以某种方式锁定printf?
它与stdout有关吗?如果在每个printf之后执行fflush(stdout)怎么办?它有什么改变吗?
我正在编写一个回调函数C:
static size_t writedata(void *ptr, size_t size, size_t nmemb, void *stream){
size_t written = fwrite(ptr, size, nmemb, (FILE)*stream);
return written;
}
Run Code Online (Sandbox Code Playgroud)
此函数将用于另一个函数,该函数执行HTTP请求,检索请求并将其写入本地计算机.该writedata功能将用于后面的部分.整个操作必须是multithreaded,所以我write和之间存在疑问fwrite.可能有人帮助我,概述之间的差异write(),并fwrite()在C,所以我可以选择哪一个最适合到我的问题吗?
我理解为了避免输出混合,必须同步多个线程对cout和cerr的访问.在同时使用cout和cerr的程序中,单独锁定它们是否足够?或者同时写cout和cerr仍然不安全?
编辑说明:我知道cout和cerr在C++ 11中是"线程安全的".我的问题是,对cout的写入和对不同线程的cerr的写入是否会以两次写入cout的方式相互干扰(导致交错输入等).
我正在尝试编写一些适用于Linux和Win32的代码.我发现它们之间最显着的区别(在我的代码中)是性能fopen().
以下代码在我的Ubuntu上需要5秒,而相同的代码在Windows XP上需要超过100秒.我想在这里做一个说明,ubuntu是VM,而XP是在真机上.
time_t start = time(NULL);
for(int i=0; i < 100000; ++i){
FILE *fp = fopen("a.txt", "a");
if (fp != NULL)
{
fprintf(fp, "Hello World");
fclose(fp);
}
}
time_t end = time(NULL);
printf("\n It took %d seconds \n", end-start);
Run Code Online (Sandbox Code Playgroud)
显然fopen()是造成这种差异的原因.我想知道为什么会有这么大的差异?
我正在使用openmp,我的程序如下所示:
\#pragma omp parallel for
for(x = 0, y = 0, x < 5, x++, y++)
function(x, y, fp);
void function(int x , int y, FILE* fp);
{
fprintf(fp, "(%d, %d)\n", x y);
}
Run Code Online (Sandbox Code Playgroud)
我希望文件的内容为
(0, 0)
(2, 2)
(1, 1)
(3, 3)
(4, 4)
Run Code Online (Sandbox Code Playgroud)
排序无关紧要,但坐标x,y应按顺序排列,即程序不应生成类似(2,3)的内容.这种行为总是得到保证吗?我在linux上使用gcc编译器.
这是我的第一个pthread程序,我不知道为什么printf语句在子线程中打印两次:
int x = 1;
void *func(void *p)
{
x = x + 1;
printf("tid %ld: x is %d\n", pthread_self(), x);
return NULL;
}
int main(void)
{
pthread_t tid;
pthread_create(&tid, NULL, func, NULL);
printf("main thread: %ld\n", pthread_self());
func(NULL);
}
Run Code Online (Sandbox Code Playgroud)
在我的平台上观察到的输出(Linux 3.2.0-32-generic#51-Ubuntu SMP x86_64 GNU/Linux):
1.
main thread: 140144423188224
tid 140144423188224: x is 2
2.
main thread: 140144423188224
tid 140144423188224: x is 3
3.
main thread: 139716926285568
tid 139716926285568: x is 2
tid 139716918028032: x is 3
tid 139716918028032: x is …Run Code Online (Sandbox Code Playgroud) 这是一个相当简单的应用程序,可以通过clone()调用创建轻量级进程(线程).
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 1024*1024
int func(void* param) {
printf("I am func, pid %d\n", getpid());
return 0;
}
int main(int argc, char const *argv[]) {
printf("I am main, pid %d\n", getpid());
void* ptr = malloc(STACK_SIZE);
printf("I am calling clone\n");
int res = clone(func, ptr + STACK_SIZE, CLONE_VM, NULL);
// works fine with sleep() call
// sleep(1);
if (res == -1) {
printf("clone error: …Run Code Online (Sandbox Code Playgroud) 假设我有两个线程将某些内容(相对较长)打印到 或stderr,stdout这两个流的函数是否都是线程安全的,因为它们永远不会“交错”字符?因此,例如,如果我有“Hello, World”,我将永远不会得到“HHellllo,, WorldWorld”或其他任何交错?适用于 x86、GCC、Linux > 3.0。
假设我已经打开dev/poll mDevPoll,我可以安全地调用这样的代码
struct pollfd tmp_pfd;
tmp_pfd.fd = fd;
tmp_pfd.events = POLLIN;
// Write pollfd to /dev/poll
write(mDevPoll, &tmp_pfd, sizeof(struct pollfd));
Run Code Online (Sandbox Code Playgroud)
...同时来自多个线程,或者我是否需要添加自己的同步原语mDevPoll?
c ×8
linux ×3
c++ ×2
pthreads ×2
unix ×2
clone ×1
concurrency ×1
cout ×1
java ×1
multicore ×1
openmp ×1
printf ×1
printstream ×1
reentrancy ×1
solaris ×1
solaris-10 ×1
winapi ×1