Linux kernel 线程执行:如何保证线程的系统启动
我在ubuntu和其他linux平台上看到了不同的线程启动方式Linux kernel 线程执行:如何保证线程的系统启动,linux-kernel,pthreads,scheduler,Linux Kernel,Pthreads,Scheduler,我在ubuntu和其他linux平台上看到了不同的线程启动方式 pthread_create ( &thread1, NULL, (void *) &myfun1, (void *) msg1); pthread_create ( &thread2, NULL, (void *) &myfun2, (void *) msg2); pthread_create ( &thread3, NULL, (void *) &myfun3, (void *)
pthread_create ( &thread1, NULL, (void *) &myfun1, (void *) msg1);
pthread_create ( &thread2, NULL, (void *) &myfun2, (void *) msg2);
pthread_create ( &thread3, NULL, (void *) &myfun3, (void *) msg3);
pthread_create ( &thread4, NULL, (void *) &myfun4, (void *) msg4);
在上面的ubuntu中,首先是thread4启动,而在其他linux操作系统中是thread1。当我检查原因时,似乎是因为计划策略(如果我错了,请纠正我)
使线程1在另一个线程执行任务之前启动的伪代码:
Mutex mutex = ... /* static init here */
Condition cond = ... /* static init here */
boolean first_thread_running = false;
thread1
{
mutex_lock
first_thread_running = true;
cond_signal
mutex_unlock
/* do things */
}
thread<N> /* with <N> = {2, 3, 4} */
{
mutex_lock
while (not first_thread_running)
cond_wait
mutex_unlock
/* thread1 definitly runs now */
/* do things */
}
main
{
/* The order of the following statements does not matter, thread 1
will always be started before the other threads "do their things". */
thread4_create
thread2_create
thread1_create
thread3_create
...
Mutex Mutex=…/*这里是静态初始化*/
条件条件cond=…/*这里是静态初始化*/
布尔值first_thread_running=false;
螺纹1
{
互斥锁
第一个线程运行=真;
cond_信号
互斥解锁
/*做事*/
}
thread/*with={2,3,4}*/
{
互斥锁
当(不是第一个线程运行时)
等一下
互斥解锁
/*thread1现在肯定会运行*/
/*做事*/
}
主要的
{
/*以下语句的顺序无关紧要,线程1
将始终在其他线程“完成其任务”之前启动*/
线程4_创建
线程2_创建
线程1_创建
线程3_创建
...
它是异步的本质。如果没有附加的同步机制(互斥体/条件),您无法预测线程运行(甚至启动)的顺序.ok synch我们可以在线程启动后添加..但是在创建线程之前如何确保?您必须假设任何线程在任何时候都可以被挂起,以便为不同的线程提供处理时间。因此,如果您设法以可靠的顺序启动它们,它仍然不会给您任何东西。我认为您认为可以实现的任何功能都是Illusion,您应该描述您的实际问题是什么。此外,您似乎针对的是C-ish语言,但到底是哪种语言?在您进行此操作时,将“多线程”添加到标记列表中。我希望除了锁定机制之外,可能还有其他方法,谢谢您的建议