![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
win32多线程程序设计
文章平均质量分 74
午后小夕
趁着青春,尽情挥洒,加油
展开
-
索求和释放“各个锁定”
注:调用 My WaitForSingleObject()会做出锁定操作,调用ReleaseMutex()或RelaeseSemaphore()则会做出解除锁定的操作。 BOOL AcquireReadLock(RWLock *pLock) { BOOL result = TRUE; if (!MyWaitForSingleObject(pLock->hMutex)) return F原创 2015-07-26 19:57:43 · 604 阅读 · 0 评论 -
拥有主消息循环的worker线程
/*Demonstrate using worker threads that have their own message queue but no window. */ #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include "MeVVerify.h" uns原创 2015-07-31 10:44:41 · 401 阅读 · 0 评论 -
非常非常小的DLL源代码
只要DllMain()被调用,它便印出状态消息,它也提供一个输出函数,TheFunction()。程序可以动态链接之并调用之。这一版的DllMain()并没有做任何实际工作,只是打印状态消息而已。 /*demonstrate a very simple DLL that prints status messages when its functions are called a prov原创 2015-07-31 09:01:13 · 608 阅读 · 0 评论 -
线程局部存储(TLS)
下例示范如何设定DLL,使之支持TLS. #include //This is the shared slot static DWORD gdwTlsSlot; BOOL DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID lpReserved) { LPVOID lpData; UNREFERENCED_PARAMETER(hin原创 2015-07-31 11:29:21 · 1039 阅读 · 0 评论 -
进程和内存相关
进程包含内存和资源。 进程中内存内存大致分为三种类型: Code Data Stack原创 2015-07-21 19:50:16 · 433 阅读 · 0 评论 -
numbers.c---一个真正运转的多线程程序
启动5个线程,并且分别交给它们参数0~4。每一个线程打印其参数10次。 #define WIN32_LEAN_AND_MEAN #include #include #include DWORD WINAPI ThreadFunc(LPVOID); int main() { HANDLE hThrd; DWORD threadId; int i; for (i = 0;原创 2015-07-22 10:26:45 · 479 阅读 · 0 评论 -
exitcode.c---示范GetExitCodeThread()的用法
/*Start tow threads and try to exit when the user presses a key*/ #define WIN32_LEAN_AND_MEAN #include #include #include #include DWORD WINAPE ThreadFunc(LPVOID); int main() { HANDLE原创 2015-07-22 11:04:43 · 5365 阅读 · 0 评论 -
EXITTHRD.C--示范ExitThread()
#define WIN32_LEAN_AND_MEAN #include #include #include DWORD WINAPI ThreadFunc(LPVOID); void AnotherFunc(void); int main() { HANDLE hThrd; DWORD exitCode = 0; DWORD threadId; hThrd原创 2015-07-22 14:32:28 · 2584 阅读 · 0 评论 -
windows下的两个等待函数
windows下的两个等待技术 第一种: Win32 Sleep()函数 这个函数要求操作系统中止线程动作,直到读过某个指定的时间之后才恢复。能在某个线程结束时(而不是某段时间结束时)被调用。 第二种:busy loop(busy waits) 不断调用GetExitCodeThread(),直到其结果不再是STILL_ACTIVE. 缺点:浪费CPU时间原创 2015-07-23 11:18:37 · 1270 阅读 · 0 评论 -
TaskQueS.c---分配工作并以WaitForSingleObject()等待之
/* Call ThreadFunc NUM_TASKS times,using no more than THREAD_POOL_SIZE threads.Thiss version uses WaitForSingleObject,which gives a very suboptimal solution.*/ //busywait.c /*Domonstrate the eff原创 2015-07-23 14:18:23 · 1133 阅读 · 0 评论 -
WaitForMultipleObjects()函数
列表:一个主消息循环,内含MsgWaitForMultipleObjects() DWORD nWaitCount; HANDLE hWaitArray[4]; BOOL quit; int exitCode; while (!quit) { MSG msg; int rc; rc = MsgWaitForMultipleObjects( nWaitCount, h原创 2015-07-23 19:50:34 · 850 阅读 · 0 评论 -
链表,配合critical section
#include typedef struct _Node { struct _Node *next; int data; } Node; typedef struct _List { Node *head; CRITICAL_SECTION critical_sec; } List; List *CreateList() { List *pList =原创 2015-07-24 09:29:13 · 479 阅读 · 0 评论 -
互斥器(mutex)
mutext:在一个时间内只能够有一个线程拥有mutex,就好像同一个时间内只能够有一个线程进入同一个critical section 一样。 Mutex是一个核心对象,因此它被保存在系统核心之中,并且和其他核心对象一样,有所谓的引用计数。原创 2015-07-24 10:09:37 · 881 阅读 · 0 评论 -
使用WaitForMultipleObjects()修正SwapLists
#include struct Node { struct Node *next; int data; } ; struct List { struct Node *head; HANDLE hMutex; } ; struct List *CreateList() { List *list = (List*)malloc(sizeof(stru原创 2015-07-24 12:52:19 · 333 阅读 · 0 评论 -
同步机制摘要
原创 2015-07-24 13:56:03 · 429 阅读 · 0 评论 -
小驱动程序Mainl
主线程会自动附着(attaches)DLL,因为程序和这个DLL链接在一起,当程序开始执行,另一个线程被产生,调用DLL中的TheFunction(),然后程序结束。 /*Driver to load the simple DLL, create a thread,call a function in the DLL,and exit.*/ #define WIN32_LEAN_AND_ME原创 2015-07-31 09:41:35 · 1571 阅读 · 0 评论 -
string类中使用CriticalSection类
临界区类: #include class CriticalSection { public: CriticalSection(); ~CriticalSection(); void Enter(); void Leave(); private: CRITICAL_SECTION m_CritSect; }; CriticalSection::CriticalSectio原创 2015-07-28 14:23:38 · 530 阅读 · 0 评论 -
Readers/Writers lock
Readers/Writers lock数据结构: READWRIT的InitRWLock() BOOL InitRWLock(RWLock *pLock) { pLock->nReaderCount = 0; pLock->hDataLock = CreateSemaphore(NULL, 1, 1, NULL); if (pLock->hDataLock == NULL原创 2015-07-26 19:37:28 · 584 阅读 · 0 评论 -
服务器的线程模型
使用一个completion port的快速摘要。原创 2015-07-25 20:18:09 · 374 阅读 · 0 评论 -
Overlapped I/O with APCs
原创 2015-07-26 19:34:58 · 368 阅读 · 0 评论 -
IOBYEVNT.C-----overlapped I/O with signaled everts
使用event对象搭配overlapped I/O,你就可以对同一个文件发出多个读取操作和多个写入操作,每一操作有自己的event对象;然后再调用WaitForMultipleObjects()来等待其中之一(或全部)完成。 以下函数能够将一个特定的请求(并指定文件偏移值和数据大小)记录起来等待执行。 #include //need to keep the events in their o原创 2015-07-25 10:48:10 · 429 阅读 · 0 评论 -
ECHOSRV.C中的main()设立一个 I/O completion port
#include int main(int argc, char* argv[]) { SOCKET listener; SOCKET newsocket; WSADATA WsaData; struct sockaddr_in serverAddress; struct sockaddr_in clientAddress; int clientAddressLength;原创 2015-07-25 21:08:15 · 822 阅读 · 0 评论 -
IOBYFILE.C---overlapped I/O with a signaled file handle
#include int ReadSometion() { BOOL rc; HANDLE hFile; DWORD numread; OVERLAPPED overlap; char buf[512]; //open the file for overlapped reads hFile = CreateFile("C:\\WINDOWS\\WINFILE.EXE", GEN原创 2015-07-25 09:58:11 · 371 阅读 · 0 评论 -
异步过程调用(Asynchronous Procedure Call)
/*Demonstrates how to use APC's(asynchronous procedure calls)instread of signaled objects to service multiple outstanding overlapped operations on a file*/ #define WIN32_LEAN_AND_MEAN #include #i原创 2015-07-25 18:43:38 · 708 阅读 · 0 评论 -
安全的使用CreateThread()
/*uses multiple threads to search the files ".c" in the current directory for the string given on th command line; this example avoids most C run-time functions,so that it can use the single-threa原创 2015-07-27 10:51:47 · 578 阅读 · 0 评论 -
_beginthreadex()
列表8-1,使用_beginthreadex() /*this example uses the multithreaded version of the C run-time library so as to be able to use the FILE functions as well as calloc and free.*/ #define WIN32_LEAN_AND_M原创 2015-07-27 09:45:50 · 644 阅读 · 0 评论 -
Console I/O 加上CreateThread
//Console I/O 加上CreateThread /*Demonsttrates how to write a program that cna use CreateThread instead of calling _beginthreadex. This program does not need the multithread library. This program co原创 2015-07-27 14:13:12 · 802 阅读 · 0 评论 -
抽象的同步化对象
产生一个类:lock.其构造函数和析构函数负责“进入”和“离开”critical section.Lock的构造函数需要将一个CriticalSection对象指针作为唯一参数。在其内部,Lock持有一个指针,指向被锁定的CriticalSection对象。 临界区类: #include class CriticalSection { public: CriticalS原创 2015-07-28 14:39:40 · 387 阅读 · 0 评论 -
由一个成员函数来启动一个线程
/*shows how to start a thread based on a class memeber function using a static member function.*/ #define WIN32_LEAN_AND_MEAN #include #include #include #include typedef unsigned(WINAPI *P原创 2015-07-28 14:03:16 · 683 阅读 · 0 评论 -
建立可互换的Locks
LockableObject类 #include //除了构造函数,其它成员函数都声明为虚函数 class LockableObject{ public: LockableObject(){} virtual ~LockableObject(){} virtual void Lock() = 0; virtual void Unlock() = 0; private: CRIT原创 2015-07-28 15:09:41 · 495 阅读 · 0 评论 -
线程启动代码
PBEGINTHREADEX_THREADFUNC类型提供了一种将线程起始函数强制转换类型的方法,而PBEGINTHREADEX_THREADID类型提供了对于线程ID的一种强制转换类型方法。 线程启动代码: #define WIN32_LEAN_AND_MEAN #include #include #include #include typedef unsigned原创 2015-07-28 13:28:11 · 597 阅读 · 0 评论 -
THRDTERM-----干净地结束一个线程
THRDTERM产生两个线程,周期性地检查一个event对象,以决定要不要结束自己。 #define WIN32_LEAN_AND_MEAN #include #include #include #include #include "MtVerify.h" DWORD WINAPI ThreadFunc(LPVOID); HANDLE hRequestExitEve原创 2015-07-24 18:22:16 · 523 阅读 · 0 评论