转载-关于线程安全在msdn中概念

下面这两个网址对于windows下c++开发真的很有用,记录一下。

Visual C++ in Visual Studio 2015
https://msdn.microsoft.com/zh-cn/library/60k1461a.aspx

欢迎使用 Visual Studio 2015
https://msdn.microsoft.com/zh-cn/library/dd831853.aspx

C++ 标准库中的线程安全

Visual Studio 2015

The following thread safety rules apply to all classes in the Standard C++ Library—this includes shared_ptr, as described below.Stronger guarantees are sometimes provided—for example, the standard iostream objects, as described below, and types specifically intended for multithreading, like those in <atomic>.

An object is thread-safe for reading from multiple threads.For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously.

If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected.For example, given an object A, if thread 1 is writing to A, then thread 2 must be prevented from reading from or writing to A.

It is safe to read and write to one instance of a type even if another thread is reading or writing to a different instance of the same type.For example, given objects A and B of the same type, it is safe when A is being written in thread 1 and B is being read in thread 2.

shared_ptr


Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership.

iostream


The standard iostream objects cin, cout, cerr, clog, wcin, wcout, wcerr, and wclog follow the same rules as the other classes, with this exception: it is safe to write to an object from multiple threads.For example, thread 1 can write to cout at the same time as thread 2.However, this can cause the output from the two threads to be intermixed.

System_CAPS_note注意

Reading from a stream buffer is not considered to be a read operation.Instead it is considered to be a write operation because the state of the class is changed. (难道这里是说cin时也不是线程安全的吗?)

下面是一个多线程进行cout输出的例子。结果显示了在没有进行任何同步时,cout输出的情况。

//MultiThread
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);   //win
int main()
{
    int j = 0;
    HANDLE hThread_1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);

   CloseHandle(hThread_1);  //句柄就相当于一个资源的指针,释放句柄相当于将这个指针释放,但相应的这个句柄所指向的线程还是会执行。

   //对closehandle()的疑惑终于弄明白了-steel2010-ChinaUnix博客
   http://blog.chinaunix.net/uid-22156506-id-401460.html
    while (j++ < 1000)
        cout << "MainThread is running for" << " the " << j << " times " << endl;
    system("pause");
    return 0;
}
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
    int i = 0;
    while (i++ < 1000)
        cout << "Thread 1 is running for" << " the " << i << " times " << endl;
    return 0;
}

输出内容:(将cout重定向到文件)a.exe>3.txt

image

虽然创建了线程1时设置了是立即执行入口函数,但是先输出的是主函数循环中的函数。

多个线程的执行是分时间片运行的,主线程和子线程即使每个线程都不主动将自己Sleep或挂起,另一个线程也是会执行的。

转载于:https://my.oschina.net/ray1421/blog/703620

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值