游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6748|回复: 23

如何新建线程

[复制链接]

33

主题

101

帖子

107

积分

注册会员

Rank: 2

积分
107
发表于 2007-6-2 13:54:00 | 显示全部楼层 |阅读模式
我的理解,线程就是另外一个东西在执行和我的主循环无关,是这样吗?
比如我的主循环负责帖图,但是我有一个函数是等待我输入(不是键盘和鼠标,总之是输入),
那是不是该为这个函数建个线程呢?
我现在是直接把这个函数放到主循环里,那么如果我不输入就不往下进行,图像就不刷新。
如果建该怎么建呢?假设我的函数是get();
请指教,谢谢

8

主题

310

帖子

311

积分

中级会员

Rank: 3Rank: 3

积分
311
QQ
发表于 2007-6-2 16:37:00 | 显示全部楼层

Re:如何新建线程

HANDLE wthread;
wthread = CreateThread(NULL,0,get,NULL,0,NULL);

33

主题

101

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2007-6-2 22:33:00 | 显示全部楼层

Re:如何新建线程

上面写的我在MSDN里看到的,但是不太明白,具体怎么使用,怎么样可以使我的获取输入函数成为一个线程呢?而且像你写的那样以后,是不是获取输入函数就会一直执行呢?谢谢

0

主题

172

帖子

176

积分

注册会员

Rank: 2

积分
176
发表于 2007-6-2 23:44:00 | 显示全部楼层

Re:如何新建线程

这种情况,采用windows消息驱动模型,要么多线。

但是多线切记注意线程同步互斥,多线带来的BUG是极其难以调试的。

最简单的,线程所执行函数里
while(true)
{
    if(不需监听了) break;
    get();
}

0

主题

172

帖子

176

积分

注册会员

Rank: 2

积分
176
发表于 2007-6-2 23:47:00 | 显示全部楼层

Re:如何新建线程

顺便说一下,就是因为有大量类似LZ的需求,所以windows的很多IO操作(比如说文件, socket之类),都提供了重叠IO结构模型

0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2007-6-3 14:19:00 | 显示全部楼层

Re:如何新建线程

灌水无罪!!!!!!!!!!

0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2007-6-3 14:20:00 | 显示全部楼层

Re:如何新建线程

灌水无罪!!!!!!!!!!

0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2007-6-3 14:21:00 | 显示全部楼层

Re:如何新建线程

灌水无罪!!!!!!!!!!

0

主题

769

帖子

1052

积分

金牌会员

Rank: 6Rank: 6

积分
1052
发表于 2007-6-3 20:39:00 | 显示全部楼层

Re:如何新建线程

这个是我当时学操作系统里面关于线程的代码
著名的读者写者问题的实现,里面有两个线程函数的定义和使用
希望有所帮助

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__A7490AF0_1731_4B9E_A713_88E0EB7AD429__INCLUDED_)
#define AFX_STDAFX_H__A7490AF0_1731_4B9E_A713_88E0EB7AD429__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct thread_info
{
        int id;
        char role;
        float delay;
        int persist;
};

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__A7490AF0_1731_4B9E_A713_88E0EB7AD429__INCLUDED_)

-----------------------------------------------------------------------------
// ReaderAndWriter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#define UserNum_Max 100
typedef HANDLE semaphore;
semaphore mutex;
semaphore mutex1;
semaphore mutex2;
semaphore mutex3;
semaphore r_w_w;
semaphore cs_Read;
semaphore cs_Write;
int readCount=0;
int writeCount=0;
thread_info User[UserNum_Max];

DWORD WINAPI reader_RF(LPVOID lpParam)
{
        thread_info Param=*((thread_info *)lpParam);
        ::Sleep((int)(Param.delay*1000));
        cout<&ltaram.id<<"is sending a reading require\n";
        ::WaitForSingleObject(mutex,INFINITE);//P操作
        readCount++;
        if(readCount==1)
        {
                ::WaitForSingleObject(r_w_w,INFINITE);               
        }
        ::ReleaseSemaphore(mutex,1,NULL);//V操作
        cout<<Param.id<<"begins reading.."<<endl;
        ::Sleep(Param.persist*1000);
        cout<<Param.id<<"finish reading"<<endl;
        ::WaitForSingleObject(mutex,INFINITE);
        readCount--;
        if(readCount==0)
        {
                ::ReleaseSemaphore(r_w_w,1,NULL);
        }
        ::ReleaseSemaphore(mutex,1,NULL);
        return 0;
}

DWORD WINAPI writer_RF(LPVOID lpParam)
{
        thread_info Param=*((thread_info *)lpParam);
        ::Sleep((int)(Param.delay*1000));
        cout<<Param.id<<"is sending a write require"<<endl;
        ::WaitForSingleObject(r_w_w,INFINITE);
        cout<<Param.id<<"begins writing.."<<endl;
        ::Sleep(Param.persist*1000);
        cout<<Param.id<<"finish writing.."<<endl;
        ::ReleaseSemaphore(r_w_w,1,NULL);
        return 0;
}

DWORD WINAPI reader_WF(LPVOID lpParam)
{
        thread_info Param=*((thread_info *)lpParam);
        ::Sleep((int)(Param.delay*1000));
        cout<<Param.id<<" is sending a reading require\n";
        ::WaitForSingleObject(mutex1,INFINITE);
        ::WaitForSingleObject(cs_Read,INFINITE);
        ::WaitForSingleObject(mutex2,INFINITE);
        readCount++;
        if(readCount==1)
        {
                ::WaitForSingleObject(cs_Write,INFINITE);
        }
        ::ReleaseSemaphore(mutex2,1,NULL);
        ::ReleaseSemaphore(cs_Read,1,NULL);
        ::ReleaseSemaphore(mutex1,1,NULL);
        cout<<Param.id<<"begins reading.."<<endl;
        ::Sleep(Param.persist*1000);
        cout<<Param.id<<"finish reading"<<endl;
        ::WaitForSingleObject(mutex2,INFINITE);
        readCount--;
        if(readCount==0)  
        {
                ::ReleaseSemaphore(cs_Write,1,NULL);
        }
        ::ReleaseSemaphore(mutex2,1,NULL);
        return 0;
}

DWORD WINAPI writer_WF(LPVOID lpParam)
{
        thread_info Param=*((thread_info *)lpParam);
        ::Sleep((int)(Param.delay*1000));
        cout<<Param.id<<" is sending a write require"<<endl;
        ::WaitForSingleObject(mutex3,INFINITE);
        writeCount++;
        if(writeCount==1)
        {
                ::WaitForSingleObject(cs_Read,INFINITE);
        }
        ::ReleaseSemaphore(mutex3,1,NULL);
        ::WaitForSingleObject(cs_Write,INFINITE);
        cout<<Param.id<<"begins writing.."<<endl;
        ::Sleep(Param.persist*1000);
        cout<<Param.id<<"finish writing.."<<endl;
        ::ReleaseSemaphore(cs_Write,1,NULL);
        ::WaitForSingleObject(mutex3,INFINITE);
        writeCount--;
        if(writeCount==0)
        {
                ::ReleaseSemaphore(cs_Read,1,NULL);
        }
        ::ReleaseSemaphore(mutex3,1,NULL);
        return 0;
}

int main(int argc, char* argv[])
{
        DWORD dwThreadId[UserNum_Max];
        int UserNum=0;
        int temp;
        HANDLE h[UserNum_Max];
        ifstream in("Users.dat");
        int mode;
        printf("*************************************\n");
        printf("*   1.读者优先                      *\n");
        printf("*   2.写者优先                      *\n");
        printf("*   3.退出                          *\n");
        printf("*************************************\n");
        printf("Please enter your choice:\n");
        mutex=::CreateSemaphore(NULL,1,1,NULL);
        mutex1=::CreateSemaphore(NULL,1,1,NULL);
        mutex2=::CreateSemaphore(NULL,1,1,NULL);
        mutex3=::CreateSemaphore(NULL,1,1,NULL);
        cs_Read=::CreateSemaphore(NULL,1,1,NULL);
        cs_Write=::CreateSemaphore(NULL,1,1,NULL);
        r_w_w=::CreateSemaphore(NULL,1,1,"reader_RF");
        cin>>mode;
        while(in>>temp&&(mode==1||mode==2))
        {
                User[UserNum].id=temp;
                in>>User[UserNum].role;
                in>>User[UserNum].delay;
                in>>User[UserNum].persist;
//                cout<<User[UserNum].id;
//                cout<<User[UserNum].role;
//                cout<<User[UserNum].delay;
//                cout<<User[UserNum].persist;
                if(mode==1)
                {
                        if(User[UserNum].role=='R')       
                        {
                                h[UserNum]=::CreateThread(NULL,NULL,reader_RF,&User[UserNum],0,&dwThreadId[UserNum]);
                        }
                        else if(User[UserNum].role=='W')
                        {
                                {
                                h[UserNum]=::CreateThread(NULL,NULL,writer_RF,&User[UserNum],0,&dwThreadId[UserNum]);
                                }
                        }
                }
                else if(mode==2)
                {
                        if(User[UserNum].role=='R')       
                        {
                                h[UserNum]=::CreateThread(NULL,NULL,reader_WF,&User[UserNum],0,&dwThreadId[UserNum]);
                        }
                        else if(User[UserNum].role=='W')
                        {
                                {
                                h[UserNum]=::CreateThread(NULL,NULL,writer_WF,&User[UserNum],0,&dwThreadId[UserNum]);
                                }
                        }
                }
                UserNum++;
        }
        if(mode==3) cout<<"Goodbye!"<<endl;
        ::WaitForMultipleObjects(UserNum,h,TRUE,INFINITE);
        return 0;
}

0

主题

769

帖子

1052

积分

金牌会员

Rank: 6Rank: 6

积分
1052
发表于 2007-6-3 20:40:00 | 显示全部楼层

Re:如何新建线程

其中是通过文件提供线程信息的:
USERS.DAT:
1 R 3 5
2 W 4 5
3 R 6 5
4 R 6 5
5 W 5.1 3
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-26 04:35

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表