From 78de56b9f2d862bbdac8a02a72dd95500b7ef83e Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Mon, 5 Aug 2024 16:59:51 +0800 Subject: Try t use coroutine, but starvation --- src/mongo.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/mongo.go') diff --git a/src/mongo.go b/src/mongo.go index 54f9533..3a23131 100644 --- a/src/mongo.go +++ b/src/mongo.go @@ -2,6 +2,8 @@ package main import ( "context" + "fmt" + "sync" "time" "go.mongodb.org/mongo-driver/bson" @@ -13,6 +15,8 @@ type mongoClient struct { dbName, colName string client *mongo.Client col *mongo.Collection + Mutex sync.Mutex + // Attention: 这把锁是否有必要? } func (mc *mongoClient) init(dbName, colName string) error { @@ -89,3 +93,12 @@ func (mc *mongoClient) Disconnect() error { mc.colName = "" return nil } + +func (mc *mongoClient) GetLock() { + for i := 0; i < 20000; { + if !mc.Mutex.TryLock() { + i++ + } + } + fmt.Printf("Die...\n") +} -- cgit v1.2.3-70-g09d2