简体   繁体   English

如何在C ++中创建一个构造函数来创建pthread,它应该在线程开始运行时返回

[英]How to create a constructor in C++ for creating pthread which should return when thread starts running

I want to create a constructor for my thread class which can create a single thread and it should return when the thread starts running at the entry point 我想为我的线程类创建一个构造函数,它可以创建一个单独的线程,它应该在线程开始在入口点运行时返回

Entry point will be some class function which will be passed during run time. 入口点将是一些将在运行时传递的类函数。 Derived class function cannot be changed and it wont update any shared variable which could be checked. 派生类函数不能更改,它不会更新任何可以检查的共享变量。

How can I make sure that the pthread I created starts executing at the entry point I specify 如何确保我创建的pthread开始在我指定的入口点执行

That's is not a good idea. 这不是一个好主意。

The constructor of the base class is run first. 首先运行基类的构造函数。 So if the base class constructor does not return until the thread has reached the entry point then the thread is running inside an object who's constructor has not run. 因此,如果基类构造函数在线程到达入口点之前没有返回,则线程在构造函数尚未运行的对象内运行。 Thus is completely uninitiated. 因此完全没有经验。

If the entry point is a virtual method that is defined in a derived type then you have definitely invoked undefined behavior. 如果入口点是在派生类型中定义的虚方法,那么您肯定会调用未定义的行为。

This is also why most threading classes are not designed this way. 这也是大多数线程类不是这样设计的原因。 Usually you create the thread object. 通常您创建线程对象。 Then call a method like start() which runs a function/method or runable object passed as a parameter. 然后调用类似start()的方法,该方法运行作为参数传递的函数/方法或可运行对象。 This way you know that the object representing the thread is fully constructed and all members are correctly initialized. 这样您就知道表示线程的对象是完全构造的,并且所有成员都已正确初始化。

You will need to use synchronization primitives to do that, for instance a barrier . 您将需要使用同步原语来执行此操作,例如屏障 You can simply wrap the entry point you get by a different entry point that signals this synchronization primitive and then calls its base entry point. 您可以简单地通过一个不同的入口点来包装您获得的入口点,该入口点用于指示此同步原语,然后调用其基本入口点。

Not much more that can be said without seeing any actual code of what you are trying to accomplish. 如果没有看到你想要实现的任何实际代码,可以说更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++,为具有返回类型的函数创建pthread吗? - C++, create a pthread for a function with a return type? 返回类型Pthread使用C ++创建 - Return Type Pthread Create Using C++ C ++:使用pthread_create创建新线程以运行类成员函数 - C++: Creating new thread using pthread_create, to run a class member function 如何在C ++中使用pthread将数据从一个线程传递到另一个正在运行的线程 - How to pass data from one thread to another running thread using pthread in C++ C ++ pthread - 如何取消线程? - C++ pthread - How to cancel a thread? pthread_create的错误返回代码是35错误,原因是进程很多,我用过pthread_exit,它应该杀死线程,不是吗? - error return code from pthread_create is 35 error due to many processes,I have used pthread_exit which should kill the thread isn't it? C++ pthread - 如何在不等待完成的情况下从线程 function 返回值? - C++ pthread - how to return a value from thread function without waiting for finish? 线程取消(pthread)和C ++ - Thread cancellation (pthread) & C++ 创建子类复制构造函数时,我是否被迫使用/创建默认的超级构造函数? [C++] - am I forced to use/create the default super constructor when creating a subclass copy constructor? [C++] 为什么pthread_create()返回0但线程从不启动 - Why does pthread_create() returns 0 but the thread never starts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM