C multi threaded lock, C Timer lock multi threaded A few questions.

Updated on technology 2024-02-08
4 answers
  1. Anonymous users2024-02-05

    For example, if you want to modify a collection, but the collection may have multiple threads accessing it. Threads make different changes to the collection, which can lead to unstable collection states. So, when making changes to a collection, give the thread a lock, lock it, modify the collection, and then release the lock so that other processes can continue to access the collection without problems.

    lock(lockobj)

    lockobj is the lock, the object type of the reference type object, it doesn't matter how to define it, but don't use an object of type public as a lock (this or typeof() is actually a very poor way to write, it is likely to deadlock).

    The contents of the quotation marks are locked segments during which other threads must wait and cannot enter.

    Example of how to write in a class:

    class a

    private readonly object syncroot = new object();

    public void clear()

    lock(syncroot)

    unsafeclear();

    Also, what exactly is that waiting time you are talking about?

  2. Anonymous users2024-02-04

    lock object can only be accessed by one thread at a time. If you have a thread dead in it and no exit. Then the other threads can't get in. will cause deadlocks.

    If you want to customize the time, you query ManualResetEvent and AutoResetEvent

  3. Anonymous users2024-02-03

    The lock keyword can be used to ensure that the block completes without being interrupted by other threads. This is achieved by acquiring a mutex for a given object during the block run. lock

    The statement starts with a keyword.

    lock, it has an object as a parameter, and after that argument there is a ** block that can only be executed by one thread at a time. For example: public

    classtestthreading

    For more information about ClassTest, see MSDN.

  4. Anonymous users2024-02-02

    Most of them are correct, but you don't seem to have a clear purpose for locking.

    Timer doesn't seem to have much to do with lock.

    Because lock is like a key, it needs a unique property that everyone can access, and how you can determine the label property is okay, on the contrary, I can write you the program in question. You can define a global integer cnt, the initial value is 0, open n threads, and at the same time perform the operation of obtaining and setting the text attribute for the label, add a judgment before setting, if the text is not x, then assign x, and let cnt++. In the end, it is theoretically possible for CNT to be 1, but in practice it may be more than that.

Related questions
2 answers2024-02-08

Single-threaded assembly and element models.

All parts created by Visual Basic use the unit model, regardless of whether the part is single-threaded or multi-threaded. A single-threaded assembly has only one cell, which contains all the objects provided by the assembly. >>>More