Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中使用螺纹+; 你能告诉我如何在C++程序中使用线程吗?我怎么编译它,因为它是多线程的?你能告诉我一些好的网站,我可以从根开始吗_C++_Multithreading - Fatal编程技术网

在C+中使用螺纹+; 你能告诉我如何在C++程序中使用线程吗?我怎么编译它,因为它是多线程的?你能告诉我一些好的网站,我可以从根开始吗

在C+中使用螺纹+; 你能告诉我如何在C++程序中使用线程吗?我怎么编译它,因为它是多线程的?你能告诉我一些好的网站,我可以从根开始吗,c++,multithreading,C++,Multithreading,感谢使用Unix/Linux/BSD,这里有一个库: 我想Win32 API中也有类似的功能 我自己没有使用过它,但有人告诉我,Boost线程库让它变得非常简单 (事后看来,这篇文章对pthreads有点片面。但我是一个Unix/Linux类型的人。就最初的主题而言,这种方法似乎是最好的。)我使用的是我大学教授写的一个库。它的实现非常简单,并且工作得非常好(使用它已经有一段时间了)。我会请他允许我和你分享 抱歉,前面的等待,但必须检查:) ++++++编辑+++++++ 好的,我和我的教

感谢使用Unix/Linux/BSD,这里有一个库:

我想Win32 API中也有类似的功能


我自己没有使用过它,但有人告诉我,Boost线程库让它变得非常简单


(事后看来,这篇文章对pthreads有点片面。但我是一个Unix/Linux类型的人。就最初的主题而言,这种方法似乎是最好的。)

我使用的是我大学教授写的一个库。它的实现非常简单,并且工作得非常好(使用它已经有一段时间了)。我会请他允许我和你分享

抱歉,前面的等待,但必须检查:)

++++++编辑+++++++

好的,我和我的教授谈过了,他不介意我在这里分享。下面是Paul Davies编写的“RT库”的.h和.cpp文件

关于线程和此库的使用,需要注意以下几点:

0)本教程将解释在windows平台上创建和使用线程。

1)C++中的线程通常被编码为同一个源的一部分(不像每个进程有自己的源文件和函数主())的进程

2) 当进程启动并运行时,它可以通过进行适当的内核调用来创建其他线程

3) 多个线程比多个进程运行得更快,因为它们是同一进程的一部分,这减少了操作系统的开销,并减少了内存需求

4) 在本例中,您将使用rt库中的CThread类

5) (确保rt.h和rt.cpp是“解决方案”的一部分,并确保在main.cpp中包含rt.h)

6) 下面是将来主线程(当然是在main.cpp中)的一部分代码,您将在其中使用CThread类创建线程

void main() 
{
    CThread   t1(ChildThread1, ACTIVE, NULL) ;      
    . . . 
    t1.WaitForThread() ;                // if thread already dead, then proceed, otherwise wait

}
t1的参数依次为:充当线程的函数的名称、线程状态(它可以是活动的,也可以是挂起的,具体取决于您想要什么),最后是指向创建时可能要传递给线程的可选数据的指针。在执行一些代码之后,您需要调用WaitForThread()函数

7) 下面是来自未来主线程(当然是在main.cpp中)的部分代码,您将在其中描述子线程的功能

UINT  _ _stdcall  ChildThread1(void *args)      
{

    . . .           
}
看起来很奇怪的是微软的线程签名。我相信,通过一些研究,你可以找到在其他操作系统中如何做到这一点。参数是可在创建时传递给子级的可选数据

8) 您可以在rt.cpp文件中找到成员函数的详细描述。以下是总结:

CThread()-负责创建线程的构造函数

Suspend()-挂起子线程,有效地暂停它

Resume()-唤醒挂起的子线程

SetPriority(int值)-将子线程的优先级更改为 指定的

Post(int消息)-将消息发布到子线程

TerminateThread()-终止或终止子线程

WaitForThread()-暂停父线程,直到子线程终止。 如果子线程已经终止,父线程将不会暂停

9) 下面是一个完整程序示例。您可以做的一件聪明的事情是创建单个线程的多个实例化

    #include “..\wherever\it\is\rt.h” //notice the windows notation

    int       ThreadNum[8] = {0,1,2,3,4,5,6,7} ;   // an array of thread numbers


    UINT _ _stdcall ChildThread (void *args)    // A thread function 
    {   
        MyThreadNumber = *(int *)(args);    

        for ( int i = 0; i < 100; i ++)
            printf( "I am the Child thread: My thread number is [%d] \n", MyThreadNumber) ;

        return 0 ;
    }
int     main()
{
    CThread     *Threads[8] ;   

// Create 8 instances of the above thread code and let each thread know which number it is.


    for ( int i = 0; i < 8; i ++) {
        printf ("Parent Thread: Creating Child Thread %d in Active State\n", i) ;
        Threads[i] = new CThread (ChildThread, ACTIVE, &ThreadNum[i]) ;
    }

    // wait for threads to terminate, then delete thread objects we created above

    for( i = 0; i < 8; i ++) {
        Threads[i]->WaitForThread() ;
    delete Threads[i] ; // delete the object created by ‘new’
    }
    return 0 ;
}
#包括“.\it\is\rt.h”//注意windows符号
int ThreadNum[8]={0,1,2,3,4,5,6,7};//线程数数组
UINT\uu stdcall ChildThread(void*args)//一个线程函数
{   
MyThreadNumber=*(int*)(args);
对于(int i=0;i<100;i++)
printf(“我是子线程:我的线程号是[%d]\n”,MyThreadNumber);
返回0;
}
int main()
{
CThread*线程[8];
//创建上述线程代码的8个实例,并让每个线程知道它是哪个数字。
对于(int i=0;i<8;i++){
printf(“父线程:正在活动状态下创建子线程%d\n”,i);
Threads[i]=新的CThread(ChildThread、ACTIVE和ThreadNum[i]);
}
//等待线程终止,然后删除上面创建的线程对象
对于(i=0;i<8;i++){
线程[i]->WaitForThread();
删除线程[i];//删除由“new”创建的对象
}
返回0;
}

10) 就这样!rt库包含一系列类,使您能够使用进程、线程和其他并发编程技术。发现其余的;)

> P>我使用英特尔线程构建块库中的TBBHthType类。

< P>有很多与C++兼容的线程库。所以首先你必须选择一个。我更喜欢POSIX线程(也称为pthreads)。如何编译它取决于您选择的库。

C/C++中线程的用法:

#include <iostream>

using namespace std;

extern "C" 
{
     #include <stdlib.h>
     #include <pthread.h>
     void *print_message_function( void *ptr );
}


int main()
{
     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;

     iret1 = pthread_create( &thread1, NULL, print_message_function (void*) message1);
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     //printf("Thread 1 returns: %d\n",iret1);
     //printf("Thread 2 returns: %d\n",iret2);
     cout<<"Thread 1 returns: %d\n"<<iret1;
     cout<<"Thread 2 returns: %d\n"<<iret2;

     exit(0);
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     //printf("%s \n", message);
     cout<<"%s"<<message;
}
#包括
使用名称空间std;
外部“C”
{
#包括
#包括
void*打印消息功能(void*ptr);
}
int main()
{
pthread_t thread1,thread2;
char*message1=“线程1”;
char*message2=“线程2”;
int iret1,iret2;
iret1=pthread_create(&thread1,NULL,print_message_函数(void*)message1);
iret2=pthread_create(&thread2,NULL,print_message_函数,(void*)message2);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
//printf(“线程1返回:%d\n”,iret1);
//printf(“线程2返回:%d\n”,iret2);

库蒂在和我的教授谈过在这里分享他的图书馆后,对我的答案进行了编辑。