Thread safety-Multiple network requests under a single VC

Source: Internet
Author: User

One, thread safety variable control show hidden loading box

Description of the problem:

The same page has two asynchronous network requests, the first request starts, and the loading rotates. The second request starts loading rotation. The first one ends, loading stops spinning, but then the second request is not over. Then the loading ended, and the problem came.


How to resolve:




Second, the problems arising from the above problems:

1. #import <libkern/OSAtomic.h>


This passage was copied from the Internet. Summarizes the role of atomic operations.

However, the osbase.h file mentioned in the article cannot be found. This file may not be in my Lib because of the version number upgrade.

The atomic manipulation functions on the IOS platform start with osatomic and include the header file <libkern/OSBase.h>. Different threads assume that the operation of the same variable by an atomic manipulation function ensures that the operation of one thread does not affect the operation of the other line range to this variable. Because these operations are atomic.

Because atomic operations can only operate on built-in types. So an atomic operation can synchronize a thread that can only be in the address space of the same process.

Reprint please explain below: Thank you.

Click to open link ( http://blog.csdn.net/a21064346/article/details/8076972 )


Singleton mode 1:

  1. + (Abaddressbook *) Sharedaddressbook
  2. {
  3. Static Abaddressbook * volatile __shared = nil;
  4. if (__shared = = nil)
  5. {
  6. Abaddressbook * tmp = [[Abaddressbook alloc] init];
  7. if (Osatomiccompareandswapptr (Nil, tmp, (void * volatile *) &__shared) = = false )
  8. [TMP release];
  9. }
  10. return (__shared);
  11. }

The above section of code is to create a abaddressbook Singleton, in order to ensure that the call Shareaddressbook . There is only one in memory and the memory address is unique (that is, if you are afraid of other threads visiting this function, visit this single case at the same time)

The effect of volatile is that each acquisition of a number of ways is worth reading directly from memory.


Singleton mode 2:

  1. + (Ilscmpersistencemanage *) sharedinstance {
  2. @synchronized ([Ilscmpersistencemanage class]) {
  3. if (__sharedinstance) {
  4. return __sharedinstance;
  5. }
  6. __sharedinstance = [[Ilscmpersistencemanage alloc] init];
  7. return __sharedinstance;
  8. }
  9. }


The difference:

The former: the lower level of the more regional data, the protection of the singleton from a deeper layer, and not only the pointer, but also other data formats. And it does not block the access of other threads to the function.

The latter: lock. The efficiency of the code is lower than the former. If you use other data, and the data is updated very quickly, then the efficiency is very poor.


References and studies:

1, http://southpeak.github.io/blog/2014/10/17/osatomicyuan-zi-cao-zuo/

2, http://www.cocoachina.com/industry/20130821/6842.html

3,http://southpeak.github.io/blog/2014/10/25/objective-c-runtime-yun-xing-shi-zhi-lei-yu-dui-xiang/


2. Osatomic vs Osatomicbarrier


On Intel and uniprocessor platforms, it doesn ' t matter.

For multiprocessor PPC systems, should all use the barrier variety of functions, unless the atomic store affects no Data Other than the atomic variable.

The following would not be OK:

data_structure[y].data++;

OSAtomicIncrement32 (y);

You must use a barrier here, because and other threads the see data_structure as out of date.

However, if you are using a atomic variable for some purpose where it stands alone, your may omit the barrier:

Y is not used to access any other data

OSAtomicIncrement32 (y);

Fine, as long as the value of Y does not affect the variable of any shared data structure.

Essentially, it ' s a cache flush. You can always safely use the barrier functions, but in some cases, you may be able to improve performance by not using th e barrier functions, such as if Y is not used relative to a data structure. There is probably not many cases where can I use the functions without the barrier.


3. int int32_t int64_t


These things are caused by cross-platform programming;

The type of data, in particular int , is different in length under the platform of different bits of machine.

The C99 standard does not specify the length size of the detail data type. Only the prescribed level. Compare the following:

Level Platform

Char 1 of bytes 8 bit

Short 2 bytes in a bit

int 2 bytes in a bit

A long 4 bytes

pointer 2 bytes


Level Platform

Char 1 of bytes 8 bit

Short 2 bytes in a bit

int 4 bytes

A long 4 bytes

A long long 8 bytes

pointer 4 bytes


Position Platform

Char 1 bytes

Short 2 bytes

int 4 bytes

A long 8 bytes (difference)

A long long 8 bytes

pointer 8 bytes (difference)


Second, programming considerations

In order to ensure the universality of the platform. Try not to use long database type in the program. Ability to define with a fixed-size data type macro:

typedef signed CHAR int8_t

typedef short INT int16_t;

typedef int int32_t;

# if __wordsize = = 64

typedef long int int64_t;

# Else

__extension__

typedef long long int int64_t;

#endif


Third, using int can also use intptr_t to ensure the universality of the platform, it is compiled on different platforms with different lengths. But the standard platform length, for example, the length of the machine is 8 bytes, the length of the machine is 4 bytes, the definition of such as the following:

#if __wordsize = = 64

typedef long int intptr_t;

#else

typedef int intptr_t;

#endif

Use sizeof to calculate the size of the data type as much as possible in programming

The above type definitions have a corresponding unsigned type.

There are also ssize_t and size_t respectively sign size_t and unsigned signed size of computer word size. They are also the word length of the computer, on the four-bit machine is an int , on the four -bit machine long , in a sense they are equivalent to intptr_t and uintptr_t. They are defined inside the stddef.h.

It is important to note that the socket 's accept function is not size_t on Some operating systems because of the type of int* accepted by accept . the size_t may be of type long int . Later BSD used sock_t to replace it.

What data types are uint8_t/uint16_t/uint32_t/uint64_t

In the nesc code. You'll see a lot of data types you don't know, like uint8_t . At first glance, it's like a new data type, just C (Nesc is the extension of C ) and it doesn't seem to have this type of data! How come you are _t again? Very many people have this kind of doubt. On the Forum, someone asked: is the type with the end of *_t a long type? Baidu on the search, only to find the answer, then found that the original oneself to C too little mastery.

So what does the _t mean? The detailed official answer was not found, but I thought there was an answer that was closer. It is a structure callout that can be understood as an abbreviation of Type/typedef , indicating that it is defined by a typedef . Instead of other data types.

uint8_t. uint16_t,uint32_t , etc. are not new data types, they are just aliases for types using typedef . The trick of the new bottle of wine.

Just, don't underestimate the typedef. It will have a very good effect on your code maintenance.

For example, there is no boolin C , so in one software, some program apes use int. Some program apes use short. Will be more chaotic, it is best to use a typedef to define. Such as:
typedef char BOOL;

In general, a C project must do some of this work, as you will be involved in cross-platform. Different platforms will have different word lengths, so using precompilation and typedef will allow you to maintain your code most efficiently. For the convenience of the user,C99 standard C language hardware defines these types for us, we can use it with confidence.

according to the POSIX standard. The corresponding *_t types for general shaping are:
1 bytes uint8_t
2 bytes uint16_t
4 bytes uint32_t
8 bytes uint64_t

Thread safety-Multiple network requests under a single VC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.