The Truth of Sisyphus
  • Introduction
  • Deep Learning
    • Basics
      • Hinge Loss
      • Regularizations
      • Linear Classification
      • Multi-Class and Cross Entropy Loss
      • Batch Norm and other Normalizations
      • Optimization
      • Optimization Functions
      • Convolution im2col
      • Activation Functions
      • Derivatives
        • Derivatives of Softmax
        • A Smooth (differentiable) Max Function
      • Model Ensemble
      • Layers Python Implementation
    • Classification
      • Mobile friendly networks
      • Non-local Neural Networks
      • Squeeze-and-Excitation Networks
      • Further Attention Utilization -- Efficience & Segmentation
      • Group Norm
      • ShuffleNet V2
    • Segmentation
      • Several Instance Segmentation
      • A Peek at Semantic Segmentation
      • Design Choices for Mobile Friendly Deep Learning Models, Semantic Segmentation
      • Efficient Video Object Segmentation via Network Modulation
      • BiSeNet
      • DeepLabV3+
    • Detection
      • CornerNet
      • IoU-Net
      • Why smooth L1 is popular in BBox Regression
      • MTCNN-NCNN
      • DetNet
      • SSD Illustration
    • RNN Related
      • GRU vs LSTM
      • BERT
    • Reinforcement Learning
      • AutoML in Practice Review
      • DRL for optimal execution of profolio transaction
    • Multi-task
      • Multi-task Overview
      • What are the tricks in Multi-Task network design?
    • Neural Network Interpretation
      • Neuron Visualization
    • Deep Learning Frameworks
      • How does Caffe work
      • [Gluon] When to use (Hybrid)Sequential and (Hybrid)Block
      • Gluon Hybrid Intro
      • Gluon HybridBlocks Walk-Through
      • A quick tour of Torch internals
      • NCHW / NHWC in Pytorch
      • Static & Dynamic Computation Graph
    • Converting Between DL Frameworks
      • Things To Be Considered When Doing Model Converting
      • Caffe to TensorFlow
    • Computation Graph Optimization
      • Two ways of TensorRT to optimize Neural Network Computation Graph
      • Customized Caffe Memory Optimization
      • NCNN Memory Optimization
      • Symbolic Programs Advantages: More Efficient, Reuse Intermediate Memory, Operation Folding
    • Deep Learning Debug
      • Problems caused by dead ReLU
      • Loss jumps to 87.3365
      • Common Causes of NANs During Training
    • Deployment
      • Efficient Convolution Operation
      • Quantization
    • What I read recently
      • Know Google the Paper Way
      • ECCV 2018
      • Neural Machine Translation
      • Street View OCR Extraction System
      • Teaching Machines to Draw
      • Pixel to Graph
      • Burst Image Deblurring
      • Material for Masses
      • Learning to Separate Object Sounds by Watching Unlabeled Video
    • Papers / Posts to be read
    • Dummy thoughts
  • Machine Learning
    • Classification
    • Regression
    • Clustering
    • Dimension Reduction
    • Metrics
    • Regularization
    • Bayesian Example
    • Machine Learning System Design
    • Recommendation
    • Essentials of Machine Learning
    • Linear Regression
    • Logistic Regression
      • Logistic Function
    • Gaussian Discriminant Analysis
    • Naive Bayes
    • SVM
    • MLE vs MAP
    • Boosting
    • Frequent Questions
    • Conclusion of Machine Learning
  • Python notes
    • Python _ or __ underscores usage
    • Python Multiprocess and Threading Differences
    • Heapq vs. Q.PriorityQueue
    • Python decorator
    • Understanding Python super()
    • @ property
    • Python __all__
    • Is Python List a Linked List or Array
    • What is the "u" in u'Hello world'
    • Python "self"
    • Python object and class
    • Python Class' Instance method, Class method, and Static Methods Demystified
    • Python WTF
    • Python find first value index in a list: [list].index(val)
    • Sort tuples, and lambda usecase
    • Reverse order of range()
    • Python check list is empty
    • Python get ASCII value from character
    • An A-Z of useful Python tricks
    • Python nested function variable scope
    • Python reverse a list
    • Python priority queue -- heapq
  • C++ Notes
    • Templates
    • std::string (C++) and char* (or c-string "string" for C)
    • C++ printf and cout
    • Class Member Function
    • Inline
    • Scope Resolution Operator ::
    • Constructor
    • Destructor
    • Garbage Collection is Critical
    • C++ Question Lists
  • Operating System
    • Basics
    • Mutex & Semaphore
    • Ticket Selling System
    • OS and Memory
    • Sort implementation in STL
    • Compile, link, loading & run
    • How to understand Multithreading and Multiprocessing from the view of Operating System
  • Linux & Productivity
    • Jupyter Notebook on Remote Server
    • Nividia-smi monitoring
  • Leetcode Notes
    • Array
      • 11. Container With Most Water
      • 35. Search Insert Position
    • Linked List
      • Difference between Linked List and Array
      • Linked List Insert
      • Design of Linked List
      • Two Pointers
        • 141. Linked List Cycle
        • 142. Linked List Cycle II
        • 160. Intersection of two Linked List
        • 19. Remove N-th node from the end of linked list
      • 206. Reverse Linked List
      • 203. Remove Linked List Elements
      • 328. Odd Even Linked List
      • 234. Palindrome Linked List
      • 21. Merge Two Sorted Lists
      • 430. Flatten a Multilevel Doubly Linked List
      • 430. Flatten a Multilevel Doubly Linked List
      • 708. Insert into a Cyclic Sorted List
      • 138. Copy List with Random Pointer
      • 61. Rotate List
    • Binary Tree
      • 144. Binary Tree Preorder Traversal
      • 94. Binary Tree Iterative In-order Traverse
    • Binary Search Tree
      • 98. Validate Binary Search Tree
      • 285. Inorder Successor in BST
      • 173. Binary Search Tree Iterator
      • 700. Search in a Binary Search Tree
      • 450. Delete Node in a BST
      • 701. Insert into a Binary Search Tree
      • Kth Largest Element in a Stream
      • Lowest Common Ancestor of a BST
      • Contain Duplicate III
      • Balanced BST
      • Convert Sorted Array to Binary Search Tree
    • Dynamic Programming
      • 198. House Robber
      • House Robber II
      • Unique Path
      • Unique Path II
      • Best time to buy and sell
      • Partition equal subset sum
      • Target Sum
      • Burst Ballons
    • DFS
      • Clone Graph
      • General Introduction
      • Array & String
      • Sliding Window
  • Quotes
    • Concert Violinist Joke
    • 船 Ship
    • What I cannot create, I do not understand
    • Set your course by the stars
    • To-do list
Powered by GitBook
On this page
  • C++
  • Demo1: Create a basic thread
  • Demo2: Passing arguments into thread
  • Demo3: Thread synchronization
  • Demo 4: Ticket Selling System
  • Python
  1. Operating System

Ticket Selling System

C++

Demo1: Create a basic thread

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;

//线程函数
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    for (int i = 0; i < 5; ++ i)
    {
        cout << "子线程:i = " << i << endl;
        Sleep(100);
    }
    return 0L;
}

int main()
{
    //创建一个线程
    HANDLE thread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
    //关闭线程
    CloseHandle(thread);

    //主线程的执行路径
    for (int i = 0; i < 5; ++ i)
    {
        cout << "主线程:i = " << i << endl;
        Sleep(100);
    }

    return 0;
}

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!
结果如下: 
主线程:i = 0 
子线程:i = 0 
主线程:i = 1 
子线程:i = 1 
子线程:i = 2 
主线程:i = 2 
子线程:i = 3 
主线程:i = 3 
子线程:i = 4 
主线程:i = 4

Demo2: Passing arguments into thread

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;

#define NAME_LINE   40

//定义线程函数传入参数的结构体
typedef struct __THREAD_DATA
{
    int nMaxNum;
    char strThreadName[NAME_LINE];

    __THREAD_DATA() : nMaxNum(0)
    {
        memset(strThreadName, 0, NAME_LINE * sizeof(char));
    }
}THREAD_DATA;



//线程函数
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    THREAD_DATA* pThreadData = (THREAD_DATA*)lpParameter;

    for (int i = 0; i < pThreadData->nMaxNum; ++ i)
    {
        cout << pThreadData->strThreadName << " --- " << i << endl;
        Sleep(100);
    }
    return 0L;
}

int main()
{
    //初始化线程数据
    THREAD_DATA threadData1, threadData2;
    threadData1.nMaxNum = 5;
    strcpy(threadData1.strThreadName, "线程1");
    threadData2.nMaxNum = 10;
    strcpy(threadData2.strThreadName, "线程2");

//创建第一个子线程
    HANDLE hThread1 = CreateThread(NULL, 0, ThreadProc, &threadData1, 0, NULL);
    //创建第二个子线程
    HANDLE hThread2 = CreateThread(NULL, 0, ThreadProc, &threadData2, 0, NULL);
    //关闭线程
    CloseHandle(hThread1);
    CloseHandle(hThread2);

    //主线程的执行路径
    for (int i = 0; i < 5; ++ i)
    {
        cout << "主线程 === " << i << endl;
        Sleep(100);
    }

    system("pause");
    return 0;
}

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

结果

主线程 === 线程1 — 0 
0 
线程2 — 0 
线程1 — 1 
主线程 === 1 
线程2 — 1 
主线程 === 2 
线程1 — 2 
线程2 — 2 
主线程 === 3 
线程2 — 3 
线程1 — 3 
主线程 === 4 
线程2 — 4 
线程1 — 4 
线程2 — 5 
请按任意键继续… 线程2 — 6 
线程2 — 7 
线程2 — 8 
线程2 — 9

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

CreateMutex、WaitForSingleObject、ReleaseMutex

从【Demo2】中可以看出,虽然创建的子线程都正常执行起来了,但输出的结果并不是我们预期的效果。我们预期的效果是每输出一条语句后自动换行,但结果却并非都是这样。这是因为在线程执行时没有做同步处理,比如第一行的输出,主线程输出“主线程 ===”后时间片已用完,这时轮到子线程1输出,在子线程输出“线程1 —”后时间片也用完了,这时又轮到主线程执行输出“0”,之后又轮到子线程1输出“0”。于是就出现了“主线程 === 线程1 — 0 0”的结果。

主线程:cout << “主线程 === ” << i << endl; 子线程:cout << pThreadData->strThreadName << ” — ” << i << endl;

为避免出现这种情况,我们对线程做一些简单的同步处理,这里我们用互斥量(Mutex)

Demo3: Thread synchronization

#include "stdafx.h"
#include <windows.h>
#include <iostream>

#define NAME_LINE   40

//定义线程函数传入参数的结构体
typedef struct __THREAD_DATA
{
    int nMaxNum;
    char strThreadName[NAME_LINE];

    __THREAD_DATA() : nMaxNum(0)
    {
        memset(strThreadName, 0, NAME_LINE * sizeof(char));
    }
}THREAD_DATA;

HANDLE g_hMutex = NULL;     //互斥量

//线程函数
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
    THREAD_DATA* pThreadData = (THREAD_DATA*)lpParameter;

    for (int i = 0; i < pThreadData->nMaxNum; ++ i)
    {
        //请求获得一个互斥量锁
        WaitForSingleObject(g_hMutex, INFINITE);
        cout << pThreadData->strThreadName << " --- " << i << endl;
        Sleep(100);
        //释放互斥量锁
        ReleaseMutex(g_hMutex);
    }
    return 0L;
}

int main()
{
    //创建一个互斥量
    g_hMutex = CreateMutex(NULL, FALSE, NULL);

    //初始化线程数据
    THREAD_DATA threadData1, threadData2;
    threadData1.nMaxNum = 5;
    strcpy(threadData1.strThreadName, "线程1");
    threadData2.nMaxNum = 10;
    strcpy(threadData2.strThreadName, "线程2");


    //创建第一个子线程
    HANDLE hThread1 = CreateThread(NULL, 0, ThreadProc, &threadData1, 0, NULL);
    //创建第二个子线程
    HANDLE hThread2 = CreateThread(NULL, 0, ThreadProc, &threadData2, 0, NULL);
    //关闭线程
    CloseHandle(hThread1);
    CloseHandle(hThread2);

    //主线程的执行路径
    for (int i = 0; i < 5; ++ i)
    {
        //请求获得一个互斥量锁
        WaitForSingleObject(g_hMutex, INFINITE);
        cout << "主线程 === " << i << endl;
        Sleep(100);
        //释放互斥量锁
        ReleaseMutex(g_hMutex);
    }

    system("pause");
    return 0;
}

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

结果

主线程 === 0 
线程1 — 0 
线程2 — 0 
主线程 === 1 
线程1 — 1 
线程2 — 1 
主线程 === 2 
线程1 — 2 
线程2 — 2 
主线程 === 3 
线程1 — 3 
线程2 — 3 
主线程 === 4 
线程1 — 4 
请按任意键继续… 线程2 — 4 
线程2 — 5 
线程2 — 6 
线程2 — 7 
线程2 — 8 
线程2 — 9

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

为进一步理解线程同步的重要性和互斥量的使用方法,我们再来看一个例子。

买火车票是大家春节回家最为关注的事情,我们就简单模拟一下火车票的售票系统(为使程序简单,我们就抽出最简单的模型进行模拟):有500张从北京到赣州的火车票,在8个窗口同时出售,保证系统的稳定性和数据的原子性。

Demo 4: Ticket Selling System

SaleTickets.h :

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <strstream> 
#include <string>

using namespace std;

#define NAME_LINE   40

//定义线程函数传入参数的结构体
typedef struct __TICKET
{
    int nCount;
    char strTicketName[NAME_LINE];

    __TICKET() : nCount(0)
    {
        memset(strTicketName, 0, NAME_LINE * sizeof(char));
    }
}TICKET;

typedef struct __THD_DATA
{
    TICKET* pTicket;
    char strThreadName[NAME_LINE];

    __THD_DATA() : pTicket(NULL)
    {
        memset(strThreadName, 0, NAME_LINE * sizeof(char));
    }
}THD_DATA;


 //基本类型数据转换成字符串
template<class T>
string convertToString(const T val)
{
    string s;
    std::strstream ss;
    ss << val;
    ss >> s;
    return s;
}

//售票程序
DWORD WINAPI SaleTicket(LPVOID lpParameter);

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

SaleTickets.cpp :

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include "SaleTickets.h"

using namespace std;

extern HANDLE g_hMutex;

//售票程序
DWORD WINAPI SaleTicket(LPVOID lpParameter)
{

    THD_DATA* pThreadData = (THD_DATA*)lpParameter;
    TICKET* pSaleData = pThreadData->pTicket;
    while(pSaleData->nCount > 0)
    {
        //请求获得一个互斥量锁
        WaitForSingleObject(g_hMutex, INFINITE);
        if (pSaleData->nCount > 0)
        {
            cout << pThreadData->strThreadName << "出售第" << pSaleData->nCount -- << "的票,";
            if (pSaleData->nCount >= 0) {
                cout << "出票成功!剩余" << pSaleData->nCount << "张票." << endl;
            } else {
                cout << "出票失败!该票已售完。" << endl;
            }
        }
        Sleep(10);
        //释放互斥量锁
        ReleaseMutex(g_hMutex);
    }

    return 0L;
}

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

测试程序:

//售票系统
void Test2()
{
    //创建一个互斥量
    g_hMutex = CreateMutex(NULL, FALSE, NULL);

    //初始化火车票
    TICKET ticket;
    ticket.nCount = 100;
    strcpy(ticket.strTicketName, "北京-->赣州");

    const int THREAD_NUMM = 8;
    THD_DATA threadSale[THREAD_NUMM];
    HANDLE hThread[THREAD_NUMM];
    for(int i = 0; i < THREAD_NUMM; ++ i)
    {
        threadSale[i].pTicket = &ticket;
        string strThreadName = convertToString(i);

        strThreadName = "窗口" + strThreadName;

        strcpy(threadSale[i].strThreadName, strThreadName.c_str());

        //创建线程
        hThread[i] = CreateThread(NULL, NULL, SaleTicket, &threadSale[i], 0, NULL);

        //请求获得一个互斥量锁
        WaitForSingleObject(g_hMutex, INFINITE);
        cout << threadSale[i].strThreadName << "开始出售 " << threadSale[i].pTicket->strTicketName << " 的票..." << endl;
        //释放互斥量锁
        ReleaseMutex(g_hMutex);

        //关闭线程
        CloseHandle(hThread[i]);
    }

    system("pause");
}

--------------------- 

作者:luoweifu 

来源:CSDN 

原文:https://blog.csdn.net/luoweifu/article/details/46835437 

版权声明:本文为博主原创文章,转载请附上博文链接!

结果:

窗口0开始出售 北京–>赣州 的票… 
窗口0出售第100的票,出票成功!剩余99张票. 
窗口1开始出售 北京–>赣州 的票… 
窗口1出售第99的票,出票成功!剩余98张票. 
窗口0出售第98的票,出票成功!剩余97张票. 
窗口2开始出售 北京–>赣州 的票… 
窗口2出售第97的票,出票成功!剩余96张票. 
窗口1出售第96的票,出票成功!剩余95张票. 
窗口0出售第95的票,出票成功!剩余94张票. 
窗口3开始出售 北京–>赣州 的票… 
窗口3出售第94的票,出票成功!剩余93张票. 
窗口2出售第93的票,出票成功!剩余92张票. 
窗口1出售第92的票,出票成功!剩余91张票. 
窗口0出售第91的票,出票成功!剩余90张票. 
窗口4开始出售 北京–>赣州 的票… 
窗口4出售第90的票,出票成功!剩余89张票.
窗口3出售第89的票,出票成功!剩余88张票. 
窗口2出售第88的票,出票成功!剩余87张票. 
窗口1出售第87的票,出票成功!剩余86张票.
窗口0出售第86的票,出票成功!剩余85张票. 
窗口5开始出售 北京–>赣州 的票… 
窗口5出售第85的票,出票成功!剩余84张票. 
窗口4出售第84的票,出票成功!剩余83张票. 
窗口3出售第83的票,出票成功!剩余82张票.

Python

多线程模拟卖票系统 主要知识点就是 线程的创建(提供了两种方式一个是通过threading.Thread()方法,一个是用过Thread类创建)和线程锁(threading.Lock())

第一种方法:通过Thread类模拟

import os
import threading

tickis=1000  # 声明一个全局变量(全局需要共享数据)存储一辆列车的总票数

# 因为数据存在安全性问题 保证卖票线程不会抢买同一张票 导致售票出问题就需要给数据上锁 从而保证在该票
# 卖了后 再卖其他的
lock=threading.Lock() # threading 提供了锁工具 同样要声明为全局变量

# 定义一个干事情即卖票的函数
def sale_tickis(thread_name):

    global tickis #函数里共享全局变量 需用关键字global声明 否则访问不到
    global lock
    # 卖票
    # 操作数据之前就需要给数据上锁
    while 1:
        lock.acquire()
        if tickis!=0:
            tickis-=1
            print(thread_name,"余票为:",tickis)
        else:
            print(thread_name,"票卖完了")
            os._exit(0)   # "0" 表示安全退出 "1"或其他数字表示非正常退出

    # 操作完数据后要释放锁 这样后面才能继续卖票 否则数据锁定则无法卖票
        lock.release()

# 定义一个类创建卖票线程 该类继承自threading.Thread类
class my_thread(threading.Thread):

    def __init__(self,name=""):
        # super(threading.Thread,self).__init__()
        threading.Thread.__init__(self)
        self.name=name
    # 重写Thread类的run()方法以创建线程
    def run(self):
        sale_tickis(self.name) #调用卖票方法

# 初始化类创建线程
if __name__=="__main__":
    for i in range(1,21):
        thread = my_thread("线程" + str(i))
        thread.start()  #开启线程

--------------------- 

作者:BroStayHungry 

来源:CSDN 

原文:https://blog.csdn.net/qq_25279303/article/details/80315833 

版权声明:本文为博主原创文章,转载请附上博文链接!

第二种方法:通过threading.Thread()方法模拟

# 创建线程 通过threading.thread方法
for i in range(1,20):
    new_thread=threading.Thread(target=sale_tickis,args=("线程"+str(i),))
    new_thread.start()
--------------------- 
作者:BroStayHungry 
来源:CSDN 
原文:https://blog.csdn.net/qq_25279303/article/details/80315833 
版权声明:本文为博主原创文章,转载请附上博文链接!

PreviousMutex & SemaphoreNextOS and Memory

Last updated 5 years ago

LogoMultiprocessing vs. Threading in Python: What you need to know.Timber.io