Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

Cのpthread_equal()


pthread_equal()関数は、2つのスレッドが等しいかどうかをチェックするために使用されます。これは0またはゼロ以外の値を返します。等しいスレッドの場合、ゼロ以外を返します。それ以外の場合は0を返します。この関数の構文は次のようになります-

int pthread_equal (pthread_t th1, pthread_t th2);

ここで、pthread_equal()の動作を見てみましょう。最初のケースでは、セルフスレッドをチェックして結果をチェックします。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t sample_thread;
void* my_thread_function(void* p) {
   if (pthread_equal(sample_thread, pthread_self())) { //pthread_self will return current thread id
      printf("Threads are equal\n");
   } else {
      printf("Threads are not equal\n");
   }
}
main() {
   pthread_t th1;
   sample_thread = th1; //assign the thread th1 to another thread object
   pthread_create(&th1, NULL, my_thread_function, NULL); //create a thread using my thread function
   pthread_join(th1, NULL); //wait for joining the thread with the main thread
}

出力

Threads are equal

2つの異なるスレッドを比較すると、結果が表示されます。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t sample_thread;
void* my_thread_function1(void* ptr) {
   sample_thread = pthread_self(); //assign the id of the thread 1
}
void* my_thread_function2(void* p) {
   if (pthread_equal(sample_thread, pthread_self())) { //pthread_self will return current thread id
      printf("Threads are equal\n");
   } else {
      printf("Threads are not equal\n");
   }
}

main() {
   pthread_t th1, th2;
   pthread_create(&th1, NULL, my_thread_function1, NULL); //create a thread using my_thread_function1
   pthread_create(&th1, NULL, my_thread_function2, NULL); //create a thread using my_thread_function2
   pthread_join(th1, NULL); //wait for joining the thread with the main thread
   pthread_join(th2, NULL);
}

出力

Threads are not equal

  1. スレッドの優先度を表示するC#プログラム

    C#でスレッドの優先度を表示するには、優先度を使用します プロパティ。 まず、 currentThreadを使用します スレッドに関する情報を表示するプロパティ- Thread thread = Thread.CurrentThread; ここで、 thread.Priorityを使用します スレッドの優先度を表示するプロパティ- thread.Priority 例 C#でスレッドの優先度を示す完全なコードを見てみましょう。 using System; using System.Threading; namespace Demo {    class MyClass

  2. Javaのスレッドプール

    スレッドプールは、事前に初期化されたスレッドのコレクションです。スレッドプールの背後にある一般的な計画は、メソッドの起動時にさまざまなスレッドを形成し、それらが座って作業を期待する場所に配置することです。サーバーは参加の呼び出しを受信すると、このプールからスレッドを起動し(使用可能な場合)、サービスの要求を渡します。スレッドがサービスを完了すると、プールに戻り、多くの作業を待ちます。プールにアクセス可能なスレッドが含まれていない場合、サーバーはスレッドが解放されるまで待機します。 新しいスレッドを作成する必要がないため、時間を節約できます。 サーブレットとJSPで、リクエストをメソッド化す