C # at a certain time, only n threads are concurrently executed, and the rest are implemented in the queue.

Source: Internet
Author: User
C # provides two thread-related classes, mutex and interlocked, all in the threading namespace ~!

Mutex provides two instance methods: witeone and releasemutex ~

Witeone is used to "block the current thread and provide atomic operations on the thread"

That is to say, when a thread encounters witeone, if there is no thread in witeone to operate, the thread goes in and operates.

When there is a thread in it, all the threads to this end need to wait in queue for the thread to complete the execution ~

The end mark for controlling such operations is to use the releasemutex method!

Just like witeone is a lock ~ Releasemutex is a key.

When 10 people see this door, the first person who arrives at the door will see no one in the room, then he will go in and lock the door at the same time ~

The person behind will naturally wait at the door. When this person finishes the task in the room, he will open the door with the key!

After going out, the lock will be handed over to the second comrade in the queue at the door. The second comrade will perform the same operation.

If the first comrade does not give the right to use the task to the second person after executing the task, he will quit directly.

The room is naturally empty, and the door is locked ~ Don't worry ~ The door will automatically open, as long as the previous person is no longer in the room ~

Next, let's talk about interlocked. The official explanation is "increment or decrease an atomic operation on a variable and save it"

The concept of atomic operations is that there is only one thread operating on this variable ~ Operations not allowed by other threads

When an atomic operation is performed on a variable, the variable is locked, and other threads cannot access the variable. They can only wait for the variable to be unlocked.

I feel that mutex is actually used for implementation.

Let's start with the specific implementation.

Public class mutextest
{
Private Static int poolflag = 0; // tag
Private const int amountthread = 10; // total number of threads
Private const int maxthread = 3; // maximum number of executable threads
Private Static mutex muxconsole = new mutex ();

Public static void main ()
{
For (INT I = 0; I <amountthread; I ++)
{
// Create a specified number of threads
// A thread calls the run method.
// Start the thread
Thread TRD = new thread (New threadstart (run ));
TRD. Name = "Thread" + I;
TRD. Start ();
}
}

Public static void run ()
{

Muxconsole. waitone (); // block the queue
Interlocked. increment (ref poolflag); // tag + 1
If (poolflag! = Maxthread) // determines whether it is equal to the upper limit
Muxconsole. releasemutex (); // If the thread does not reach the maximum number of executable threads, activate the thread to bring the subsequent threads in.
Console. writeline ("{0} is running... \ n", thread. currentthread. Name );
Thread. Sleep (5000); // simulate execution
Console. writeline ("{0} has aborted... \ n", thread. currentthread. Name );

// Mark-1
interlocked. decrement (ref poolflag);
}< BR >}

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.