【博客324】one thread one loop

内容: 记录一种编程逻辑:one thread one loop

one thread one loop:

一个网络IO线程一个循环,循环里面做着一样的事情

简述:网络模型中工作线程的一种逻辑,当主线程accept接受新连接后,以一定的负载均衡规则将
     连接派发到工作线程中去处理,此时工作线程执行的逻辑就是:one thread one loop。

one thread one loop的基本逻辑框架:

void thread_func(void* thread_arg)
{
   
 //初始化工作

 while (线程退出标志)   //用于main线程停止工作线程时使用
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
session1 【Objective and Requirement】 Objective: Be familiar with the creation of process and thread on Windows. Requirement: Sub Task 1: Create a console application, "child", which keeps printing out "The child is talking at [system time]" (in a loop, one per 1s). Sub Task 2: Create another console application, "parent". It create a child process to execute “child”. The "parent" process keeps printing out "The parent is talking at [system time]". (one per 1s) Execute "parent" and explain the output you see. Sub Task 3: Create a "winPS" program which can output information about all the running processes. Try to output details about each process, such as PID, executable file name and path, etc. Sub Task 4: In the "mainThread" program, use "CreateThread" to create a child thread. Both the main thread and the child thread keep printing out "[ThreadID] + [System time]". session2 Objective: Create "ps" and "kill" commands on Windows. Requirement: Sub Task 1: On Linux/Unix there are a "ps" command and a "kill" command. The "ps" command can list information about all the running processes. The "kill" command can terminate processes. Based on the tasks finished in Session 1, create "ps" and "kill" commands on Windows. Tips: using "TerminateProcess" to "kill" a process. session3 Objective: Learn how to use semaphore to solve IPC problems. Requirement: Task 3.1. Sleeping barber Use semaphores to solve the problem of sleeping barber. Task 3.2. Reader & Writer Use semaphores to solve the reader and writer problem, with the readers (and writers) have higher priority. session4 Title: Upgrade Linux/Unix commands Problem: Write a program "supershell" that takes another command as an argument and executes that command. For instance, executing: “./supershell cat /usr/greg/readme" would invoke the cat command on the file /usr/greg/readme. After execution of the specified command has completed, "supershell" should display statistics that show some of the system resources the co
以下是Android中C++类Thread的部分源码,具体实现可以在Android源码中的`frameworks/native/libs/nativebase/include/utils/Thread.h`文件中找到: ```cpp class Thread : public Runnable { public: Thread(); virtual ~Thread(); // Launch a new thread to execute the runnable object. virtual status_t run(const char* name = 0, int32_t priority = PRIORITY_DEFAULT, size_t stack = 0); // Ask the currently running thread to stop. This is called from a different thread. virtual status_t requestExitAndWait(); // Ask the currently running thread to stop. This is called from within the thread to be stopped. virtual status_t requestExit(); // Good place to do one-time initializations. virtual bool threadLoop() = 0; // Return the thread's name. virtual const char* name() const; // Return the thread's priority. virtual int32_t getPriority() const; // Set the thread's priority. virtual status_t setPriority(int32_t priority); // Set the thread's scheduling policy. virtual status_t setSchedulingPolicy(int32_t policy); // Get the thread's scheduling policy. virtual int32_t getSchedulingPolicy() const; // Get the thread's unique identifier. virtual pid_t getTid() const; // Sleeps for the specified number of milliseconds. static void sleep(uint32_t ms); protected: // Exit the thread. virtual void onExit(); // Called before the thread starts executing. virtual bool readyToRun(); // Return the thread's ID. pthread_t id() const; // Return the thread's JNIEnv. JNIEnv* env() const; // Return the thread's JavaVM. JavaVM* javaVM() const; private: class DeathRecipient : public IBinder::DeathRecipient { public: DeathRecipient(Thread* recipient) : mRecipient(recipient) {} virtual ~DeathRecipient() {} virtual void binderDied(const wp<IBinder>& who); private: wp<Thread> mRecipient; }; static int _threadLoop(void* user); int threadLoop(); pthread_t mThread; volatile bool mRunning; volatile bool mExited; bool mNeedToExit; bool mHasStarted; bool mSuccessfullyStartedException; Mutex mLock; Condition mThreadExitedCondition; const char* mName; int32_t mPriority; size_t mStackSize; pid_t mTid; JavaVM* mJavaVM; DeathRecipient* mDeathRecipient; }; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值