我正在嘗試構建一個電梯模擬器程式,并且開始使用乘客功能。我需要為每位乘客創建一個執行緒并要求他們輸入他們的樓層號碼。我已經能夠創建所需的乘客(執行緒)數量,我只需要詢問他們的輸入。但我一直無法弄清楚
代碼
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_PASSENGERS 5
#define floor 5
#define NUM_THREAD 6
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; /*Mutex Initializer*/
int dest_floor;
int elevator[floor];
int current_floor;
int no_passengers;
int t, user_input;
void *passengers(void *threadid)
{
// Getting thread ID
long tid;
tid = (long)threadid;
printf("Passenger %d Kindly select your floor number :\n", tid);
scanf("%d\n", &user_input); // Where I ask them for input
// End of Getting ID
pthread_exit(NULL);
}
int main()
{
pthread_t thread[NUM_THREAD];
int th;
for (t = 1; t < NUM_THREAD; t )
{
th = pthread_create(&thread[t], NULL, passengers, (void *)t);
if (th)
{
printf("ERROR Creating Thread\n");
exit(-1);
}
}
pthread_exit(NULL);
}
我嘗試了什么:
void *passengers(void *threadid)
{
// Getting thread ID
long tid;
tid = (long)threadid;
printf("Passenger %d Kindly select your floor number :\n", tid);
scanf("%d\n", &user_input); // Where I ask them for input
// End of Getting ID
pthread_exit(NULL);
}
我所期待的:
Passenger 2 Kindly select your floor number:2
Passenger 3 KIndly select your floor number:4
... and so on
我得到了什么:
Passenger 1 Kindly select your floor number:
Passenger 2 KIndly select your floor number :
Passenger 3 Kindly select your floor number:
Passenger 4 KIndly select your floor number :
Passenger 5 Kindly select your floor number:
2
3
4
5
6
它不允許用戶在提示符上輸入樓層號,而是列印出所有執行緒然后收集輸入。
uj5u.com熱心網友回復:
這是基于@Lundin 評論的更新版本。感謝那
這是使用您已經定義的互斥鎖的版本,因此一次只有一個執行緒正在讀取stdin. 請注意,它也user_input必須是一個區域變數,passengers()否則它會在執行緒之間共享,并且我已經修復printf()(%ldfor tid)并更改了資料通過arg.
通常你傳遞一個指向包含所有必要資訊的結構的指標,所以我在這里做。您必須確保每個執行緒都收到該結構的不同副本。在這個例子中,這是通過在每次呼叫之前分配新記憶體來解決的,在退出之前,執行緒本身中的記憶體thread_create()是free()d 。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>
#define MAX_PASSENGERS 5
#define floor 5
#define NUM_THREAD 6
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*Mutex Initializer*/
int dest_floor;
int elevator[floor];
int current_floor;
int no_passengers;
int t;
struct passenger_ctx {
long tid;
};
void *passengers(void *ctx )
{
// Getting thread ID
struct passenger_ctx *p_ctx = ctx;
long tid;
int user_input;
tid = p_ctx->tid;
pthread_mutex_lock( &mutex );
printf("Passenger %ld Kindly select your floor number :\n", tid);
scanf("%d", &user_input); // Where I ask them for input
pthread_mutex_unlock( &mutex );
printf( "Passenger %ld selected %d\n", tid, user_input );
// End of Getting ID
free( p_ctx );
pthread_exit(NULL);
}
int main()
{
pthread_t thread[NUM_THREAD];
int th;
for (t = 1; t < NUM_THREAD; t )
{
struct passenger_ctx *p_ctx;
p_ctx = calloc( 1, sizeof *p_ctx ); // error check to be added
p_ctx->tid = t;
th = pthread_create(&thread[t], NULL, passengers, p_ctx );
if (th)
{
printf("ERROR Creating Thread\n");
exit(-1);
}
}
pthread_exit(NULL);
}
現在看起來像這樣:
Passenger 1 Kindly select your floor number :
9
Passenger 1 selected 9
Passenger 2 Kindly select your floor number :
8
Passenger 2 selected 8
Passenger 4 Kindly select your floor number :
7
Passenger 4 selected 7
Passenger 3 Kindly select your floor number :
6
Passenger 3 selected 6
Passenger 5 Kindly select your floor number :
5
Passenger 5 selected 5
uj5u.com熱心網友回復:
執行緒是……多執行緒的。這意味著執行是并發的。您不能/不應該假設任何特定的執行順序,因為這是由作業系統處理的。就像隨時可能發生的執行緒之間的背景關系切換一樣。實作您想要的唯一方法是在當前執行緒正在執行時停止所有其他執行緒,這將有效地破壞使用執行緒開始的目的。
我知道這只是一個用于學習執行緒的人造學術程式,但現實世界的程式被安排得相當好,以便一個執行緒處理所有用戶輸入。正如評論中所指出的,所有執行緒共享相同的stdin. 所以這個程式沒有多大意義。
明智的解決方案可能是在創建執行緒之前在 main() 中詢問所有這些資訊,然后將樓層號作為引數傳遞給執行緒。這樣,您還將練習將引數傳遞給執行緒,這對學習很重要。也許相反,每個執行緒在開始時列印“乘客 x 正在 y 樓等待”?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531157.html
標籤:C多线程线程
