In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you Perl thread life cycle example analysis, I hope you have something to gain after reading this article, let's discuss it together!
Sharing and synchronization
threads::shared
Unlike most existing Perl threading models, in the Perl threads model, no data structure is shared by default. When a new Perl thread is created, it already contains a private copy of all the current data structures, and any operations on this copy of the data structure in the new Perl thread will not be valid in other Perl threads. Therefore, if any shared data needs to be used, it must be explicitly stated. The threads::shared package can be used for the purpose of sharing data between Perl threads.
lock
Since there is shared data between multiple Perl threads, it is important to access shared data carefully, otherwise conflicts are inevitable. The lock method built into the Perl threading model implements a locking mechanism for sharing data between Perl threads. Interestingly, there is no unlock method to explicitly unlock, and the life cycle of a lock is block-wise, that is, when the lock is implicitly released after the execution of the block in which the lock is performed. for example
Listing 10. Locking mechanisms in Perl threads
usethreads::shared; #inthread1 { lock($share);#lockfor3seconds sleep(3);#otherthreadscannotlockagain } #unlockimplicitlynowaftertheblock #inthread2 { lock($share);#willbeblocked,asalreadylockedbythread1 $share++;#afterthread1quitfromtheblock } #unlockimplicitlynowaftertheblock
In the example above, we locked an ordinary scalar using the lock method in thread1, which causes thread2 to block while trying to acquire the lock of the $share variable. When thread1 exits from the code block calling lock, the lock is implicitly released, so thread2 blocks. After the lock succeeds, thread2 can perform the $share++ operation. For arrays and hash tables, lock must be applied to the entire data structure, not to an element of the array or hash table.
If a Perl thread locks a shared variable before it releases the lock, if another Perl thread locks the shared variable, the Perl thread blocks. Blocking is not aborted automatically, but until the lock is released by the previous Perl thread. This pattern leads to our common deadlock problem.
for example
Listing 12. Deadlocks in Perl threads
usethreads; usethreads::shared; #inthread1 { lock($a);#lockfor3seconds sleep(3);#otherthreadscannotlockagain lock($b);#deadlockhere } #inthread2 { lock($b);#willbeblocked,asalreadylockedbythread1 sleep(3);#afterthread1quitfromtheblock lock($a);#deadlockhere }
Deadlocks are often the most insidious problems in multithreaded Perl programs, often difficult to detect and debug, and increase the difficulty of troubleshooting. In order to avoid deadlock problems in the program, we should try to avoid acquiring locks for multiple shared variables at the same time in the program. If it cannot be avoided, we should try to use the same order to acquire locks for multiple shared variables, and we should also refine the granularity of locking as much as possible to reduce the locking time.
semaphore
The Thread::Semaphore package provides semaphore support for Perl threads. You can create your own semaphore and synchronize access to resources with down and up operations. In fact, down and up operations correspond to what we know as P and V operations. Internally, Thread::Semaphore is essentially a locked shared variable that is encapsulated in a Perl thread-safe package. Because semaphores don't have to be tied to any variables, they are flexible enough to control any data structure and program behavior you want to synchronize. for example
Listing 13. Semaphores in Perl threads
usethreads; usethreads::shared; useThread::Semaphore; my$s=Thread::Semaphore->new(); $s->down();#Poperation ... $s->up();#Voperation
Essentially, a semaphore is a reference to a shared integer variable. By default, its initial value is 1, the down operation decrements its value by 1, and the up operation increments its value by 1. Of course, you can also customize the initial semaphore value and how the semaphore changes with each up or down operation.
Perl thread queue
The Thread::Queue package provides Perl thread-safe queue support for Perl threads. Similar to semaphores, internally Thread::Queue encapsulates a shared queue accessed synchronously through a lock mechanism into a Perl thread-safe package and provides a uniform interface for use. Thread::Queue can greatly simplify the difficulty and cost of Perl thread communication in some cases. For example, in the producer-consumer model, the producer can constantly enqueue on the Perl thread queue, while the consumer only needs to constantly dequeue on the Perl thread queue, which easily realizes the synchronization problem between the producer and the consumer.
Other useful non-core packages
Everything discussed earlier in this article falls within the scope of the Perl Thread Core package. There are other non-core packages on CPAN that are related to Perl threads, and they often bring great convenience to the use of Perl threads. Here, we will select two to introduce them.
The Thread::Pool package allows you to create a batch of Perl threads in your program to perform multiple similar tasks. For example, if you want to create a multithreaded Perl program to perform the task of checking whether 1000 ip addresses can be pinged, the Thread::Pool package is handy.
The Thread::RWLock package provides locking support for read and write operations in Perl threads. For example, if you have multiple reader and writerPerl threads accessing a file or files in common, the Thread::RWLock package can be handy.
After reading this article, I believe you have a certain understanding of "Perl thread life cycle example analysis", if you want to know more related knowledge, welcome to pay attention to the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.