- 浏览: 67132 次
- 性别:
- 来自: 青岛
-
文章分类
最新评论
////////////////
// The following example uses _beginthread and _endthread // crt_BEGTHRD.C // compile with: /MT /D "_X86_" /c // processor: x86 #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <stddef.h> #include <stdlib.h> #include <conio.h> void Bounce( void *ch ); void CheckKey( void *dummy ); /* GetRandom returns a random integer between min and max. */ #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) BOOL repeat = TRUE; /* Global repeat flag and video variable */ HANDLE hStdOut; /* Handle for console window */ CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */ int main() { CHAR ch = 'A'; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); /* Get display screen's text row and column information. */ GetConsoleScreenBufferInfo( hStdOut, &csbi ); /* Launch CheckKey thread to check for terminating keystroke. */ _beginthread( CheckKey, 0, NULL ); /* Loop until CheckKey terminates program. */ while( repeat ) { /* On first loops, launch character threads. */ _beginthread( Bounce, 0, (void *) (ch++) ); /* Wait one second between loops. */ Sleep( 1000L ); } } /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */ void CheckKey( void *dummy ) { _getch(); repeat = 0; /* _endthread implied */ } /* Bounce - Thread to create and and control a colored letter that moves * around on the screen. * * Params: ch - the letter to be moved */ void Bounce( void *ch ) { /* Generate letter and color attribute from thread argument. */ char blankcell = 0x20; char blockcell = (char) ch; BOOL first = TRUE; COORD oldcoord, newcoord; DWORD result; /* Seed random number generator and get initial location. */ srand( _threadid ); newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 ); newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 ); while( repeat ) { /* Pause between loops. */ Sleep( 100L ); /* Blank out our old position on the screen, and draw new letter. */ if( first ) first = FALSE; else WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result ); WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result ); /* Increment the coordinate for next placement of the block. */ oldcoord.X = newcoord.X; oldcoord.Y = newcoord.Y; newcoord.X += GetRandom( -1, 1 ); newcoord.Y += GetRandom( -1, 1 ); /* Correct placement (and beep) if about to go off the screen. */ if( newcoord.X < 0 ) newcoord.X = 1; else if( newcoord.X == csbi.dwSize.X ) newcoord.X = csbi.dwSize.X - 2; else if( newcoord.Y < 0 ) newcoord.Y = 1; else if( newcoord.Y == csbi.dwSize.Y ) newcoord.Y = csbi.dwSize.Y - 2; /* If not at a screen border, continue, otherwise beep. */ else continue; Beep( ((char) ch - 'A') * 100, 175 ); } /* _endthread given to terminate */ _endthread(); }
/*The following sample code demonstrates how you can use the thread handle returned by _beginthreadex with the synchronization API WaitForSingleObject. The main thread waits for the second thread to terminate before it continues. When the second thread calls _endthreadex, it causes its thread object to go to the signaled state. This allows the primary thread to continue running. This cannot be done with _beginthread and _endthread, because _endthread calls CloseHandle, destroying the thread object before it can be set to the signaled state.*/ // crt_begthrdex.cpp // compile with: /MT #include <windows.h> #include <stdio.h> #include <process.h> unsigned Counter; unsigned __stdcall SecondThreadFunc( void* pArguments ) { printf( "In second thread...\n" ); while ( Counter < 1000000 ) Counter++; _endthreadex( 0 ); return 0; } int main() { HANDLE hThread; unsigned threadID; printf( "Creating second thread...\n" ); // Create the second thread. hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // Wait until second thread terminates. If you comment out the line // below, Counter will not be correct because the thread has not // terminated, and Counter most likely has not been incremented to // 1000000 yet. WaitForSingleObject( hThread, INFINITE ); printf( "Counter should be 1000000; it is-> %d\n", Counter ); // Destroy the thread object. CloseHandle( hThread ); }
The CreateThread function creates a new thread for a process. The creating thread must specify the starting address of the code that the new thread is to execute. Typically, the starting address is the name of a function defined in the program code (for more information, see ThreadProc). This function takes a single parameter and returns a DWORD value. A process can have multiple threads simultaneously executing the same function. /* The following is a simple example that demonstrates how to create a new thread that executes the locally defined function, ThreadProc. The creating thread uses a dynamically allocated buffer to pass unique information to each instance of the thread function. It is the responsibility of the thread function to free the memory. The calling thread uses the WaitForMultipleObjects function to persist until all worker threads have terminated. Note that if you were to close the handle to a worker thread before it terminated, this does not terminate the worker thread. However, the handle will be unavailable for use in subsequent function calls.*/ #include <windows.h> #include <strsafe.h> #define MAX_THREADS 3 #define BUF_SIZE 255 typedef struct _MyData { int val1; int val2; } MYDATA, *PMYDATA; DWORD WINAPI ThreadProc( LPVOID lpParam ) { HANDLE hStdout; PMYDATA pData; TCHAR msgBuf[BUF_SIZE]; size_t cchStringSize; DWORD dwChars; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if( hStdout == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type. pData = (PMYDATA)lpParam; // Print the parameter values using thread-safe functions. StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), pData->val1, pData->val2); StringCchLength(msgBuf, BUF_SIZE, &cchStringSize); WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, NULL); // Free the memory allocated by the caller for the thread // data structure. HeapFree(GetProcessHeap(), 0, pData); return 0; } void main() { PMYDATA pData; DWORD dwThreadId[MAX_THREADS]; HANDLE hThread[MAX_THREADS]; int i; // Create MAX_THREADS worker threads. for( i=0; i<MAX_THREADS; i++ ) { // Allocate memory for thread data. pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); if( pData == NULL ) ExitProcess(2); // Generate unique data for each thread. pData->val1 = i; pData->val2 = i+100; hThread[i] = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadProc, // thread function pData, // argument to thread function 0, // use default creation flags &dwThreadId[i]); // returns the thread identifier // Check the return value for success. if (hThread[i] == NULL) { ExitProcess(i); } } // Wait until all threads have terminated. WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE); // Close all thread handles upon completion. for(i=0; i<MAX_THREADS; i++) { CloseHandle(hThread[i]); } }
相关推荐
多线程 1. 什么是线程: 线程就是程序中单独顺序的流控制。线程本身不能运行,它只能用于程序中。 2. 什么是多线程: 多线程则指的是在单个程序中可以同时运行多个不同的线程执行不同的任务. 说明: 线程是程序内...
《狂神说多线程详解》是一份深入探讨多线程技术的资源包,其中包含了对多线程编程的详尽解析。多线程是现代计算机编程中的一个重要概念,尤其在处理高性能计算、并发操作以及实时系统时,多线程技术显得尤为重要。它...
1. **Synchronize方法**:用于在多线程环境中安全地访问主线程中的控件或数据。调用Synchronize方法会将指定的代码放入主线程的消息队列,待主线程有机会时执行。 2. **TEvent对象**:可以用来在多个线程之间同步...
1. **死锁**:两个或更多线程相互等待对方释放资源,导致所有线程都无法继续执行。避免死锁需要遵循一些原则,如避免循环等待、资源有序分配等。 2. **活锁**:线程不断尝试获取资源,但一直失败,导致线程无限期地...
"大漠多线程模板"是一个专门针对C#开发的多线程处理框架,它为开发者提供了便捷的方式来管理和优化多线程应用。这个框架由知名开发者"大漠"创建,旨在简化复杂的并发编程,提高代码的可读性和可维护性。 多线程允许...
1. PowerBuilder 9.0的多线程实现:PB9不直接支持多线程,但可以通过第三方控件或自定义编程实现。 2. Ttimer.ocx控件:这是一个可能用于多线程环境的ActiveX定时器控件,可以触发并发操作。 3. 多线程的优势:多...
在IT领域,多线程编程是一项关键技能,尤其是在性能优化和并发处理方面。本文将深入探讨多线程编程的基础知识,以帮助初学者快速入门。 首先,我们需要理解什么是多线程。多线程是指在一个进程中同时执行多个独立的...
在IT行业中,多线程是一种常见的编程技术,它允许程序同时执行多个独立的任务,从而提高计算机系统的效率和响应性。特别是在自动化工具如“按键精灵”中,多线程的应用能够显著提升其性能和实用性。 标题“多线程_...
在编程领域,多线程是实现并发执行任务的重要机制,特别是在现代计算机系统中,多核处理器使得多线程成为提高程序性能的关键手段。C#语言提供了丰富的多线程支持,让我们能够编写出高效的多线程应用程序。在这个"多...
在编程领域,多线程是实现并发执行任务的重要机制,特别是在易语言中,它能有效提升程序的执行效率。易语言是一种中文编程语言,旨在降低编程门槛,让普通用户也能进行程序开发。本文将深入探讨易语言中的多线程以及...
1. **线程安全**:确保共享数据在多线程环境下正确访问,通常需要使用锁(如Monitor或Mutex)、信号量(Semaphore)或原子操作来实现。 2. **线程间通信**:线程间的同步和通信至关重要,可以使用事件(Event)、...
1. 提高系统吞吐量:多线程使得CPU资源得以充分利用,提高程序整体执行效率。 2. 响应性:在进行耗时操作时,主线程不会被阻塞,保持用户界面的响应性。 3. 资源复用:多线程共享进程资源,减少内存开销。 缺点: 1...
"鱼刺多线程注册源码例子"是一个基于"鱼刺多线程稳定框架"的编程实践,旨在展示如何在软件开发中有效地利用多线程技术来提高程序的执行效率和稳定性。在这个例子中,"鱼刺框架"可能是一个专门为多线程编程设计的开源...
在.NET框架中,C#语言提供了强大的多线程支持,使得开发者可以充分利用现代多核处理器的优势,实现并行处理和高效能编程。本资源包含六个C#.NET多线程的实例,涵盖了多线程的基本使用到更高级的概念,如线程互斥。...
《深入理解易语言版多线程通用框架》 在计算机编程中,多线程是一种并发执行任务的技术,它允许多个任务在同一时间内运行,从而提高了系统资源的利用率和程序的响应速度。尤其在易语言这样的高级编程环境中,多线程...
在编程领域,尤其是在开发高效、响应迅速的应用程序时,多线程技术扮演着至关重要的角色。Qt5框架提供了一种方便的方式来实现多线程,它允许开发者在不同的线程中执行任务,从而避免主线程(GUI线程)因处理耗时操作...
在编程领域,多线程是实现并发执行任务的关键技术,特别是在C#这样的语言中,它提供了丰富的多线程支持。本文将深入探讨C#中的多线程实例,以帮助开发者理解如何有效地利用多核处理器资源,提高程序的执行效率。 多...
1.多线程的执行顺序无法保证,与操作系统的调度策略和线程优先级等因素有关。 2.多线程的切换可能发生在任何时刻、任何地点。 3.多线程对代码的敏感度高,因此对代码的细微修改都可能产生意想不到的效果。 先由一...
基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip...
.NET框架的多线程技术是开发高性能应用程序的关键组成部分,特别是在处理并发操作、并行计算以及UI更新时。在.NET 2.0版本中,多线程功能已经得到了充分的优化和增强,允许开发者构建出更加高效的应用程序。下面将...