C中一次只能运行一个线程 [英] Only one thread runs at a time in C

查看:140
本文介绍了C中一次只能运行一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图同时运行多个线程,但是我一次只能看到一个线程.您能否让我知道这种情况的任何示例.以下是供您参考的源代码.
请注意,我正在尝试通过Win32 API在Windows上的C语言中实现逻辑. 它是一个矩阵程序,试图在每一列中逐行打印5个字符.因此,当程序运行时,我想同时打印多列,但一次不只打印一列.请给我建议一种方法.

Hi ,
I am trying to run multiple threads at a same time but i see that only one thread runs at a time. Can you please let me know any example of this scenario. Below is the source code for your reference.
Please note that i am trying to implement the logic in C on windows through win32 apis.
Its a matrix program trying to print 5 chars in each column line by line one below the other. So when program runs i want to print multiple columns at the same time but not only one column at one time. please suggest me a way to do this.

#include <Windows.h>
#include <process.h>
#include <stdio.h>

void MyThread1( void * );
void _gotoxy( int, int, TCHAR );

static int iCol, iRow;
HANDLE hStdOut, hMain, hThread1, hThread2;
TCHAR cArr[] = { ''a'',''b'',''c'',''d'',''e'',''f'',''g'',''h'',''i'',''j'',''k'',''l'',''m'',
				 ''n'',''o'',''p'',''q'',''r'',''s'',''t'',''u'',''v'',''w'',''x'',''y'',''z'',
				 ''A'',''B'',''C'',''D'',''E'',''F'',''G'',''H'',''I'',''J'',''K'',''L'',''M'',
				 ''N'',''O'',''P'',''Q'',''R'',''S'',''T'',''U'',''V'',''W'',''X'',''Y'',''Z'' };

int main()
{
	hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
	hMain = CreateEvent( NULL, FALSE, FALSE, NULL );
	hThread1 = CreateEvent( NULL, FALSE, TRUE, NULL );
	_beginthread( MyThread1, 0, NULL );
	
	while( TRUE )
	{
		WaitForSingleObject( hMain, INFINITE );
		_gotoxy( iCol, iRow, cArr[ rand() % ( sizeof( cArr ) / sizeof( cArr[0] ) ) ] );
		Sleep( 100 );
		SetEvent( hThread1 );
		Sleep( 100 );
		_beginthread( MyThread1, 0, NULL );
	}
	return 0;
}

void MyThread1( void * arg )
{
	int i;
	
	while( TRUE )
	{
		iCol = rand() % 40;
		iRow = 0;

		for( i = 0; i < 5; i++ )
		{
			WaitForSingleObject( hThread1, INFINITE );
			SetEvent( hMain );
			Sleep( 100 );
			++iRow;
		}
	}
	_endthread();
}

void _gotoxy( int iCol, int iRow, TCHAR ch )
{
	COORD position;

	position.Y = iRow;
	position.X = iCol;
	SetConsoleCursorPosition( hStdOut, position );
	printf("%c", ch );
}



请帮助实现C中的逻辑.



Please help to implement the logic in C.

推荐答案

问题是,除了调用睡眠以外,将值打印到屏幕上也是最昂贵的操作.这意味着该线程在睡眠期间以及其他线程正在向控制台写入时处于非活动状态.写入控制台或更新GUI必须始终由主线程完成,并且由于只有一个主线程,因此只有一个线程处于活动状态.

另一个非常重要的事情是如何使用事件对象.事件对象已设置但从未重置(请参见ResetEvent).

您可能想查看有关此问题的msdn上的示例以获取更多信息:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms685081%28v=vs.85%29.aspx [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms686915%28v=vs.85%29.aspx [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms686927%28v=vs.85%29.aspx [
The problem is that printing the values to the screen is, besides the call to sleep, the most expensive operation. This means that the thread is inactive during the sleep and also if another thread is writing to the console. Writing to the console or updating the GUI must always be done by the main thread and because there is only one main thread, just one thread is active.

Another very important thing is how you use the event objects. The event object is set but never reset (see ResetEvent).

You might want to check out the examples on msdn on this for more info:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms685081%28v=vs.85%29.aspx[^]

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915%28v=vs.85%29.aspx[^]

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686927%28v=vs.85%29.aspx[^]

Good luck!


您似乎有意将线程序列化为一次工作.您将其引发事件处理程序hThread1.看看您是如何创建的!您使用自动重置(第二个参数,手动重置,为false).这是一种非常平凡的模式:它设计为一​​次仅允许一个威胁通过,并在此句柄上抛出等待.如果这样做,您如何期望并行工作?我想您知道,这种调度"违反了 multi 线程化的目的.

—SA
It looks like you intentionally serialize the threads to work one at a time. You do it throw the event handler hThread1. Look how you create it! You use auto reset (second parameter, manual reset, is false). This is a very non-trivial mode: it is designed to allow only one threat at a time to be passes throw the wait on this handle. How can you expect working in parallel if you do so? I think you understand that such "scheduling" defeats the purpose of multithreading.

—SA


这篇关于C中一次只能运行一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆