Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不中断行的情况下打印新线程?(特别是c+;+;) 我在Linux上用C线程处理了一个相当不错的量,现在我尝试在Windows上做C++,但是我在打印到标准输出方面遇到了麻烦。在线程执行的函数中,我有: void print_number(void* x){ int num = *(static_cast<int*> (x)); std::cout << "The number is " << num << std::endl; }_C++_Multithreading - Fatal编程技术网

如何在不中断行的情况下打印新线程?(特别是c+;+;) 我在Linux上用C线程处理了一个相当不错的量,现在我尝试在Windows上做C++,但是我在打印到标准输出方面遇到了麻烦。在线程执行的函数中,我有: void print_number(void* x){ int num = *(static_cast<int*> (x)); std::cout << "The number is " << num << std::endl; }

如何在不中断行的情况下打印新线程?(特别是c+;+;) 我在Linux上用C线程处理了一个相当不错的量,现在我尝试在Windows上做C++,但是我在打印到标准输出方面遇到了麻烦。在线程执行的函数中,我有: void print_number(void* x){ int num = *(static_cast<int*> (x)); std::cout << "The number is " << num << std::endl; },c++,multithreading,C++,Multithreading,当我希望每个人都在一条单独的线上。我猜每个线程都能够在另一个线程在“您的C示例是意外工作的;printf等等”之间编写一个单独的部分之后写入标准输出 这确实是互斥的情况。我通常在本地分配它的静态功能。例如: void atomic_print(/*args*/) { static MyMutex mutex; mutex.acquire(); printf(/*with the args*/); mutex.release(); } 您的C示例是偶然工作的;pr

当我希望每个人都在一条单独的线上。我猜每个线程都能够在另一个线程在“您的C示例是意外工作的;
printf
等等”之间编写一个单独的部分之后写入标准输出

这确实是互斥的情况。我通常在本地分配它的静态功能。例如:

void atomic_print(/*args*/) {
    static MyMutex mutex;
    mutex.acquire();
    printf(/*with the args*/);
    mutex.release();
}

您的C示例是偶然工作的;
printf
等等

这确实是互斥的情况。我通常在本地分配它的静态功能。例如:

void atomic_print(/*args*/) {
    static MyMutex mutex;
    mutex.acquire();
    printf(/*with the args*/);
    mutex.release();
}

您的C示例是偶然工作的;
printf
等等

这确实是互斥的情况。我通常在本地分配它的静态功能。例如:

void atomic_print(/*args*/) {
    static MyMutex mutex;
    mutex.acquire();
    printf(/*with the args*/);
    mutex.release();
}

您的C示例是偶然工作的;
printf
等等

这确实是互斥的情况。我通常在本地分配它的静态功能。例如:

void atomic_print(/*args*/) {
    static MyMutex mutex;
    mutex.acquire();
    printf(/*with the args*/);
    mutex.release();
}

<>在C++中,我们首先喜欢把参数作为<代码> int */COD>。然后,我们就可以锁定。在C++ 11中:

std::mutex mtx; // somewhere, in case you have other print functions 
                // that you want to control

void print_number(int* num) {
    std::unique_lock<std::mutex> lk{mtx}; // RAII. Unlocks when lk goes out of scope
    std::cout << "The number is " << *num << std::endl;
}
std::mutex mtx;//在某个地方,以防您有其他打印功能
//你想控制的
无效打印编号(int*num){
std::unique_lock lk{mtx};//RAII.在lk超出范围时解锁

STD:C++中的CUT< P>,首先我们喜欢把参数作为<代码> int */COD>。然后,我们可以锁定。在C++ 11中:

std::mutex mtx; // somewhere, in case you have other print functions 
                // that you want to control

void print_number(int* num) {
    std::unique_lock<std::mutex> lk{mtx}; // RAII. Unlocks when lk goes out of scope
    std::cout << "The number is " << *num << std::endl;
}
std::mutex mtx;//在某个地方,以防您有其他打印功能
//你想控制的
无效打印编号(int*num){
std::unique_lock lk{mtx};//RAII.在lk超出范围时解锁

STD:C++中的CUT< P>,首先我们喜欢把参数作为<代码> int */COD>。然后,我们可以锁定。在C++ 11中:

std::mutex mtx; // somewhere, in case you have other print functions 
                // that you want to control

void print_number(int* num) {
    std::unique_lock<std::mutex> lk{mtx}; // RAII. Unlocks when lk goes out of scope
    std::cout << "The number is " << *num << std::endl;
}
std::mutex mtx;//在某个地方,以防您有其他打印功能
//你想控制的
无效打印编号(int*num){
std::unique_lock lk{mtx};//RAII.在lk超出范围时解锁

STD:C++中的CUT< P>,首先我们喜欢把参数作为<代码> int */COD>。然后,我们可以锁定。在C++ 11中:

std::mutex mtx; // somewhere, in case you have other print functions 
                // that you want to control

void print_number(int* num) {
    std::unique_lock<std::mutex> lk{mtx}; // RAII. Unlocks when lk goes out of scope
    std::cout << "The number is " << *num << std::endl;
}
std::mutex mtx;//在某个地方,以防您有其他打印功能
//你想控制的
无效打印编号(int*num){
std::unique_lock lk{mtx};//RAII.在lk超出范围时解锁

std::看不到互斥体和信号量之间的差异看不到互斥体和信号量之间的差异看不到互斥体和信号量之间的差异@RaphaelM。事实上,这是因为根据标准,
printf
是线程安全的,但不是(必要的)原子的。请参阅我答案中的第二个链接。@RaphaelM。确实如此;这是因为根据标准,
printf
是线程安全的,但不一定是原子的。请参阅我答案中的第二个链接。@RaphaelM。确实如此;这是因为根据标准,
printf
是线程安全的,但不一定是原子的。请参阅我答案中的第二个链接。@RaphaelM。确实如此;这是因为根据标准,
printf
是线程安全的,但不一定是原子的。请参阅我答案中的第二个链接。请注意,在某些平台(如Windows)上,写入控制台的成本相当高(IIRC每次调用至少50ms,因此此处给出的函数中的单个
cout
)为150ms)。在写入期间锁定通常意味着您基本上已将“并行”函数顺序化。在这些情况下,您最好将控制台写入转移到单独的线程,并使用线程安全的消息传递方案将字符串传递到写入线程。请注意,在某些平台(如Windows)上写入控制台非常昂贵(IIRC每次调用至少50ms,因此这里给出的函数中单个
cout
至少150ms)。在写入期间锁定通常意味着您基本上已将“并行”函数顺序化。在这些情况下,您最好将控制台写入转移到单独的线程,并使用线程安全的消息传递方案将字符串传递到写入线程。请注意,在某些平台(如Windows)上写入控制台非常昂贵(IIRC每次调用至少50ms,因此这里给出的函数中单个
cout
至少150ms)。在写入期间锁定通常意味着您基本上已将“并行”函数顺序化。在这些情况下,您最好将控制台写入转移到单独的线程,并使用线程安全的消息传递方案将字符串传递到写入线程。请注意,在某些平台(如Windows)上写入控制台非常昂贵(IIRC每次调用至少50ms,因此这里给出的函数中单个
cout
至少150ms)。在写入期间锁定通常意味着您基本上已将“并行”函数顺序化。在这些情况下,最好将控制台写入转移到单独的线程,并使用线程安全的消息传递方案将字符串传递到写入线程。