QNXマルチスレッドプログラミングthread example
9004 ワード
QNX Momentics環境にはいくつかのサンプルが初心者の参考になります.QNXマルチスレッドプログラミングを学ぶ良い方法の一つは、QNXのマルチスレッドサンプルを導入し、テスト実行後にサンプルコードを分析することです.自分でテストサンプルを書くよりも、入力エラー、構成エラーなどの低レベルのエラーによる問題を減らすことができます.
QNX Momenticsで「File->New->Example...」を選択サンプルウィザードを開き、「QNX Example Neutrino Threads Project」を選択します.次にデフォルト設定を保持し、「Next」をクリックし、「Finish」をクリックするとQNXマルチスレッドサンプルプロジェクトが作成されます.
作成したプロジェクトのターゲットシステムはX 86なので、QNX仮想マシン上で変更することなく実行できます.プロジェクトを選択し、右クリックして「Build Project」を選択してプロジェクトをコンパイルします.
正常に実行されると、コンソールは次のように出力されます.
ソース・プログラム・コードは次のとおりです.
まとめ、pthread_create(,,)作成スレッド(共有リソーススレッドを含む)、sleepサスペンションスレッド(時間単位s)、pthread_join()メインスレッドをスレッドの終了を待つ
pause(); サスペンション改スレッドは、信号量入力が直接あり、後のコードは実行されません.
QNX Momenticsで「File->New->Example...」を選択サンプルウィザードを開き、「QNX Example Neutrino Threads Project」を選択します.次にデフォルト設定を保持し、「Next」をクリックし、「Finish」をクリックするとQNXマルチスレッドサンプルプロジェクトが作成されます.
作成したプロジェクトのターゲットシステムはX 86なので、QNX仮想マシン上で変更することなく実行できます.プロジェクトを選択し、右クリックして「Build Project」を選択してプロジェクトをコンパイルします.
正常に実行されると、コンソールは次のように出力されます.
Creating thread now.
functionA active
Thread was created.
The main thread is going on...
... as long as it hasn't used up its timeslice.
Creating another thread.
functionB is working...
Thread was created.
The main thread continues...
We are now creating two more threads...
universalthread is now running...
universalthread: localvariable 3 * parameter 12 = 36
universalthread is now running...
universalthread: localvariable 3 * parameter 59 = 177
Threads created.
Main thread now waiting for first thread to finish
universalthread: localvariable 15 * parameter 12 = 180
universalthread: localvariable 62 * parameter 59 = 3658
universalthread: localvariable 27 * parameter 12 = 324
universalthread: localvariable 121 * parameter 59 = 7139
functionA is finished.
Thread 2 completed its function!
Main thread now waiting for termination through user.
Other threads keep running in the background until
they finish or are terminated together with the main thread.
universalthread: localvariable 39 * parameter 12 = 468
universalthread: localvariable 180 * parameter 59 = 10620
functionB done.
universalthread: localvariable 51 * parameter 12 = 612
universalthread: localvariable 239 * parameter 59 = 14101
universalthread with parameter 12 finished.
universalthread with parameter 59 finished.
ソース・プログラム・コードは次のとおりです.
/* T H R E A D C R E A T I O N */
/* Project Name: "example_threads" */
/* What are threads under QNX?
* Under QNX Neutrino, there are processes and threads. It's
* not a process that actually runs, it's (at least) one thread
* that runs inside.
* Threads are the ones being scheduled, they are consuming
* CPU time when they run.
* Per default, a process has one thread, the main thread.
* To create this main thread, you don't need to do anything,
* it is created automatically for you.
* You can create further threads within your process, for
* example to parallelize tasks. A process has its own virtual
* address space. All threads share the memory of the process.
* In this example program, we will create some additional
* threads using the appropriate functions.
*/
/* In a second thread related example,
* "example_sched.c", we will play with priorities
* and scheduling algorithms.
*/
/* More information:
* See "Processes and Threads" in the Programmer's Guide,
* chapter "Programming Overview".
* You will find it in the Help Viewer.
*/
#include
#include
#include
#include // needed when you create thread
#include
#include
void *functionA();
void *functionB();
void *universalthread(int parameter);
/* At this point our process goes live and has one thread, the
* main thread. All setup for this was done by the OS.
*/
int main () {
pthread_t our_thread_id;
/* Next, we will create own, additional threads within this
* process. All our threads will print something to the console,
* so don't be surprised when it looks a little mixed-up. :-)
* The function pthread_create is used to create new threads.
* Parameters: a place for the thread ID, thread attributes,
* address of the function for the thread to execute, and a
* place to possibly pass in a parameter to that function.
*/
/* Let's create a thread!
* Its thread ID will be put inside "our_thread_id".
* For the Thread attributes (priority, scheduling) we use
* the default values (Prio 10, Round Robin Scheduling). To
* do this we just need to pass in NULL as parameter.
* The function our first thread shall execute is functionA().
* As a parameter for this function we don't pass anything in,
* so we use NULL. */
printf("Creating thread now.
");
pthread_create(&our_thread_id, NULL, &functionA ,NULL);
printf("Thread was created.
");
/* The thread is created now. It will print something from
* time to time. Our main thread continues to run as well, and
* will run until it has consumed its time slice (round robin
* scheduling). The newly created thread will run afterwards,
* as it was placed at the end of the queue of threads that want
* to get the CPU (the "ready queue").
*/
printf("The main thread is going on...
");
printf("... as long as it hasn't used up its timeslice.
");
/* Let's create another thread, this time with a different
* function: functionB(). This time we don't care about the
* thread ID, so we pass in NULL for that.
*/
printf("Creating another thread.
");
pthread_create(NULL,NULL, &functionB ,NULL);
printf("Thread was created.
");
printf("The main thread continues...
");
/* You can provide own code (an individual function) for each
* thread, but you don't have to at all. You can also create
* a whole bunch of threads all using the same code.
* This works very well, because every thread has its own stack.
*/
/* Now let's create a third and a forth thread, this time both
* will use the same code (function). We will call the function
* "universalthread()". We will pass a value to the function.
* Inside the thread we will use this value to determine which
* thread is which upon console output.
*/
printf("We are now creating two more threads...
");
pthread_create(NULL,NULL, (void *) &universalthread, (void *) 12);
pthread_create(NULL,NULL, (void *) &universalthread, (void *) 59);
printf("Threads created.
");
/* Now, what shall we do in the main thread? In fact you can do
* anything you want. It can do further work, or it can wait
* for another thread to complete (thread exits its function).
* Let's wait for a thread to complete! For this we need its
* thread ID and the function pthread_join().
*/
printf("Main thread now waiting for first thread to finish
");
/* When we created the first thread, we stored the thread ID
* in "our_thread_id". We use it now. The function pthread_join()
* will not return until the corresponding thread completes.
*/
pthread_join(our_thread_id, NULL);
printf("Thread %d completed its function!
",our_thread_id);
/* Now let's have the main thread wait for cancellation through
* user (Ctrl+C). The other threads will run until they are
* completed. If you press Ctrl+C before they are completed,
* they are terminated along with the main thread. If you wait
* a while, the threads will finish on their own. Type "pidin"
* on a different console or look in the System Information
* Perspective - the finished threads will be labelled 'dead'.
*/
printf("Main thread now waiting for termination through user.
");
printf("Other threads keep running in the background until
");
printf("they finish or are terminated together with the main thread.
");
/* Hitting Ctrl+C sends a SIGTERM signal to the process. If the
* process receives this signal, it is killed along with all of
* its threads. We now use the pause() function to wait for
* the signal.
*/
pause();
/* Hitting Ctrl+C will end the whole program, thus any lines
* after the pause() and the } will not be executed.
* If you want the main thread exit after the others threads
* finished, use pthread_join() as shown above.
* Have fun while exploring!
*/
return EXIT_SUCCESS;
}
void *functionA()
{
printf("functionA active
");
sleep(5);
printf ("functionA is finished.
");
return(NULL);
}
void *functionB()
{
printf("functionB is working...
");
sleep (7);
printf("functionB done.
");
return(NULL);
}
void *universalthread(int parameter)
{
int localvariable;
int counter = 0;
localvariable = 3;
printf("universalthread is now running...
");
while ( counter < 5) {
printf ("universalthread: localvariable %d * parameter %d = %d
", localvariable, parameter, (localvariable * parameter));
localvariable = localvariable + parameter;
counter++;
sleep(2);
}
printf("universalthread with parameter %d finished.
",parameter);
return(NULL);
}
まとめ、pthread_create(,,)作成スレッド(共有リソーススレッドを含む)、sleepサスペンションスレッド(時間単位s)、pthread_join()メインスレッドをスレッドの終了を待つ
pause(); サスペンション改スレッドは、信号量入力が直接あり、後のコードは実行されません.