惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 说不得

折腾了两天的跨站脚本提交问题,与IIS7有关 MongoDB 服务启动时指定dbpath DataGridView绑定行号 【转帖】三种决不能放进数据库的东西 Func<T, TResult>的一个使用场合 没别的,来张截图好了 SqlBulkCopy使用心得 VS2010 出现错误提示“Error Creating Control - Object reference not set to an instance of an objec” 的解决方法 SQLite.Net操作类 Win7 下使用 SQLite Expert 操作C盘下的数据库发生错误:Attempt to write a readonly database. C#查找某一窗口并按钮 再见,Google 使用ManagedSpyLib监视.net程序中控件属性的变化 计算两张黑白图片的相似度 使用c#把一个32位整数按位相加最快的方法是什么? 关于变量在循环内声明还是在循环外声明 轻量级分页控件 安装sqlserver2008时出现“Rule "Previous releases of Microsoft Visual Studio 2008" failed.”错误的解决办法 最近见过的垃圾代码 - 说不得 - 博客园
使用两个信号量实现主线程和线程池同步
说不得 · 2011-05-21 · via 博客园 - 说不得

目前项目里面有个需求,需要多线程操作数据库,等待数据库操作完毕之后,主线程才能继续执行以后的操作。

想了想,最后决定使用两个信号量来完成这个需求。

具体需求如下 :

  1. 操作数据库的线程最多20个
  2. 主线程必须等待所有操作数据库的线程返回后,才能进行下一步操作
具体操作步骤如下:

  1. 声明一个最大值为20,初始值为20的信号量s1和一个最大值为1,初始值为0的信号量s2,以及一个任务计数器count。
  2. 将需要线程池操作的任务数赋给count。 
  3. 在调用线程池的循环中,调用s1的WaitOne方法,在线程的具体操作执行完毕后调用s1的Release方法,并且使用Interlocked的Decrement方法将任务数count减1,并且判断count是否等于0,如果count等于0,调用s2的Release方法。
  4. 最后在调用线程池的循环结束之后,调用s2的WaitOne方法。

运行截图如下:


具体代码如下:

 using System;

using System.Threading;
using System.Collections.Generic;public class UseSemaphore
{
    
private static readonly Semaphore s1 = new Semaphore(3,3);
    
private static readonly Semaphore s2 = new Semaphore(0,1);
    
private const int TaskCount = 10;
    
private static int count;public static void Main()
    {
        Console.WriteLine(
"MainThread Start");
        count 
= TaskCount;
        
for(int i = 0; i < TaskCount; ++i)
        {
            s1.WaitOne();
            ThreadPool.QueueUserWorkItem(Process, 
null);
        }
        s2.WaitOne();
        Console.WriteLine(
"MainThread");
        Console.ReadKey();
    }
    
    
private static void Process(object obj)
    {
        Console.WriteLine(
"ThreadPool {0} Start", Thread.CurrentThread.ManagedThreadId);
        Thread.Sleep(
100);
        Console.WriteLine(
"ThreadPool {0} Stop", Thread.CurrentThread.ManagedThreadId);
        s1.Release();
        Interlocked.Decrement(
ref count);
        
if(count == 0)
        {
            Console.WriteLine(
"s2 Release");
            s2.Release();
        }
    }
}